#### Archived

This topic is now archived and is closed to further replies.

# Finding next step in line from point a to b

This topic is 6144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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)

##### Share on other sites
The line algorithm would be the simplest way if you already know p1 and p2.

##### Share on other sites
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

##### Share on other sites
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.)

##### Share on other sites
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
------------------------------

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632168
• Total Posts
3004543

×