Opponent car logic in race games

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?

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.

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.

I'm not a high level programmer, I just know some basic java, however I am a racer. Racing games such as Forza have a very good AI, but other games that have racing as a secondary feature don't. When constructing this "dot track" based on coordinates of the track and world, I'd recommend watching ViperConcepts on YouTube and trying to liken the line you create for your track to his recommended racing line. In my experience of real life racing vs Forza, the main problem, even in the best "simcade" games, such as Forza, GT, Project Cars, etc... the main thing that I notice detracts from the realism is the fact that the AI are very predictable and they usually don't take themselves out, pass eachother, or make mistakes. They don't have much aggression as far as overtaking or defending/blocking. I assume your game won't be competing to be the next Forza, but I'd definitely love to play a game where the AI are a little bit more of a mess maybe, rather than a parade. In real life, I've ended up on the podium with a damaged engine, just by keeping my nose out of trouble. I want to say good luck with your game and I hope it manages to capture some of these elements!