Sign in to follow this  
DOrmisher

Targetting and aiming

Recommended Posts

DOrmisher    100
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
mcmuzzle    248
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this