Moving a sprite between 2 arbitrary points?

BlitzMax Forums/BlitzMax Beginners Area/Moving a sprite between 2 arbitrary points?

Bukky(Posted 2007) [#1]
Hey guys,

This is a bit frustrating. How do I get a sprite to move between two variable points on a 2d plane with a constant rate of movement? I have a working function now, but my sprite takes the same amount of time to move between two points regardless of distance. D'oh!


jhans0n(Posted 2007) [#2]
You could assign your sprite a speed, and make it so it always moves a certain number of pixels per second.


Bukky(Posted 2007) [#3]
Hmm, well the trick is finding that speed.

Right now I am calculating the offset per frame as such:

x_offset = (destination_x - origin_x)/100
y_offset = (destination_y - origin_y)/100

Where 100 is how many steps it takes to complete the transformation between point 1 and point 2.

Any math wizzes have any ideas?


FlameDuck(Posted 2007) [#4]
Any math wizzes have any ideas?
Yes. Magic numbers. If you don't want to use a fixed speed, calculate a unit vector (to do this, divide by the length of the line, rather than 100), and scale that up by whatever amount is appropriate.


Bukky(Posted 2007) [#5]
Hey Flameduck,

Thanks for that! When you say divide by the length of the line, do you mean to calculate the distance between the two points and then use that as the divisor? I.e. replace the 100 with a variable distance such that:

distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)

?


SSS(Posted 2007) [#6]
Yea, that'll work and then just multiply it by the desired speed.


Grey Alien(Posted 2007) [#7]
Yes, when you work out the distance using pythag, you then divide the X and Y speed by the distance to get a "normalised vector". This means the x and y speeds are basically in the range -1 to 1 (as a floating point number, so make sure your variables are floats or doubles). Then you multiply the X and Y speeds by a fixed amount which is your actualy end speed.