Archived

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

Jumpman

Following a Line with a Sprite

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 this post


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

Share this post


Link to post
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();
}
}

-CProgrammer

My Website


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

Share this post


Link to post
Share on other sites