# Stuck with rotating a object with the camera , Directx

4 replies to this topic

### #1Anddos

Valued Member

• Members
• 177 posts

Posted 04 February 2010 - 06:04 AM

Ok so i have the camera working fine for yaw,pitch,roll but now i want to rotate the cube with the rotation of the camera, i am trying...

device->GetTransform(D3DTS_VIEW,&View);
D3DXMATRIX InView;
D3DXMatrixInverse(&InView,NULL,&(View));

D3DXVECTOR3 Pos(InView._41,InView._42,InView._43+1.0f);
now i see the cube infront of the camera

now i want the cube to rotate with the camera ..

D3DXMatrixTranslation(&CubeMat1,Pos.x,Pos.y,Pos.z);
D3DXMATRIX Rot;
D3DXMatrixRotationY(&Rot,InView._31);
device->SetTransform(D3DTS_WORLD,&(CubeMat1 * ROT));

it does it little bit and stops on about 25 degree's of yawing left, why?

pic

now if i set it to

device->SetTransform(D3DTS_VIEW,&(CubeMat1));

then it turns with the camera but it's far away like this..

can anyone help?

### #2rouncer

Senior Member

• Members
• 2722 posts

Posted 04 February 2010 - 06:45 AM

I would do it like this:


D3DXMATRIX orientation;
D3DXMatrixInverse(&orientation, NULL, &view); //view is your camera view matrix.

orientation._41=0;
orientation._42=0;
orientation._43=0;
orientation._14=0;orientation._24=0;orientation._34=0;orientation._44=1;

//now orientation is the rotation matrix for the cube to keep it facing the camera.
//it basicly just "unrotates"


All i did was invert the view matrix and took out the translation part of the matrix, so
it should only "orientate" not translate.

If you want, implement it and tell me what it does, im curious. :)

[EDIT] i tested it it works. Maybe someone would like to post a method that doesnt
take matrix inversion?[/EDIT]
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

### #3Reedbeta

DevMaster Staff

• 5306 posts
• LocationBellevue, WA

Posted 04 February 2010 - 07:35 AM

With the view matrix you should be able to invert it by taking the transpose, because it's orthonormal. Regardless, though, don't worry about the expense of the inversion; this is only done once per frame so it'll never even show up on a profile. Now if you were doing it 1000 times per frame I'd start worrying.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #4Anddos

Valued Member

• Members
• 177 posts

Posted 04 February 2010 - 08:31 PM

Reedbeta said:

With the view matrix you should be able to invert it by taking the transpose, because it's orthonormal. Regardless, though, don't worry about the expense of the inversion; this is only done once per frame so it'll never even show up on a profile. Now if you were doing it 1000 times per frame I'd start worrying. ;)

I don't understand what you mean , can you show an example?

### #5Reedbeta

DevMaster Staff

• 5306 posts
• LocationBellevue, WA

Posted 04 February 2010 - 09:13 PM

Which part of my post are you confused about - inverting by taking the transpose? Basically, if a matrix contains a pure rotation, no scaling or translation, it satisfies a condition called orthonormality. Don't worry too much about the mathematical definition; the practical consequence is that for matrices of this kind, their inverse is provably equal to their transpose. Since the transpose is cheap it's useful to substitute it for the inverse in cases where you know the matrix to be orthonormal.

Note that for 4x4 matrices, the transpose/inverse equivalence strictly speaking applies only when the translation part of the matrix is zero. You can also use this property to speed up inversion when there is a nonzero translation but I'll leave that as an exercise for the reader.
reedbeta.com - developer blog, OpenGL demos, and other projects

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users