How do i find if an object is before or after a waypoint?

Posted by BoMann Andersen on Stack Overflow See other posts from Stack Overflow or by BoMann Andersen
Published on 2012-12-15T11:52:49Z Indexed on 2012/12/18 11:03 UTC
Read the original article Hit count: 187

Filed under:
|
|

Im working on a racing game for a school project. Using Visual studio 10 pro, and Irrlicht. Sorry for bad grammar >.>, and its my first question so not sure if its done right.

How i want it to work is that i make waypoints at different points on the track, and then i run my waypoint check to see if a car is past its next waypoint (the next it "needs" to go past), if yes then it updates the next waypoint, else nothing.

The way i hope this will work is, i make a vector from n to n+1, then find the vector that is perpendicular to the first vector at n. Then i see if the object is in front or behind that vector. I found a Gamedev.net forumpost that helped me make this function:

void Engine::checkWaypoint(Vehicle* vehicle)
{
  btVector3 vector = waypoints[vehicle->nextWaypoint];
  // n
  btVector3 nextVector = waypoints[vehicle->nextWaypoint + 1];
  // n+1
  vector = nextVector - vector;
  // First vector
  btVector3 pos = btVector3(vehicle->position.X,vehicle->position.Y,vehicle->position.Z);

  float product = vector.dot(pos - waypoints[vehicle->nextWaypoint]);
  // positiv = before, negative = behind

  if(product < 0)
    vehicle->nextWaypoint += 1;
}

Current bugs with this is:

  1. Updates the nextwaypoint more then ones without going past a new point.

  2. When it gets to the end and resets, it stops triggering on the first waypoints.

So my questions:

Is this an good way to do this?

Did i do it right?

© Stack Overflow or respective owner

Related posts about c++

Related posts about irrlicht