Jump to content
  • Advertisement
Sign in to follow this  
Veslefrikk

Object targeting

This topic is 3549 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.. Ive been strugling with this all day, so any feedback would be much appriciated. Im trying to have a turret in my 3d scene target the camera in this example. This is my current code wich works fine until I add some X and Z rotation to the source object, then it seems like the target vector dont get transformed into the source orientation.
D3DXVECTOR3 A = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );  // camera is always at 0,0,0
D3DXVec3TransformCoord( &A, &A, &ship_entity.matrix ); // transformation matrix

D3DXVECTOR3 B = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );          // Source object
D3DXVec3TransformCoord( &B, &B, &ship_entity.matrixRot );  // Rotation matrix

D3DXVec3Normalize(&A,&A);
D3DXVec3Normalize(&B,&B);

float Dot = D3DXVec3Dot(&A,&B);

float angle = acosf(Dot);

D3DXVECTOR3 cross;
D3DXVec3Cross(&cross,&A,&B);

if( cross.y > 0.0f )
	ship_entity.AddYRot(-0.001f);
else
	ship_entity.AddYRot(+0.001f);

Stefan

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!