#### Archived

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

# Following a Line with a Sprite

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

## Recommended Posts

Hi All I am curious if there is a algo to make a sprite follow a line (ie move from point1 to point2) which can be run real time (taking into account frame speed and a movement speed) or is it best to generate the x,y cords of everypoint on the line (gives line length points) and the movement amount is basically calculated as an offset into the line point array.. i hope that makes sence.. Cheers Chris Jumpman - Under Construction

##### Share on other sites
Use time based movement and just have waypoints (x,z) and have the sprite follow the terrain''s y-axis height.

##### Share on other sites
You could use bresenham''s line algorhitm for the locations where to draw the sprite.

##### Share on other sites
Im not so sure what you mean but if you want a sprite to follow a line just create a normalized vector of the two points then multiply it by the speed and then add the vector multiplied by delta time to your sprites coords every frame.
In code that would be something like this:
Say we have Points A(a1, a2, a3) and B(b1, b2, b3)
struct Vector{    FLOAT x;    FLOAT y;    FLOAT z;    void CreateNormalizedFromPoints(float x1, float y1, float z1,                                    float x2, float y2, float z2)    {        x = x2-x1;        y = y2-y1;        z = z2-z1;        //Normalize(setting length to 1 using pythagoras)        float length2d = sqrt(x*x+y*y);        float length = sqrt(length2d*length2d+z*z);             x/=length;        y/=length;        z/=length;    }};void main(){    Vector dir;    dir.CreateNormalizedFromPoints(a1,a2,a3,b1,b2,b3);    while(1)    {        //warning pseudocode        objpos.x+=dir.x*speed*deltat;        objpos.y+=dir.y*speed*deltat;        objpos.z+=dir.z*speed*deltat;                Draw();    }}-CProgrammerMy Website

[edited by - CProgrammer on July 3, 2003 3:38:17 AM]

##### Share on other sites
thanks all.. that looks great.. many thanks

Jumpman - Under Construction

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013294
×