我正在尝试沿直线路径移动 Sprite。每次执行该方法时,我都想在斜坡上或斜边上移动 5 个像素,直到到达终点。
我有直线的斜率和 y 轴截距,我还通过 getX() 和 getY() 获得了 Sprite 的当前 X 和 Y 值。最后停止的 X 和 Y 点是变量 finalX 和 finalY。
我已经尝试了很多方程式,但我似乎无法让它们中的任何一个起作用。我错过了什么!!?
我最近的方程式试图使用 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/