Move Object along a straight line

浪子不回头ぞ 提交于 2019-12-13 01:34:28

问题


I am trying to move a Sprite along a straight line path. I want to move it 5 pixels on the slope, or the hypotenuse each time I go through the method until I reach the end point.

I have the slope and y-intercept of the line, I also have the current X and Y values of the sprite through getX() and getY(). The final X and Y points to stop at are variables finalX and finalY.

I have tried so many equations but I can't seem to get any of them to work. What am I missing!!?

My latest equation was trying to use y=mx+b.

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

回答1:


Can help you with a few equations from my recent game, the code moves an object given its rotation:

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;

You just need to derive the angle in which you need your sprite to move and this will do for you. Hope this helps.



来源:https://stackoverflow.com/questions/11574341/move-object-along-a-straight-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!