Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:18:57+00:00 2026-05-31T12:18:57+00:00

I’ve been trying to make an AI for an asteroids game but have become

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:18:58+00:00Added an answer on May 31, 2026 at 12:18 pm

    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

    asteroid_x + asteroid.vx * t
    asteroid_y + asteroid.vy * t
    

    and the position of the ship will be

    ship_x + ship.vx * t
    ship_y + ship.vy * t
    

    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

    sqrt( (asteroid_x - ship_x)^2 + (astroid_y - ship_y)^2 )
    

    I suggest you read more about collision detection in general as well

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.