zombie ai

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
tomtetlaw
Posts: 110
Joined: Sun Jul 05, 2009 9:57 am

zombie ai

Post by tomtetlaw »

When I run my code, my zombie just sits there and does nothing, I don't know why. Here is my code:

Code: Select all

void C_BaseZombie::Update(ICameraSceneNode* pCamera, u32 uElapsedTime)
{
	if(pNode) 
	{ 
		vPosition = pNode->getPosition();
		vector3df vec = vPosition;

		if(vec.getDistanceFrom(pCamera->getPosition()) < 4)
			nCurrentState = following;
		if(vec.getDistanceFrom(pCamera->getPosition()) < 1)
			nCurrentState = attacking;
		else
			nCurrentState = idle;

		fDistanceFromPlayer = vec.getDistanceFrom(pCamera->getPosition());
	}

	int nOldState = nCurrentState;
	if(nOldState != nCurrentState)
		ProcessState(nCurrentState, pCamera, uElapsedTime);
}

void C_BaseZombie::ProcessState(int eCurrentState, ICameraSceneNode* pCamera, u32 uElapsedTime)
{
	switch(eCurrentState)
	{
	case following:
		{
			vector3df vec = pCamera->getPosition() - vPosition;
			f32 fDistance = uElapsedTime * fMov_speed;
			vec = vec.normalize() * fDistance; 
			pNode->setPosition(pNode->getPosition() + vec);

			//play walking animation
			//play walking sound
		}
	case idle:
		{
			//play idle animation
			//play idle sound
		}
	case attacking:
		{
			//play attacking animation
			//play attacking sound
			//damage the player
		}
	}
}
I guess what I will also have to change

Code: Select all

if(vec.getDistanceFrom(pCamera->getPosition()) < 1)
because it's the same method.

i wanna know why my zombie isnt moving

Any help would be appreciated.
#1 Most Common Error:
Reality Error: Object behind keyboard cannot program.
Malgodur
Posts: 195
Joined: Sun Mar 15, 2009 8:22 pm

Post by Malgodur »

Use debuger...
and change distances to higher, 4 isnt far enough to let zombie spot the camera. Maybe 400/100 should be ok
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

2 things about this code:

Code: Select all

void C_BaseZombie::Update(ICameraSceneNode* pCamera, u32 uElapsedTime)
{
   if(pNode)
   {
      vPosition = pNode->getPosition();
      vector3df vec = vPosition;

      if(vec.getDistanceFrom(pCamera->getPosition()) < 4)
         nCurrentState = following;
      if(vec.getDistanceFrom(pCamera->getPosition()) < 1)
         nCurrentState = attacking;
      else
         nCurrentState = idle;

      fDistanceFromPlayer = vec.getDistanceFrom(pCamera->getPosition());
   }

   int nOldState = nCurrentState;
   if(nOldState != nCurrentState)
      ProcessState(nCurrentState, pCamera, uElapsedTime);
}
1st - you calculate the distance 3 times, it would be faster if you do it this way:

Code: Select all

   if(pNode)
   {
      vPosition = pNode->getPosition();
      vector3df vec = vPosition; // why you're dublicating the position here ???
      fDistanceFromPlayer = vec.getDistanceFrom(pCamera->getPosition());

      if(fDistanceFromPlayer < 4)
         nCurrentState = following;
      if(fDistanceFromPlayer < 1)
         nCurrentState = attacking;
      else
         nCurrentState = idle;

   }
and the error you have is because of this (probably):

Code: Select all


   int nOldState = nCurrentState;
   if(nOldState != nCurrentState)
      ProcessState(nCurrentState, pCamera, uElapsedTime);
you set the old state equal to the current state and then you check if they are different, but they never will be in this case !!! :lol:
and there is also a problem with the declaration of nOldState (it must be static) !!! :shock:
here the working code:

Code: Select all

void C_BaseZombie::Update(ICameraSceneNode* pCamera, u32 uElapsedTime)
{
   static int nOldState = idle;
   if(pNode)
   {
      fDistanceFromPlayer = pNode->getPosition()->getDistanceFrom(pCamera->getPosition());

      if(fDistanceFromPlayer < 4)
         nCurrentState = following;
      if(fDistanceFromPlayer < 1)
         nCurrentState = attacking;
      else
         nCurrentState = idle;

   }

   if(nOldState != nCurrentState)
   {
      ProcessState(nCurrentState, pCamera, uElapsedTime);
      nOldState = nCurrentState;
   }
} 
Last edited by Acki on Sat Sep 19, 2009 3:07 pm, edited 1 time in total.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

yeah ,lol the same mistakes found in deltatime codes
where they write :

Code: Select all

then=now
and then check for elapsed seconds which ofcourse will give a zero :P
Post Reply