Following a Line with a Sprite

Started by
3 comments, last by Jumpman 20 years, 9 months ago
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
Advertisement
Use time based movement and just have waypoints (x,z) and have the sprite follow the terrain''s y-axis height.
You could use bresenham''s line algorhitm for the locations where to draw the sprite.
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
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]
thanks all.. that looks great.. many thanks

Jumpman - Under Construction

This topic is closed to new replies.

Advertisement