Algorithm for moving between two points on a plane...
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.
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.
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 + (newposoldpos)/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.
If you want fixed time, just figure out where it will be at any given frame by curpos = oldpos + (newposoldpos)/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?
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.)
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.
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.
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.
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*(1u), A.y*u+B.y*(1u), A.z*u+B.z*(1u))
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/(endtimestarttime)
You can precalculate 1u to make it faster...
the easiest way would be to get
C = (A.x*u+B.x*(1u), A.y*u+B.y*(1u), A.z*u+B.z*(1u))
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/(endtimestarttime)
You can precalculate 1u to make it faster...
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Requesting Help: Smooth keyframe interpolation algorithm  whogben  4  4,632 
Jan 5, 2009 11:15 PM Last Post: whogben 

Angle between two points?  Graphic Ace  6  5,485 
Nov 8, 2008 12:11 PM Last Post: macnib 

Depth Sorting algorithm  Leroy  1  4,507 
Jul 2, 2007 01:47 AM Last Post: aegidian 

Box to Plane Intersection With Quaternions  KiroNeem  6  5,136 
Jun 25, 2006 05:44 PM Last Post: KiroNeem 

Good collision points  Skorche  7  4,644 
Sep 22, 2005 09:09 PM Last Post: Skorche 