Finding next step in line from point a to b

Started by
4 comments, last by griffenjam 22 years, 5 months ago
I am trying to make a 2D missile move from point a to b, but given 2 2D points how do I find my path? Do I have to use a line drawing alog? Isn't there an esier way? Edit: Also note that I am using OpenGL. Jason Mickela ICQ : 873518 E-Mail: jmickela@sbcglobal.net ------------------------------ "Evil attacks from all sides but the greatest evil attacks from within." Me ------------------------------ Edited by - griffenjam on October 23, 2001 2:29:40 PM
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
Advertisement
if you have the speed of your missile and the angle to the target this is easy...
dX = cos(angle) * speed
dY = sin(angle) * speed
dX and dY are the amount to move the missile...
make sure you use a fairly precise type of number to store these though (an integer will round it off so much that the missile probably won''t hit the target)...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
The line algorithm would be the simplest way if you already know p1 and p2.
linear interpolation (LINEar, Inter=Between Poles)...

P = A + ( (B-A) * t)

A is point A, B is point B, and t is how far along the line you want (between 0 to 1 [0.5 is halfway, 0.25 is quarter way etc]). In code:

  // stuff you inputfloat t;float pointa_x, pointa_y;float pointb_x, pointb_y;// what you get backfloat result_x, result_y;// do the linear interpolationresult_x = pointa_x + ((pointb_x - pointa_x) * t);result_y = pointa_y + ((pointb_y - pointa_y) * t);  



An example (draw it as a graph on paper to get the idea):

if pointa is 0,0 and pointb is 640,480...

t=0 would give a result of 0, 0
t=0.25 would give a result of 160, 120
t=0.5 would give a result of 320, 240
t=0.75 would give a result of 480, 360
t=1 would give a result of 640, 480


All you do to travel along the path is increase t from 0 to 1.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

If you want to use all integer math you can use Bresenham''s Line Algorithm, it''s a bit more complex though. (You probably knew this already, given that you mention line-drawing algos in your post, just posting for the benefit of others.)
Thanks alot for the replys, I''ll use krez''s method.
I was using a linear equation ( y=mx+b). That works, but I''d like a method where I can easily set the angle the missile flies at.

Thanks again.


Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net
------------------------------
"Evil attacks from all sides
but the greatest evil attacks
from within." Me
------------------------------
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery

This topic is closed to new replies.

Advertisement