Jump to content
  • Advertisement
Sign in to follow this  
DOrmisher

Targetting and aiming

This topic is 3472 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am currently trying to implement some simple targeting. There are 2 tanks in my scene, I need the turrets on the tanks to rotate slowly on their local Y until they are pointing at the enemy tank. It seems to be working nearly fine, when they begin targeting, they slowly spin and when the angle between the turrets Z axis and the vector from tank position to enemy position is 0, it stops. However, the problem is that only one of the tanks is pointing at the enemy, and the other is pointing away (although on the correct vector). If I change the >0 condition in the if else, to <0, it changes which tank is pointing the wrong way. Any idea's why? This is the code:
CVector3 normX = Normalise( Matrix(2).XAxis() );
CVector3 normZ = Normalise( Matrix(2).ZAxis() );

//Then normalise vector from tank to target
CVector3 normTargVec = Normalise( CVector3( Matrix().Position(), m_Enemy->Matrix().Position() ) );

//Get dot products of angles and vectors
double b = Dot( normX, normTargVec );
	
double a = acos( Dot( normZ, normTargVec ) );
double turnSpeed = 0.01f;

if( b > 0 )
{			
	Matrix(2).RotateLocalY( min( turnSpeed, a ) );
}
else
{
	Matrix(2).RotateLocalY( min( -turnSpeed, a ) );
}
Thanks

Share this post


Link to post
Share on other sites
Advertisement
Your cannon is pointing in the good direction when the return value (b) equal 0. But as you use double precision doing a comparison to 0.0 is risky so you better do something like abs(b) > 0.01 let's say (0.01 error margin is 1.8 degrees)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!