AI : how to create a run away behavior?
-
- Posts: 149
- Joined: Wed Sep 09, 2009 4:57 pm
- Contact:
AI : how to create a run away behavior?
Hi everyone. I was wondering how to implement this kind of behavior: the enemy starts to run away from player when the distance between them is f ex. smaller then MIN_DONT_RUN_DIST?
I have camera coordinates, obiect coordinates and obiect vector of movement. How to do it with theese data??
I have camera coordinates, obiect coordinates and obiect vector of movement. How to do it with theese data??
Something along these lines?
Code: Select all
if (isMoving)
{
core::vector3df position = runner->getAbsolutePosition();
if (position.getDistanceFrom (scaryGuy->getAbsolutePosition) > MIN_DONT_RUN_DIST)
{
isMoving = false;
return;
}
}
- The glory days.
i recommend thinking about a longterm solution that can be sized for your game later. So many times we create code only for this particular issue, when a more robust answer will save you time in the long run.
I like the state machines for doing what you say. when a guard is STATE_IDLE then he stands around and plays a few animations every once in a while. When he becomes STATE_BORED then he walks around and whistles. After seeing the player, he becomes STATE_AROUSED and goes towards the player. Once he realizes that the player is kicking his tail, he becomes STATE_RUNAWAY and leaves the area.
do a little searching on google and you will find TONS of code for this. My personal favorite is from the Game Programming Gems books and looks like this
I like the state machines for doing what you say. when a guard is STATE_IDLE then he stands around and plays a few animations every once in a while. When he becomes STATE_BORED then he walks around and whistles. After seeing the player, he becomes STATE_AROUSED and goes towards the player. Once he realizes that the player is kicking his tail, he becomes STATE_RUNAWAY and leaves the area.
do a little searching on google and you will find TONS of code for this. My personal favorite is from the Game Programming Gems books and looks like this
Code: Select all
virtual bool ProcessStateMachine(CSObjectMessage* msg)
{
BeginStateMachine
OnEnter
SetState(STATE_IDLE);
State(STATE_IDLE)
OnEnter
SetTimer(0);
Stop();
SetRotation(vector3df(0,GetRandomFloat(0,360),0));
OnUpdate
if (m_Timer++ > GetMaxIdleTimer()) SetState(STATE_WANDER);
SetAnimation("IDLE1");
OnExit
State(STATE_WANDER)
OnEnter
SetTimer(0);
OnUpdate
if (m_Timer++ > GetMaxWanderTimer()) SetState(STATE_IDLE);
SetAnimation("WALK");
SetMoveForward(true);
OnExit
SetMoveForward(false);
EndStateMachine
}
In IrrAI i have a character class which will run away from enemies and it basically says if I see an enemy then run off to a random point in the map... That's horribly basic behaviour and can result in the player running INTO the enemy which isn't always sensible! But it wouldn't be too much work to expand on that to stop them running towards the enemy (unless they had to, down a dead end for example).
learn something about dot products and the like, firstly the distance between the two needs to increase secondly the direction in which the thing is running away must be optimal (that is where dot product comes in). Also if you want the thing to take into account walls and obstacles, do 10-20 raycasts from the thing outwards in all directions and they should tell the instance of the thing where are the obstacles.
And obviously you don't want to run directly away from the enemy... it might be optimal in getting distance between the AI and the enemy but if you're running directly away from someone you're an easy shot (assuming guns are the primary weapon) because in their view you're effectively not moving, just getting very slightly smaller all the time so really you'd want to run off at like a 90 degree angle to them so that you're a moving target.
-
- Posts: 54
- Joined: Thu Oct 25, 2007 1:38 pm
- Location: Montreal, Canada
Read book named: AI by Example from Mat Buckland
Hi !
There is a beautiful (incredible) chapter about STEERING BEHAVIOR for character or any "object" that move in a video game. With VECTORIAL MATH (physics like speed, acceleration, position, force, mass, inertie, etc.), you can do activity like: FLEE, HIDE, PURSUIT, EVADE, INTERPOSE, etc.
You must read this and take the code that came with the book.
It will REALLY help you.
http://www.ai-junkie.com/books/toc_pgaibe.html
There is a beautiful (incredible) chapter about STEERING BEHAVIOR for character or any "object" that move in a video game. With VECTORIAL MATH (physics like speed, acceleration, position, force, mass, inertie, etc.), you can do activity like: FLEE, HIDE, PURSUIT, EVADE, INTERPOSE, etc.
You must read this and take the code that came with the book.
It will REALLY help you.
http://www.ai-junkie.com/books/toc_pgaibe.html
OpenSteer is pretty good for this, too...
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java