# Plane Game A.I

15 replies to this topic

### #1 devilgod18

devilgod18

GMC Member

• New Member
• 16 posts
• Version:GM8.1

Posted 30 June 2012 - 05:55 PM

Guys Hi I am Sujit:) I need a little help. I am making a top down plane shooter game. so i want add a line of sight means a circle around the plane so when a plane comes near it changes its direction so they dont collide.
• 0

### #2 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 30 June 2012 - 06:28 PM

does this suffice?
in step event of ai plane:
if square(x-player.x)+square(y-player.y)<=sight{
var temp;
temp=direction-point_direction(x,y,player.x,player.y);
if temp<-180
temp+=360;
if temp>180
temp-=360;
direction+=5*sign(temp);
}
• 0

### #3 devilgod18

devilgod18

GMC Member

• New Member
• 16 posts
• Version:GM8.1

Posted 01 July 2012 - 03:15 AM

can yu explain what this code will do in simple language??
• 0

### #4 Pancratius

Pancratius

GMC Member

• GMC:Member
• 55 posts
• Version:GM8

Posted 01 July 2012 - 08:36 AM

I am not sure how to code this, but this is the method I would use:

1) Use a collision_circle to check within a certain radius of the plane for other planes.
2) When it finds another plane, find out what direction it is moving.
3) Turn away from the other planes direction of movement.

If it finds multiple planes, then use the same method but find an average of their movement directions, It should still work in moving them away from each other. This method is a really simplified version of the clear path algorithm. Admittedly Clear Path is used for crowds, but if you give the planes enough line of sight they should turn away successfully.

Edited by Pancratius, 01 July 2012 - 08:36 AM.

### #5 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 01 July 2012 - 08:42 AM

sorry, change square to sqr in my code.
how it works is prettey easy:
if sqr(x-player.x)+sqr(y-player.y)<=sight{ // checks if the air plane is near the player, within a radius of the square root of sight
var temp; // creates a temporary variable
temp=direction-point_direction(x,y,player.x,player. y); // sets the variable to the difference between the direction he is going and the direction between him and the player
if temp<-180 // next 4 lines adapts the angle to a value between 180 and -180
temp+=360;
if temp>180
temp-=360;
direction+=5*sign(temp); // moves away from the player
}
• 0

### #6 Pancratius

Pancratius

GMC Member

• GMC:Member
• 55 posts
• Version:GM8

Posted 01 July 2012 - 08:55 AM

Sorry jo-thijs, but your code was really hard to read, and slightly wrong I thought, here is my version of it.

```
// player in the code is the object of the player you are controlling.

// The if statement checks if the air plane is near the player. (Ie within a set Distance.)
// I have used 200 in this case, but it is an arbitrary value which can be changed if you need to.

if (point_distance(x, y, player.x, player.y) < 200)
{
var temp;
// creates a temporary variable

temp=direction-point_direction(x,y,player.x,player. y);
// sets the variable to the difference between the direction he is going and the direction between him and the player

// the next 2 lines adapts the angle to a value between 180 and -180
if (temp<-180) {temp+=360}
if (temp>180) { temp-=360 }

direction += 5*sign(temp)
// moves away from the player. 5 is another Arbitrary value, which affects the speed of the turn away from the player.
// However this may look stupid at slow speeds, so feel free to include another variable which changes dependent on the speed of the plane.
}```

Also, I am unsure on how to make it the turnMultiplier, turn the plane realistically, but here is an equation which may get you started:

`turnMultiplier = smallValueHere * ( speed )`

If you make the small value less than 1, it should get the plane to turn faster at higher speeds, and slower and lower speeds. No idea if this is accurate but its a start.

### #7 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 01 July 2012 - 10:36 AM

it's the same as my code, only you used the point_distance code which will take an extra square root.
• 0

### #8 devilgod18

devilgod18

GMC Member

• New Member
• 16 posts
• Version:GM8.1

Posted 01 July 2012 - 01:10 PM

Thanks Jo and Pan...Pan i used ur code..because i am not that much advance game dev...and jo code was giving errors coz of tge variabls problem i cant correct...but after pan code tge plane dont turn when it come in the area of 200pix but when it come total close i.e above the main player the enemy plane rotate her and there..so i think its slightly working can yu help now...both of yu guys
• 0

### #9 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 01 July 2012 - 03:10 PM

where is your planes origin?
• 0

### #10 devilgod18

devilgod18

GMC Member

• New Member
• 16 posts
• Version:GM8.1

Posted 01 July 2012 - 04:02 PM

planes origin??? let me explain you..i have put the code in enemy plane that it will follow the main plane and fire bullets ...so when these enemy plane came over the main plane they cant go in fornt of the main plane like in real life...so i need a.i for that so enemy also follow the main plane but the if the main plane slow down it doesnt stop on the main plane but go away and attack again...and when a enmy plane comes too close to the main plane it changes the direction left or right...
• 0

### #11 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 01 July 2012 - 06:16 PM

yes, but wat is the sprite origin?
it will affect the position of the area a bit.
for example: plane1 has a width of 96 and his origin on (0;0) and plane2 has a width of 96 and his origin on (96;0),
it will screw up the distance checking so that from 1 side, the plane has to fly over the other before changing direction.
• 0

### #12 Pancratius

Pancratius

GMC Member

• GMC:Member
• 55 posts
• Version:GM8

Posted 01 July 2012 - 08:10 PM

it's the same as my code, only you used the point_distance code which will take an extra square root.

I also added a few extra comments for fool proofing.

Thanks Jo and Pan...Pan i used ur code..because i am not that much advance game dev...and jo code was giving errors coz of tge variabls problem i cant correct...but after pan code tge plane dont turn when it come in the area of 200pix but when it come total close i.e above the main player the enemy plane rotate her and there..so i think its slightly working can yu help now...both of yu guys

Devil we may find it easier to help you fix the problem if you uploaded an example of your work for us to look at. Although to help out, you may want to go to each of your plane sprites. and click the center button at the bottom left of the sprite properties panel. Then if the problem continues to persist, you could change the 200 in the code to a larger number. That will increase the distance that the enemy plane checks, before turning away.

Edited by Pancratius, 01 July 2012 - 08:11 PM.

### #13 devilgod18

devilgod18

GMC Member

• New Member
• 16 posts
• Version:GM8.1

Posted 02 July 2012 - 05:08 AM

http://db.tt/tNbwtgCd

here is the dropbox link please check it...
• 0

### #14 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 02 July 2012 - 12:05 PM

3 things:
1) you have put a > in your, what should be a <, else you activate the code when your plane is far from their side
2) you set the direction variable automatic to image_angle so that it doesn't matter what we do with direction, so replace direction in our code by image_angle
3) your other code to point to trhe player is still being excuted when near, so surround that part with if point_direction(...)>200
• 0

### #15 Pancratius

Pancratius

GMC Member

• GMC:Member
• 55 posts
• Version:GM8

Posted 02 July 2012 - 06:21 PM

I fixed the AI, and I tried to add a little extra feel free to remove anything you don't like.
Here's the code:

```motion_set(image_angle,movespeed);

turnMultiplier = 19/(movespeed+1)

if(turnMultiplier > 5)
{
turnMultiplier = 5
}

if object_exists(obj_plane)
{
dirDiff = point_direction(x, y, obj_plane.x, obj_plane.y) - direction
distance = point_distance(x, y, obj_plane.x, obj_plane.y)

while(dirDiff<=-180) {dirDiff+=360}
while(dirDiff>180) {dirDiff-=360}

direction += turnMultiplier*sign(dirDiff)
image_angle = direction

if(movespeed < 8 and distance > 200)
{
movespeed+=0.2
}
else if(movespeed > (8-6*(distance/400)) )
{
movespeed-=0.2
}

if(distance < 200)
{
if(abs(dirDiff) < 10)
{
shooting = true
}
else
{
shooting = false
}
}
else
{
shooting = false
}

if can_shoot and shooting  //then if it can shoot
{
// shoot billet
bullet = instance_create(x,y,obj_bullet_enemy);

//can_shoot = false
can_shoot=false;
bullet.direction=image_angle;
bullet.image_angle=image_angle;

//reload time
alarm[0] = room_speed/3;
}
}```

To work it, Place all of that code into the step event of the enemy plane. Then go to its create event and add this line:
`shooting = false`

Ok, now let me explain the few changes I made and how they work.

The first thing I did was to modify it so that the enemy planes are constantly trying to turn towards you, regardless of how far away they are.
This means that when they are flying they will always find the direction towards you and turn towards it.

I then created the new variable to check whether or not they were shooting at you. Then I used this in conjunction with the direction towards the player, by adding a range of angles for which the plane will shoot. Eg if the direction of flight, and the direction towards the player differs by less than 10 degrees the plane will begin shooting. This is the part of the code I am referring to:

```if(distance < 200)
{
if(abs(dirDiff) < 10)
{
shooting = true
}
else
{
shooting = false
}
}
else
{
shooting = false
}

if can_shoot and shooting  //then if it can shoot
{
// shoot billet
bullet = instance_create(x,y,obj_bullet_enemy);

//can_shoot = false
can_shoot=false;
bullet.direction=image_angle;
bullet.image_angle=image_angle;

//reload time
alarm[0] = room_speed/3;
}```

Next I figured that planes don't move at constant speed all the time, and an AI might want to catch up with the player if he is farther away. So I added a small section that checks how far away the player is and adjusts speed accordingly. Also, the players maximum speed is 8 so I limited the speed of the enemy plane to match. Finally, I realised that the planes don't stop so after a little playing around with maths (just tried different numbers), I found that the minimum speed should be around 2, so (8-6*(distance/400))) seemed to give us this minimum speed when the player got too close. This is the part I am talking about:

```if(movespeed < 8 and distance > 200)
{
movespeed+=0.2
}
else if(movespeed > (8-6*(distance/400)) )
{
movespeed-=0.2
}```

Finally, Planes can turn at different speeds based on how fast they are flying, the Slower the movement speed of the plane the faster it can turn, So I added this line of code to change the speed at which the plane turns based on how fast it is traveling:

```turnMultiplier = 16/(movespeed)
```

However planes, can't turn infinitely fast, so I added this if statement to limit it:
```
if(turnMultiplier > 5)
{
turnMultiplier = 5
}```

This code should now provide a good starting point for the AI in the game . (At least I hope it does xD)

The next few changes, should be to add an anti-collision detection system, so that enemy planes don't fly inside each other. The algorithm I would recommend attempting would be the clear path algorithm, because its very powerful, and if programmed efficiently will prove very useful .

Edited by Pancratius, 02 July 2012 - 06:28 PM.

### #16 devilgod18

devilgod18

GMC Member

• New Member
• 16 posts
• Version:GM8.1

Posted 03 July 2012 - 06:54 AM

can you send the updated file Pan...i want to see it Thanks bro...
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users