## Algorithm for moving between two points on a plane...

Member
Posts: 321
Joined: 2004.10
Post: #1
I've taken a stab at writing a function to do this, but I wouldn't
exactly call it elegant or even pretty and I suspect it has problems.

Seems like this would be a pretty common algorithm but I'm not finding

Apologies in advance if this is trivial but I don't want to
reinvent the wheel; especially since mine seems to be built with
bricks, string, and glue.
Member
Posts: 370
Joined: 2002.04
Post: #2
Do you want your object to move at a constant velocity or to move in a fixed amount of time?

If you want fixed time, just figure out where it will be at any given frame by curpos = oldpos + (newpos-oldpos)/numframes*framenum or something like that.

Velocity based, figure out which direction it should be going using atan2 (the arctangent) and then use sine*velocity and cosine*velocity as the distance to move per unit time.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Moderator
Posts: 460
Joined: 2002.09
Post: #3
One method would be to subtract the coordinates of the origin point from the destination point. This yields a vector.

Note that if you add this vector to point1 you get point2. Note also that if you multiply the vector by a fractional value, you get a new vector that when added to point1 moves point1 some of the way towards point2.

So all you need to do is to scale your original difference vector by numbers from 0.0 to 1.0 and add them to the original point. Each time you have moved point1 a little closer to point2.

It's straightforward to figure out how to get it to point2 at a particular moment, or adapt this to do it by velocity (move it different amount depending on how much time has passed.) Ask if you need hints.

Note that I reduce possible floating point precision problems by always applying the math to point1. If you start from the last point you calculated, the degree of error could build up.

(I used a variant of this method for WordBeGone. For that game I wanted a velocity. So I normalized the difference vector, then scaled it by the velocity. For each frame I then multiplied my velocity vector by the time elapsed to see how far it travelled in that time. Worked fine, except that this is almost guaranteed not to leave you exactly at point2 when you are done. So the actual algorithm was, move towards point2 until time >= target_time, at which point just put the object at point2.)

Measure twice, cut once, curse three or four times.
Member
Posts: 321
Joined: 2004.10
Post: #4
Thanks. I seem to have implemented the arctangent, etc. solution.
Although the 2nd solution looks interesting.

I finally put it (my function) under a test harness and it is working! So
my problem lies elsewhere. It was late and I probably posted to early
in frustration.
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
If you just want to get a straght line between two points A (x, y, z), B (x, y, z),
the easiest way would be to get
C = (A.x*u+B.x*(1-u), A.y*u+B.y*(1-u), A.z*u+B.z*(1-u))
where u goes from 0 - 1, 0 would put you at A, 1 at B 0.5 halfway between etc.
So if you knew the starttime, endtime and currenttime (is seconds or constant size of ticks),
u = currenttime/(endtime-starttime)

You can precalculate 1-u to make it faster...

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!