Skip to main content
GameDev.net gamedev.net
🔒 Locked

Object targeting

Started by Veslefrikk Jan 6, 2009 at 11:55 AM 0 replies 700+ views
Original Post
Veslefrikk
Veslefrikk
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(✗,&A,&B);

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

Stefan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.