I’ve been trying to make an AI for an asteroids game but have become stuck. Everything else in the game is set up (i.e. asteroids created and going in random directions, collision detection, ship and bullets.) The AI is looped through every 150ms and can only tell the ship to turn left, turn right, move up and shoot.
Here’s a snippet of my code that I’m using so far:
// Convert points to positive
float asteroid_x = Math.Abs(asteroid.X);
float asteroid_y = Math.Abs(asteroid.Y);
float ship_x = Math.Abs(ship.X);
float ship_y = Math.Abs(ship.Y);
// Calculate difference in X and Y using positive values
float dx = asteroid_x - ship_x;
float dy = asteroid_y - ship_y;
// Get velocities of asteroid
float vx = asteroid.VX;
float vy = asteroid.VY;
Using these variables (dx, dy, vx, vy) I need to calculate if the asteroid is going to hit the ship. I’ve been trying to use trigonometry but it keeps thinking it’s going to hit when it isn’t. The equations that I’ve been trying to use are:
float equation1a = (dy / vy) - (dx / vx);
float equation1 = ((dy / dx) - (vy / vx));
I’ve then been trying to use an IF statement to determine whether it’s going to hit or not but I’m not sure how to go about it properly. After this IF statement I then want to go into the AI to either turn and shoot the asteroid or evade it.
Thanks for any help.
EDIT: The X and Y values for the ship or the asteroid can also be negative, with the ship starting on the origin (0,0).
EDIT2: Got a bit further with it all now and come up with this:
// Determine if asteroid will collide
int turns_ahead = 100;
bool collision = false;
int least_turns = 500;
for (int i = 0; i < turns_ahead; i++)
{
PointF asteroid_pos_next = NextPosition(new PointF(asteroid.X, asteroid.Y), new PointF(asteroid.VX, asteroid.VY), i);
if (asteroid.CollidesWith(asteroid_pos_next.X, asteroid_pos_next.Y, ship.R))
{
collision = true;
}
}
And the next position is worked out with this function:
// -----------------------------------------------------------------
// Function to find position of object in X turns (turns_ahead)
// -----------------------------------------------------------------
private static PointF NextPosition(PointF current_pos, PointF velocity, int turns_ahead)
{
float next_x_pos = current_pos.X + (velocity.X * turns_ahead);
float next_y_pos = current_pos.Y + (velocity.Y * turns_ahead);
// IFs to deal with screen wrap-around
if (next_x_pos < -Form1.maxX)
{
next_x_pos = Form1.maxX;
if (next_y_pos < 0)
next_y_pos += Form1.maxY;
else
next_y_pos -= Form1.maxY;
}
else if (next_x_pos > Form1.maxX)
{
next_x_pos = -Form1.maxX;
if (next_y_pos < 0)
next_y_pos += Form1.maxY;
else
next_y_pos -= Form1.maxY;
}
if (next_y_pos < -Form1.maxY)
{
next_y_pos = Form1.maxY;
if (next_x_pos < 0)
next_x_pos += Form1.maxX;
else
next_x_pos -= Form1.maxX;
}
else if (next_y_pos > Form1.maxY)
{
next_y_pos = -Form1.maxY;
if (next_x_pos < 0)
next_x_pos += Form1.maxX;
else
next_x_pos -= Form1.maxX;
}
PointF next_pos = new PointF(next_x_pos, next_y_pos);
return next_pos;
}
the collidesWith is a function of the asteroid class and is used elsewhere to detect if a bullet/ship has hit into an asteroid. I’m not sure whether my next position function is correct though as if the asteroid does wrap around the X/Y value seems to get stuck at the maxX/maxY value?
Consider the following: at any moment you have the position of the asteroid and the ship and their velocities. Then, at a time t (intervals of 150ms), the position of asteroid will be
and the position of the ship will be
So you can just iterate through t (0 to 100) and check if at any time the asteroid and the ship are within some distance (e.g. the sum of their radii) of each other – which means a collision.
p.s. the distance is given by
I suggest you read more about collision detection in general as well