• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Point to Point Sprite Motion

This topic is 6323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, Is there an easy way or a DirectX function to move a sprite from a certain point x1,y1 to x2,y2 without using trig functions or square roots after every frame? Like in Diablo where you click a point and the character walks in a straight line. What is the most effecient way to accomplish this? VK

##### Share on other sites
no need for trig at all -

say you want to move from x1 to x2 in T seconds/frames/whatever, so... Vx (the x velocity) = (x2 - x1)/T this is computed only once in the animation. Now, at every second/frame/whatever:

x += Vx (until x = x2) and
y += Vy

Of course, you may have to get a little more complex to account for fractional times, rounding errors, etc. But the point is that you don''t need trig anywhere in the algorithm at all.

##### Share on other sites
Not exactly what you are looking for but a little trick that can be used for certain effects...

Assume you have a sprite ''mySprite'' and you wish to move it to the position ''dest'' on the screen.

mySprite->xPos -= (mySprite->xPos - dest->xPos) / step;
mySprite->yPos -= (mySprite->yPos - dest->yPos) / step;

This produces movement such that the sprite decelerates as it moves towards the destination point. The higher the value of ''step'' the more time it takes to reach the destination point.

Cheers,
Sharky

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634067
• Total Posts
3015325
×