Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Animating a sprite between 2 points  (Read 1959 times)

0 Members and 1 Guest are viewing this topic.

peacemaker

  • Newbie
  • *
  • Posts: 2
    • View Profile
Animating a sprite between 2 points
« on: January 10, 2012, 05:03:18 pm »
Hey guys,

I understand the basics of it, you step the sprite by offset*elapsed frame time per frame in the update loop until it reaches the destination.

But I think I'm missing some (I admit elementary) math when it comes to something like moving the sprite diagonally for example, imagine a wizard casts a spell at a monster.  The wizard is at (X:100,Y:100) and the monster is at (X:500, Y:10).

My current code in this example would increase X and decrease Y for each step but what happens is Y gets to the destination before X so you have a very strange looking effect where the spell travels diagonally until it is at the correct height then it goes horizontally until it reaches the destination.

So what am I missing?  I feel like that is some simple math which can calculate the steps better :)

aBallofWin

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Animating a sprite between 2 points
« Reply #1 on: January 10, 2012, 05:16:03 pm »
Hey, i doubt i could help you with this (as i'm a beginner) but i think others would be able to if you posted your code :)

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Animating a sprite between 2 points
« Reply #2 on: January 10, 2012, 05:17:32 pm »
You must first calculate the distance between the points. For x this will be 500-100 = 400 and the y will be -90.

The ratio of these numbers is −4.444, so for every y value you decrement, the  x value must be added with 4,444.
Or for every x value you increment, the y value must be decreased by 0,225.

I hope you will be able to put that into a code, I don't have time for it now.
TGUI: C++ SFML GUI

peacemaker

  • Newbie
  • *
  • Posts: 2
    • View Profile
Animating a sprite between 2 points
« Reply #3 on: January 10, 2012, 05:33:02 pm »
Hey texus,

Thanks for that - it was along the lines I was thinking but I have had a look around online and read about various 2d vector translations, working out the angle between the two points and so on and assumed there was a "better" solution.

However, if simply updating the sprites x and y in the manner you suggested works then I don't see why I'd need to worry about the other stuff :)

 

anything