# Thread: ai for enemy ships in asteroids

1. ## 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?

2. 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.

3. 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.

4. 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?

5. 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.

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. 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).

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).