AI : how to create a run away behavior?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
blackMasoon
Posts: 149
Joined: Wed Sep 09, 2009 4:57 pm
Contact:

AI : how to create a run away behavior?

Post by blackMasoon »

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??
Brkopac
Posts: 88
Joined: Fri Sep 19, 2008 2:36 am

Post by Brkopac »

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;
		}
	}
Image - The glory days.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

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

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
	}
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

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).
Image Image Image
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

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.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

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.
Image Image Image
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

or zig-zag + jump if you are clever enough
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

Post by stefbuet »

What about A* ?
You get a random point which is more far from the ennemy than the current player position, then you calculate the path to follow with the A* including the ennemy as an obstacle ?
Danny Gilbert
Posts: 54
Joined: Thu Oct 25, 2007 1:38 pm
Location: Montreal, Canada

Read book named: AI by Example from Mat Buckland

Post by Danny Gilbert »

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

:)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

OpenSteer is pretty good for this, too... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

AI in my game is still going to be a series of raycasts or sensors :)
Post Reply