Extremly basic AI
Hey, I have been doing a few tutorials using SDL, I am quite comfortable using it, collision detection and what not.
I now want to be able to create basic AI for a platform game I am working on.
I noticed in a previous post that someone asked for help with AI similar to that of Super Mario Bro's, thats exactly what I am looking for, only your replies to said post were not helpful to me at all.
Also I am wondering about path finding, e.g. once enemy AI is in place, how do I get the enemies to sense the player is near and then come towards him/her?
Any/all help will be much appreciated.
Thank you.
Well, you're question is asking a little bit more than that of the AI from Mario Bros. Mario Bros. AI didn't have any path finding, and due to the nature of a 2D platformer, the enemies only had to walk left, or walk right.
First things first, test if the player is close to the enemy, or vice versa. You can do this a few ways, the simplest of which is a distance check (or square distance check to be more optimal). You can either have the player do a distance check with all of the enemies every frame, or have all of the enemies do a distance check with the player every frame that the enemy is alive. If you have no sort of partitioning system then I might suggest having the enemies do the distance check..since they will be spawned in and taken out faster.
So typically an enemy (or agent, in the AI world) is going to have a few states associated with it. Idle, being the basic state, is where the agent just sits there and does nothing. When you spawn your enemies in you'll want them to start in idle state and then begin checking distance to the player. When the distance is less than or equal to the enemies viewing range, then you'll pop them out of idle and into "attack state" or someting like that. You can also do a simple test along the X axis to see if the enemy should go right or left.
Now, like I mentioned before, Mario Bros. enemies didn't really do pathfinding. They checked where the player was, and when the player was close they just moved in that direction (right or left). This makes it easy on you, as now you just need to give them some cool behaviors for their "attack state".
Mario Bros. enemies were very basic. You had two types of Goombas, those that would walk towards you without regard for edges (and thus fall into pits easily) and those that would walk towards you but turn around at an edge. Assuming your game is 2D you would, hopefully, have some sort of tile system. So you should be able to use tile data to see if they are going to walk off an edge or not. The you had the Koopas (pelican turtle things). The Koopas' AI was just like the Goombas' AI except sometimes they jumped too.
Getting Mario Bros. AI isn't too difficult. If you have a more specific question then please ask, but I would say your best bet right now is to use basic distance checks (or square distance checks for optimality) and then flip your agents' state depending on that distance.
First things first, test if the player is close to the enemy, or vice versa. You can do this a few ways, the simplest of which is a distance check (or square distance check to be more optimal). You can either have the player do a distance check with all of the enemies every frame, or have all of the enemies do a distance check with the player every frame that the enemy is alive. If you have no sort of partitioning system then I might suggest having the enemies do the distance check..since they will be spawned in and taken out faster.
So typically an enemy (or agent, in the AI world) is going to have a few states associated with it. Idle, being the basic state, is where the agent just sits there and does nothing. When you spawn your enemies in you'll want them to start in idle state and then begin checking distance to the player. When the distance is less than or equal to the enemies viewing range, then you'll pop them out of idle and into "attack state" or someting like that. You can also do a simple test along the X axis to see if the enemy should go right or left.
if (player.position.x < enemy.position.x) move enemy to the left;else move enemy to the right;
Now, like I mentioned before, Mario Bros. enemies didn't really do pathfinding. They checked where the player was, and when the player was close they just moved in that direction (right or left). This makes it easy on you, as now you just need to give them some cool behaviors for their "attack state".
Mario Bros. enemies were very basic. You had two types of Goombas, those that would walk towards you without regard for edges (and thus fall into pits easily) and those that would walk towards you but turn around at an edge. Assuming your game is 2D you would, hopefully, have some sort of tile system. So you should be able to use tile data to see if they are going to walk off an edge or not. The you had the Koopas (pelican turtle things). The Koopas' AI was just like the Goombas' AI except sometimes they jumped too.
Getting Mario Bros. AI isn't too difficult. If you have a more specific question then please ask, but I would say your best bet right now is to use basic distance checks (or square distance checks for optimality) and then flip your agents' state depending on that distance.
Thanks a lot for that, I got it to work.
For some reason I cannot work out how to get the enemy to do a "static" pose until the player is of a viewable (in reality) distance.
I've tried things like
But the enemy always just goes to towards the player until their x coordinates
are the same.
[Edited by - Artificial_Institute on June 16, 2009 1:38:43 AM]
For some reason I cannot work out how to get the enemy to do a "static" pose until the player is of a viewable (in reality) distance.
I've tried things like
if(enemy.x == player.x - 100){ enemy.x -= ENEMY_WIDTH / 10; // ENEMY_WIDTH = 20 (20/20 square), this is the // speed at which the enemy is traveling}else if(enemy.x == player.x + 100){ enemy.x += ENEMY_WIDTH / 10;}
But the enemy always just goes to towards the player until their x coordinates
are the same.
[Edited by - Artificial_Institute on June 16, 2009 1:38:43 AM]
Try something like this.
if (enemy.state == STATE_IDLE){ // Wait for player to get close if (abs(enemy.x - player.x) < 100) { // Attack! enemy.state = STATE_ATTACK; }}if (enemy.state == STATE_ATTACK){ // If we aren't right on top of the player if (abs(enemy.x - player.x) > ENEMY_WIDTH) { // Move towards player if (enemy.x < player.x) { enemy.x += ENEMY_WIDTH / 10; } else { enemy.x -= ENEMY_WIDTH / 10; } }}
Thanks for that, but I am still not sure on where exactly to implement that/those function(s). Would I put them with the player moving moving functions, e.g. each step the player takes could/will change the behaviour of the enemy? Or is that technically not "AI"? Would it be better to let the enemy AI just do it's thing until the player gets close enough? P.S thanks for the "abs" parameter I never even knew it existed, looked it up on cplusplus.com, will come in handy.
The enemy class should have a Update Method. where things happen, such as animation update, movement update, and thinking.
there then should be at least 3 update methods.
those lines will be inside AIUpdate.
so every game main loop you call for each agent the Update() method(actualy you should call only for the AIs that are in the screen).
there then should be at least 3 update methods.
public void Update(){ AIUpdate(); AnimationUpdate(); PositionUpdate();}
those lines will be inside AIUpdate.
so every game main loop you call for each agent the Update() method(actualy you should call only for the AIs that are in the screen).
Sorry for the not so pepper english!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement