java - 沿直线移动对象

标签 java android math path line

我正在尝试沿直线路径移动 Sprite。每次执行该方法时,我都想在斜坡上或斜边上移动 5 个像素,直到到达终点。

我有直线的斜率和 y 轴截距,我还通过 getX() 和 getY() 获得了 Sprite 的当前 X 和 Y 值。最后停止的 X 和 Y 点是变量 finalX 和 finalY。

我已经尝试了很多方程式,但我似乎无法让它们中的任何一个起作用。我错过了什么!!?

Hopefully this image makes sense for what I am trying to do!

我最近的方程式试图使用 y=mx+b。

float X = (getY() + 5 - interceptY)/slope;
float Y = slope*(getX() + 5) + interceptY;
setPosition(X, Y);

最佳答案

可以用我最近玩的游戏中的一些方程式帮助你,代码根据物体的旋转移动物体:

float xDirection = FloatMath.sin((float) Math.toRadians(getRotation()))
            * currentSpeed;
float yDirection = FloatMath.cos((float) Math.toRadians(getRotation()))
            * -currentSpeed;

float newX = getX() + xDirection;
float newY = getY() + yDirection;

您只需要导出您需要 Sprite 移动的角度,这就可以了。希望这会有所帮助。

关于java - 沿直线移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11574341/

相关文章:

c++ - 连续分布的指数衰减随机分布和离散化

android - 在 android 的主屏幕上创建应用程序快捷方式时如何防止自动显示 toast 消息我不想要消息

Android,将外部库导入到NDK

java - 运行 ObjectBox 查询时获得随机字符串作为结果而不是值

java - 如何在 Luna Service (4.4.1) 上使用 Eclipse m2e 将 jar 文件添加到本地 Maven 存储库?

android - 右对齐 LinearLayout 的 imageView

c++ - 从 vtk 渲染计算相机空间坐标

c# - 将 3D 点投影到 2D 屏幕坐标

java - 如何在正则表达式中添加参数?

java - 打印屏幕期间的噪音