Skip to main content
GameDev.net gamedev.net
🔒 Locked

Creating a path for an object walks

Started by CSharper Oct 24, 2005 at 7:09 AM 2 replies 750+ views
Original Post
CSharper
CSharper
Hi fellows I need to create a path for an object walk. Sample: I have 2 lines: L1: x1 = 100, y1 = 50, x2 = 200 , y2= 250 L2: x1 = 200 , y1= 250, x2 = 600, y2 = 500 What I want: the object walks in the L1 and in the end of the line, it in the L2. I have any idea to start. Someone can send some tips, tutorials or sites when I can to study this? I think that I'll use sin and cos, but I think, I'm not sure about this.] Thanks for the support
mrbig
mrbig
How about some linear math?
You know, y = mx + b?
You don't need any trigonometry at all.

If you don't have problems with using float numbers, you simply need to do this:

int x1, y1, x2, y2; // Coordinates of the line.int x, y;           // Current coordinates of the object.float m, b;m = (y2 - y1) / (x2 - x1);b = y1 - m * x1; while(x != x2)  {   y = (int)(m * x + b);   x++;   draw_object_at(x, y);  }


That would work only in case that x1 < x2, y1 < y2.
You need to detect all cases to make the algorithm robust.
If you don't want to use floats, everything gets much more complicated, but that is only needed to draw lines, for object's path, floats would do.
CSharper
CSharper
Thanks mrbig

One more thing only, where can I found infos about questions that seems like mine. I want to study more about this. Can you help(again)?
mrbig
mrbig
Of course:
http://www.gamedev.net/reference/articles/article1275.asp

On this page, you will find a simple line algorithm like the one you need, as well as a more complicated one that uses only ints.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.