1

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

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

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

希望这张图片对我想做的事情有意义!

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

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

1 回答 1

4

可以帮助您解决我最近的游戏中的一些方程式,代码在给定旋转的情况下移动对象:

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;

你只需要导出你需要你的精灵移动的角度,这将为你做。希望这可以帮助。

于 2012-07-20T07:02:30.343 回答