Following a set of points?

Posted by user1010005 on Game Development See other posts from Game Development or by user1010005
Published on 2012-03-30T04:58:36Z Indexed on 2012/03/30 5:42 UTC
Read the original article Hit count: 211

Filed under:

Lets assume that i have a set of path that an entity should follow :

const int Paths = 2
Vector2D<float> Path[Paths] = { Vector2D(100,0),Vector2D(100,50)   };

Now i define my entity's position in a 2D vector as follows :

Vector2D<float> FollowerPosition(0,0);

And now i would like to move the "follower" to the path at index 1 :

int PathPosition = 0; //Start with path 1

Currently i do this :

Vector2D<float>& Target = Path[PathPosition];
bool Changed = false;

if (FollowerPosition.X < Target.X) FollowerPosition.X += Vel,Changed = true;
if (FollowerPosition.X > Target.X) FollowerPosition.X -= Vel,Changed = true;
if (FollowerPosition.Y < Target.Y) FollowerPosition.Y += Vel;,Changed = true;
if (FollowerPosition.Y > Target.Y) FollowerPosition.Y -= Vel,Changed = true;

if (!Changed)
{
    PathPosition = PathPosition + 1;
    if (PathPosition > Paths) PathPosition = 0;
}

Which works except for one little detail : The movement is not smooth!! ...So i would like to ask if anyone sees anything wrong with my code.

Thanks and sorry for my english.

© Game Development or respective owner

Related posts about math