I’m a bit new Flash (only began working on it seriously this year) but lately I’ve been experimenting with simple Flash games. About a week ago I began working on racing game. I have everything already working for a single car moving around a tile-based course, but I’ve never dealt with AI before and I’m trying to determine how to create computer controlled opponents. I’m basing movement for the opponent cars on the typical sin/cos radial movement setup I’m using for the player car:
[FONT=Courier New] if (this.ACCELERATING) {
if (this.speed<this.maxspeed) {
this.speed += this.acceleration;
}
} else if (this.BRAKING) {
if (this.speed>this.maxreverse) {
this.speed -= this.brake;
}
} else {
this.speed *= this.decelerate;
}
if (this.TURNLEFT) {
this._rotation -= this.handling;
} else if (this.TURNRIGHT) {
this._rotation += this.handling;
}
this.xspeed = this.speed*Math.sin(_rotation*(Math.PI/180));
this.yspeed = this.speed*Math.cos(_rotation*(Math.PI/180))*-1;[/FONT]
What I’m considering for the AI is a series of invisible waypoints and checkpoints that apply only to the opponent cars. Each opponent checkpoint will intersect the entire width of the track and each waypoint will be the center of its respective checkpoint:
As the AI cars travel, their goal will be to reach the next waypoint. Even though they’re attempting to get to the waypoint, as long as the cross the appropriate checkpoint, they get to move towards the next waypoint in order. It’s easy enough to do a hit test on the checkpoint, reset the goal for the next waypoint and to determine the distance between an AI car and the next waypoint, but after that I’m getting stumped… I’m unsure how to determine when the AI car should turn (and in which direction,) when the car should accelerate, etc. Basically, I can’t figure out how to make the AI cars go for their goals.
Beyond all that, I’m not even sure this is the best route to go. As I said, I’m a bit new to this, and though I’ve been programming for years, I’ve never attempted any type of game logic like this. If anyone has any suggestions to share, please do.