I have a sprite with an UpdateHandler that can change it's position every 0.1 seconds.
My Question is , given the Sprites current position, how can I move it to another position(like from 10,10 to 250,250).
I found out the rotation angle for my sprite like this:
double alfa = (float) Math.atan2(endY - startY, endX - startX) * 180 / PI;
And an example of moving the position is:
sprite.setPosition(sprite.getCurrentX()+1, sprite.getCurrentY());
This would move my sprite every 0.1 seconds to the right. I need to make it work between any 2 points.
Thanks!
Answer
All you need to do is calculate the delta's (difference) between the old position and the new position and then choose in how much time you want to change the position.
For example: X1 = 0, X2 = 100, then deltaX = X2-X1 = 100. Y1 = 50 Y2 = 0, deltaY = Y2-Y1= -50.
Now You want to object to move there in 10 seconds and the update step is every 1 second. So you divide the deltas by 100, since there are 10 steps in 1 second * 10 gives 10 seconds in 100 steps.
So this will be what's in your update step
sprite.setPosition(sprite.getCurrentX()+ deltaX/100, sprite.getCurrentY() + deltaY/100);
No comments:
Post a Comment