View Full Version : Algorithm for moving between two points on a plane...
WhatMeWorry
2005.08.23, 03:07 AM
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
any hits with google, etc.
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.
Steven
2005.08.23, 03:14 AM
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.
MattDiamond
2005.08.23, 08:33 AM
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.)
WhatMeWorry
2005.08.23, 12:09 PM
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.
unknown
2005.08.23, 03:36 PM
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...
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.