Thread: ai for enemy ships in asteroids

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    ai for enemy ships in asteroids

    I'm working on an asteroids clone and I've been trying to put in some AI ships to chase you around some of the higher levels. The best approach to this that I've come up with so far is to use the enemy's coords (x,y,th) to generate two points, one pi/4 (and 100 out) to the left of the direction the enemy is facing in, and one to the right. I then calculate the distance from these points to the coords of the player's ship and turn the enemy towards the shorter distance. The problem with this is that it produces very twitchy, unnatural motion. Does anyone have another approach to this that looks smoother / is more challenging?
    Illusion and reality become impartiality and confidence.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    First of all, visit http://aihorizon.com There are things there that I can't even begin to fathom yet, as well as simpler things which will help you. Second...for something very simple and faster than what it sounds like you are doing, compute the vector between the enemy ship and the player ship, turn the enemy ship to be aligned with this vector, and adjust its position along the vector. For more information on vectors, I would suggest starting here , there is a ton of information about vectors available.
    Away.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    if your using a velocity value to move your ships (hopefully you are) you can simply modify the vector of the enemy ships to converge on the current location of the player ship. If the velocities are in controll of movement you simply adjust the acceleration vector (determined by position of player/enemy ships). For ai difficulties you could change the update time (how often enemy ship updates its vector) and the accuracy of the new acceleration vector(hindered by the introduction of some randomness to the calculated vector).

    Heres some psuedo code

    Code:
    struct phys_movement {
         x; y; [z;]// (position of sprite using movment physics) [optional]
         vx; vy; [vz;]//(velocity added to xyz position each update)
         ax, ay, [az;]//(acceleration vector that modifies velocity)
    }
    
    // in UPDATE function
    if( getTickCount > lastTickValue * ai_difficulty ) /* how often does ai recalculate acceleration (based on difficulty) */
    {
         getPlayerPos( x, y [,z] );
         Vector accelerateVec = getVectorToPlayerFromEnemy( ex, ey [,ez], x, y [,z] );
    
         randDeviation = rand()%ai_difficulty
         
         if( DecideWhichDirectionIsLargest ) {  // find out largest part of vector
               accelerateVec.x += randDeviation; // increase for inacurate
               accelerateVec.y -= randDeviation; // decrease to compensate
          }
         /* you compensate for the additional random deviation to make sure your acceleration vector doesnt exceed the fastest you want the ships to be able to accelerate */
    
    }
    
        // update velocities of ships
        ship.vx *= accelerationVec.x;
        ship.vy *= accelerationVec.y;
        if( shipMaxVelocity > MAGNITUDE( shipVelocityVector ) ) {
          // clip the .vx, .vy values so they dont exceed maximum velocity
        }
    
         ship.x += ship.vx;
         ship.y += ship.vy;
    // etc.




    I hope that was helpfu.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Thanks for your help, I see why the motion was so jerky, I needed to base the turn amount on the direction to the player:
    Code:
    float th = atan((ship.y-enemy.y)/(ship.x-enemy.x));
    int temp = 100 - level * level;
    if (temp < 25) temp = 25;
    if (turnleft) enemy.th -= pi / temp;
    else enemy.th += pi / temp;
    also:
    Code:
    Vector accelerateVec
    I've been making all my own vector stuff (source available), where did you get that?
    Illusion and reality become impartiality and confidence.

  5. #5
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Its "Psuedo code" for a reason.

    I made it up, its to serve as an example. Replace type declrations with your own, replace variables with your own. etc. etc.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  6. #6
    This is off topic, but ichijoji on your website why don't you supply the source code to your games? I would really like to see the source for your tic-tac-toe game since I have been having difficulty with mine. Plus I really like the way you showed your board, I just need to figure out how you did it, is it the same way you do it in batch?

  7. #7
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    If you follow the links to my other site everything is complete with source, but the games on my main site are mostly old, when I lacked the wisdom to include things like that.

    I've attached the source to ttt, I got the characters for the board by editing .com files and copying them directly into my source, but I think you can get them by doing stuff like char(mid 200's).
    Illusion and reality become impartiality and confidence.

  8. #8
    Yea, I looked at someone elses code in their poker game and saw that they were displaying boxes by using loops and char(ascii value).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple space combat AI
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-06-2009, 12:54 AM
  2. chess ai contest
    By Raven Arkadon in forum Contests Board
    Replies: 7
    Last Post: 07-09-2005, 06:38 AM
  3. AI Contest Proposal
    By MadCow257 in forum Contests Board
    Replies: 4
    Last Post: 03-13-2005, 03:27 PM
  4. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM
  5. Technique of all board-like games?
    By Nutshell in forum Game Programming
    Replies: 28
    Last Post: 04-24-2002, 08:19 AM