Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing a race game in Andengine. I have created a car which player is controlling. But since this is a race game, there should be 3 or 4 car which snatch with the player's car. My question is: How do the opponent cars move along the path? How their logic works? Must I know certain path?

share|improve this question

In racing games it's often best to use both track relative coordinates as well as Cartesian world coordinates. This makes it easier to reason about overtaking and optimal lines the cars should follow.

A great article to read would be Shawn Hargreaves' postmortem on the MotoGP AI

You can also try to use follow and flock algorithms like Open Steer

If you are more interested in how to determine the optimal curve that (AI) drivers should follow on a track you could research flow fields. You can find more info about those in the links in this answer on this same website.

share|improve this answer

I outline one simple implementation:

Assume your track is a series of points joined together and the last point joins the first to form a cicular track.

loop through all the points and find the nearest point to your car. Take this point and minus the previous point to get a normalized track forward vector; take the car position minus the previous point to get a "car vector"; do a dot product to get the perpendicular distance from the track vector.

The perpendicular distance represents how far away the car is from the center of the track. Now you know the distance and on which side of the track the car is on, you can steer (change the rotation/direction) the car back onto the track.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.