not sure if subject is worded correctly, but let me quickly explain my problem.
I’m almost finished my turn based racing game, which uses vectors to move cars around the track. Track itself is a 2D image and I use overlay image for collision detection if player goes outside the track or hits some obstacle etc.
For collision detection I do test each pixel on overlay map for specific colours. Car moves from one grid intersection point to another, which are spaced 15 pixels.
Anyway, all works fine.
My problem is that I want to implement AI in this game which should find its own way around the track. As it is my first game ever I’m not sure which way is better to implement it (keeping in mind it’s an iPhone game).
Do you think I should have a tree with nodes of possible moves around the track and implement one of A* algorithms?
Or would it be better to do per pixel collision detection?
Something like testing say 50 pixels ahead, if all clear go that path if not test by few degrees to one side etc.?
I’m afraid that per pixel test could be costly on iPhone as for each move I might need to do ~400 tests?
Please share your wisdom, any input is really appreciated!
Typically, a racing game relies on waypoints marking out the track. These are placed to trace an ideal racing line (ie, shortest distance around the track). An NPC car can be made to aim for the next waypoint, then, when it passes that waypoint, to aim for the next after that and so on around the track. The car needs a bit of lookahead to anticipate what is coming up on the track (straight section, tight curve, etc) and vary the speed accordingly.
Beyond that, the AI needs to make decisions about how to avoid other cars on the way around. Again, looking a few waypoints ahead will tell the AI if it has enough time to attempt to overtake or should just cruise where it is.
All of this has some dependence on physical calculations or heuristics about the car’s performance. This will include the car’s braking and acceleration rate and how tight a corner it can turn for a given speed. If a tight corner is approaching, then the car will need to slow down, so it probably shouldn’t attempt to overtake, etc.
It really depends on how much complexity you want to build into the game, but there are obviously any number of additions you could make to this scheme to make the cars cleverer. For example, you could arrange for a car to detect when another was trying to overtake and then move to block it.