Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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);

share|improve this answer
    
Thanks. This is what I was also considering. Nice clear answer. –  Fofole Jan 26 '12 at 15:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.