Advertisement

AI Issues

Started by January 14, 2006 03:16 AM
3 comments, last by smittix 18 years, 10 months ago
I have eight characters all randomly placed in a scene, and I would like for each of them to find the closest character to themselves and move toward it, and stop if it gets too close. The characters seem to find the closest character to them and move toward it fine, the problem I am having is that some of them stop pretty far away from each other and some stop very close to each other. I am really not sure why this is, the code looks like it should work fine to me.

void RunAI( float DeltaTime )
{
	for(int i = 0; i < 8; i++)
	{
		float ClosestDistance = 999999;
		D3DXVECTOR2 TargetPosition(0,0);

		//Find closest enemy
		for(int j = 0; j < 8; j++)
		{
			//We do not need to test the length of an enemy to itself
			if( i != j )
			{
				//Find distance to enemy in question
				float TempDistance = Vec2Length( &(D3DXVECTOR2( Enemies.Position.x, Enemies.Position.z )), &(D3DXVECTOR2( Enemies[j].Position.x, Enemies[j].Position.z )) );

				//If the distance found is closer than the closest distance, then its the new closest distance
				if( TempDistance < ClosestDistance )
				{
					ClosestDistance = TempDistance;
					TargetPosition = D3DXVECTOR2( Enemies[j].Position.x, Enemies[j].Position.z );
				}
			}
		}
			
		if( ClosestDistance > 3.0f )
		{
			//Find vector pointing to closest enemy
			D3DXVECTOR2 LookVector = TargetPosition - (D3DXVECTOR2(Enemies.Position.x, Enemies.Position.z));
			D3DXVec2Normalize( &LookVector, &LookVector );

			//Rotate toward closest enemy
			Enemies.Rotation = GetRotation( TargetPosition, D3DXVECTOR2(Enemies.Position.x, Enemies.Position.z) );

			//Move toward closest enemy
			Enemies.Position.x += LookVector.x * 5.0f * DeltaTime;
			Enemies.Position.z += LookVector.y * 5.0f * DeltaTime;
		}
	}
}

float Vec2Length( D3DXVECTOR2 *Point1, D3DXVECTOR2 *Point2 )
{
	float Point1Dist = D3DXVec2Length( Point1 );
	float Point2Dist = D3DXVec2Length( Point2 );

	if( Point1Dist > Point2Dist )
		return Point1Dist - Point2Dist;
	else
		return Point2Dist - Point1Dist;
}

float GetRotation( D3DXVECTOR2 Target, D3DXVECTOR2 Position )
{
	return atan2(Target.y - Position.y , Target.x - Position.x);
}


Thanks in advance for any help with this. -Chris
Your code works if the target is static. However, if the target is moving towards the character as well, the target movement is not predicted by your code. You need to take that movement into account as well.
Advertisement
Would the target movement need to be taken into consideration even if the RunAI function is being called 60 times a second? I was thinking that because it is updated so frequently, the target movement would not need to be taken into consideration. It seems to me that if used 60 times a second, it should work fine, is this not correct?

-Chris
You would experience errors of 0.083 units with 60 updates per second. That's indeed quite small.

What numeric distances do they stop at, usually? And what does the debugger say about it?
Well they are supposed to stop if they get within 3 units of each other, but the range is quite large. As of now I do not have any actual numerical output displaying the distances between them, but as a rough estimate they appear to stop anywhere between 3 and 15 units apart.

-Chris

This topic is closed to new replies.

Advertisement