Enemy car moving logic in race games

510 views Asked by At

I'm developing a race game in Andengine.I created a car which player is controlling.But because of being race game, there should be 3 or 4 car which snatch with the player's car.My question is: How do the other cars move along to path? How are their logics? Must I know certain path?

1

There are 1 answers

0
Cygon On

That's a pretty wide question. From the Andengine I see that it is a 2D game engine, so I I'm going to assume that you're doing a Micro Machines clone.

A very simple method is to construct a poly line (a list of line segments) around the desired route of your race track. Then do the following:

  • Determine the closest point to the car on the poly line (for example by looking for the poly line vertex closest to the car, then finding the closest point on the two connecting line segments)
  • Now follow the poly line by a fixed or variable amount (variable = depending on the car's speed). This is also simple math since you can calculate the total lengths of the line segments and just sum them up until you know which line segment your targeting point is in
  • Then make the car steer towards this point. If the car slides a lot, tune the lookup to pick points further ahead so the car will turn earler.