Enemy AI Question

Talk about making games.

Enemy AI Question

Postby foleyjo » Tue Jul 27, 2010 12:32 pm

Hi
Just after a bit of help with enemy AI

Its a 2d space shooter in an astoroid field

Ive got the enemy ships to fly behind the player but I want enemy ships to be able to avoid the astoroids (and other ships).
It doesn't have to be 100% accurate everytime. Part of the fun will be causing enemies chasing you to crash

Any hints on the best way of doing this?

I'm guessing a sensor system where it checks to see if theres a collision in front above and below and then changes direction accordingly.
I'm not sure how to set this up though
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Enemy AI Question

Postby DilloDude » Tue Jul 27, 2010 1:23 pm

Here's a really awesome technique I'm working with. For your enemy, calculate all the directions to move, figure out the priority of moving in that direction, and add them all together. Use the result to calculate your direction.
To begin with, we create two variables:
Code: Select all
double aimXV;
double aimYV;

Now we add the direction towards the player, using our exceptionally useful xdist and ydist functions:
Code: Select all
double ang = direction(x, y, player.x, player.y); // calculate the direction towards the player
aimXV += xdist(ang, 5);
aimYV += ydist(ang, 5);

In this case we used 5 as our distance. This will act as the 'priority' of the movement. A bigger number means we're more likely to move in that direction. If you had more than one player to chase, you'd have to do this in a loop, and calculate the priority based on how close the player is, and maybe other factors such as the player's health.
Now, for avoiding stuff. Let's make a sensor actor which covers the area we want to check. Then we'll use getAllActorsInCollision to check what actors are touching the sensor, loop through them and check their stats, and decide whether we want to move towards them or away from them, and how important it is. Then use xdist and ydist to increase aimXV and aimYV by the appropriate amount - an asteroid that's further away is not so important to move away from as one that is close. Also a larger or faster moving asteroid may be more important to run from.
Once we've added in all our factors, we check the size of our variables:
Code: Select all
if (distance(0, 0, aimXV, aimYV) > 1)

You can compare it with a larger or smaller value depending on how precise you want to be. If true, then we need to move. So we calculate the direction to move in:
Code: Select all
double aimAng = direction(0, 0, aimXV, aimYV);
That's the direction we need to move in. So we use whatever system we want to adjust our velocity to that direction, and there we are! You could also store the distance we checked, and use it again to adjust our velocity, if you wanted to have different speeds.
Then add an else, so in the event we don't have to move, we can either slow to a stop, or just move randomly, or whatever.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Re: Enemy AI Question

Postby foleyjo » Tue Jul 27, 2010 1:44 pm

Thank you. That gives me something to work with.

Much appreciated
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Enemy AI Question

Postby foleyjo » Tue Jul 27, 2010 2:28 pm

Terribly sorry but that is slightly too advanced for me I'm missing a few bits of knowledge :oops:

Is there a good guide for creating a sensor anywhere and how to do a loop on everything in it?

Also at the end

"we use whatever system we want to adjust our velocity to that direction"

I have been using xvelocity = and yvelocity
How would I incorperate the result.

Forgive my ignorance I havent done any programming for about 10 years and even then I wasn't that good at it
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Enemy AI Question

Postby foleyjo » Tue Jul 27, 2010 2:43 pm

Scrap the last part of the question I used

angle =aimAng;
directional_velocity = plyrspeed;
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Enemy AI Question

Postby krenisis » Tue Jul 27, 2010 11:20 pm

Ok how are you doing? To make the enemy act according to your movement is the question. Download my 3D pong game and use that code its a bit more easier here
viewtopic.php?f=6&t=8468

Study those and remember to check out my tutorial database here

viewtopic.php?f=4&t=8680&p=60889&hilit=tutorial+database#p60889

Please use all this info to help make your awesome game.
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

Re: Enemy AI Question

Postby DilloDude » Wed Jul 28, 2010 2:34 am

Okay, I'll make you a simple demo.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Re: Enemy AI Question

Postby DilloDude » Wed Jul 28, 2010 4:44 am

Try this out.
AI demo.zip
(35.06 KiB) Downloaded 70 times

At the start some solid objects (white dots) are randomly created, as well as some ships (reg, green and blue dots).
The red ships chase the green ships, the green ships chase the blue ships, and the blue ships chase the red ships.
Have a look at it, play around with it and try out different values and see how that affects it. If you have any questions about it, let me know.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Re: Enemy AI Question

Postby foleyjo » Wed Jul 28, 2010 4:15 pm

Again many thanks.

I have been playing around with the code DilloDude and am understanding it a lot more now.

I had a bit of trouble at first because I forgot to check the variables and the global code.

One thing that surprised me is that I forgot that the FOR TO DO loops exsisted.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Enemy AI Question

Postby foleyjo » Sun Sep 26, 2010 5:36 pm

Sorry for bringing up an old topic but I struggled with this and moved on to something else hoping that my skills would improve enough for me to be able to do it.

Sadly they didn't.
I understand in theory what is happening but I can't seem to put it into practice.

Could somebody please look through my code and put me on the right path :oops:

The actor doing the following is BortElite and he has a Bort Sensor

He is to follow Player but not actually touch him

He is supposed to avoid solid objects
Attachments
AIHELP.rar
(1.02 MiB) Downloaded 76 times
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Enemy AI Question

Postby foleyjo » Wed Sep 29, 2010 10:00 am

Hello again.

Not much response last time but I did manage to find the 2 big problems in the code

1 - I forgot the angle line so it was doing the calculations but not moving anything


2- After researching it I found out that I needed to use the XSCREEN YSCREEN's as I was using the view as a parent.


So now I have it kind of doing what I want but with 2 problems.
(again with the 1 2's)
1- The enemy ship (Bortelite) follows the player but keeps bouncing about instead of moving in a straight line. Will I need to make a section to say - If behind the player angle = player angle and speed = player speed, or can I change a value in what I have already that will do this.

2- The enemy ship does avoid other objects but doesn't do it very well. So he kind of moves away slightly but then just goes straight through. Is this to do with the size of my sensor or again have I entered values wrong.




on a different unrelated not I just found oout Spotify has the medal of honor soundtrack. Amazing
Attachments
data.zip
(1.02 MiB) Downloaded 72 times
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Enemy AI Question

Postby DilloDude » Wed Sep 29, 2010 11:22 am

I've made a few changes which seem to fix it up.
AI - fix.zip
(1.07 MiB) Downloaded 76 times

I made the enemy ship separate types (now _Bort and _BortMine) so the ship can distinguish between other ships and mines (so the if (Cols[i].cloneindex != cloneindex) check only occurs with another ship).
I also adjusted the value for the fleet ships - the distance is now subtracted from 400 instead of 200 to allow for the fleet ships' large size. Also changed the Mine avoidance priority value.
I also fixed up a few other things - for checking the angle for the animation, you had just one &, instead of && - and I changed the code for setting the aim position behind the player - now it check the view xvelocity instead of angle, and uses an else.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Re: Enemy AI Question

Postby foleyjo » Wed Sep 29, 2010 1:57 pm

Thanks again Dillo :D
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest