Moving enemy waypoints and FSM[SOLVED]

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
screwgh
Posts: 19
Joined: Tue Oct 14, 2008 12:27 am

Moving enemy waypoints and FSM[SOLVED]

Post by screwgh »

Hi,

I am just clueless at the moment I having a weird acting enemy in my game. The enemy needs to walk from waypoint A to B to C to D and back to A. This is going good when I just say that he needs to walk from A to B or B to C or C to D or D to A. But now it comes the enemy needs to make a round kind of a patrol round. So he needs to be able to make a turn this is also not a big problem from A to C or D to B but when I want him to move from B to D or C to A he just hangs at the first waypoint he is stuck.

As far is I debuged it and can see the problem it is only when the enemy had to move in the z -=3.0 direction. he does this not in the z +=3.0. I debuged it further so I can see where he is stuck and he is stuck between v.X < t.X and v.X > t.X.

I can understand that you cant understand this whole story above so here is a some code.

Code: Select all

void MoveEnemy()
{

	v = enemy->getPosition();

	if(v.X < t.X)
	{
		printf("v.X < t.X \n");
		v.X +=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,0,0));
	}
	else if (v.Z < t.Z)
	{
		printf("v.Z < t.Z \n");
		v.Z +=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,-90,0));
	}
	else if(v.X > t.X)
	{
		printf("v.X > t.X \n");
		v.X -=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,180,0));
	}
	else if(v.Z > t.Z)
	{
		printf("v.Z > t.Z \n");
		v.Z -=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,90,0));
	}

		
}
Maybe someone has a tought about this whole moving story :) Thanks in advance.
Last edited by screwgh on Wed Nov 12, 2008 2:43 pm, edited 1 time in total.
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

The problem is that your dude never actually gets to A. At some point, he will (most definitely) be a little less than 3.0 away, so when you move him he will actually go past his target. Then, the next iteration he will be on the other side and try to go back, but once again overshoot the target. This is a handy-dandy function I use for 2d directional movement, should be easily adapted to your purposes:

Code: Select all

core::vector2df moveToTarget( core::vector2df pos , core::vector2df & tpos , f32 spd , f32 elapsed )
	{
		f32 ym = fabs( sin( atan( ( tpos.Y - pos.Y ) / ( tpos.X - pos.X ) ) ) ) ;
		f32 xm = fabs( cos( atan( ( tpos.Y - pos.Y ) / ( tpos.X - pos.X ) ) ) ) ;

		if ( pos.X < ( tpos.X - ( elapsed * spd * xm ) ) )
		{
			pos.X += ( elapsed * spd * xm ) ; 
		}
		else if ( pos.X < tpos.X ) 
		{
			pos.X += ( tpos.X - pos.X ) ; 
		}
		if ( pos.X > ( tpos.X + ( elapsed * spd * xm ) ) ) 
		{
			pos.X -= ( elapsed * spd * xm ) ;
		}
		else if ( pos.X > tpos.X ) 
		{
			pos.X -= ( pos.X - tpos.X ) ; 
		}

		if ( pos.Y < ( tpos.Y - ( elapsed * spd * ym ) ) ) 
		{
			pos.Y += ( elapsed * spd * ym ) ; 
		}
		else if ( pos.Y < tpos.Y ) 
		{
			pos.Y += ( tpos.Y - pos.Y ) ; 
		}
		if ( pos.Y > ( tpos.Y + ( elapsed * spd * ym ) ) ) 
		{
			pos.Y -= ( elapsed * spd * ym ) ;
		}
		else if ( pos.Y > tpos.Y ) 
		{
			pos.Y -= ( pos.Y - tpos.Y ) ;
		}

		return pos ;
	}
screwgh
Posts: 19
Joined: Tue Oct 14, 2008 12:27 am

Thanks

Post by screwgh »

Hi,

Thanks for the reply but I already figured that out that is why the others are working. I have a function that looks wich waypoint the enemy is the closest to and then set a new target. Beside this I have a function that let do make him a round so when at A go to D etc.. I know when the enemy is arrived with collision detection I used the coordinates from the waypoint so if the enemy is in a range of the size lets say 10 he looks where he is and set his new waypoint. I almost got a round but when he needs to do z-=3; he is stuck.

Here is some more info about the coordinates where he is stuck maybe that helps
x7.37
y-313.38
z1168
x10.37
y-313.38
z1168
x7.37
y-313.38
z1168
x10.37
y-313.38
z1168
This is some output that I use for debuging. So only the x as changes. But he has arrived at his point he also sees that but he just wont go z-=3;

Here some waypoint code

Code: Select all

//Creating waypoints for enemy to walk
vector3df wayA = vector3df(10, -313.869, 10);
vector3df wayB = vector3df(10, -313.869, 1177.71);
vector3df wayC = vector3df(-2437.58, -313.869, 1177.71);
vector3df wayD = vector3df(-2437.58, -313.869, 10);
I am clueless at the moment. Thanks for any further help.


Edit-------

Hi I have some new progress in my program, the enemy moves good now from all the points to all the points he is able to walk a guard round. This is the code after all

Code: Select all

//Move enemy from waypoint to waypoint
void MoveEnemy()
{

	v = enemy->getPosition();

	if(v.X < t.X)
	{
		printf("v.X < t.X \n");
		v.X +=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,0,0));
	}
	if(v.Z < t.Z)
	{
		printf("v.Z < t.Z \n");
		v.Z +=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,-90,0));
	}
	if(v.X > t.X)
	{
		printf("v.X > t.X \n");
		v.X -=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,180,0));
	}
	if(v.Z > t.Z)
	{
		printf("v.Z > t.Z \n");
		v.Z -=3.0;
		enemy->setPosition(v);
		enemy->setRotation(vector3df(0,90,0));
	}
	
	
}
The only thing I changed are the else if statements to if statements. This inly get the problem that the enemy just walk around without his head facing the right way so now I have a new chalenge but that worked before so it is able to get that too.

Ill keep updated if it works all the way. Thanks for the help


Edit---------

So everything solved just a minute later I already got it right to let the enemy face the right way.

So solved thanks for all the help
Post Reply