Skip to content Skip to sidebar Skip to footer

Move Object Along A Straight Line

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

Solution 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.

Post a Comment for "Move Object Along A Straight Line"