Stuck with rotating a object with the camera , Directx

Abd4b56ff96074042dda5243e99a5d14
0
Anddos 103 Feb 04, 2010 at 06:04

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
cubework.png

now if i set it to

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

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

cubework2.jpg

can anyone help?

4 Replies

Please log in or register to post a reply.

Fd80f81596aa1cf809ceb1c2077e190b
0
rouncer 103 Feb 04, 2010 at 06:45

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]

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Feb 04, 2010 at 07:35

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. :D

Abd4b56ff96074042dda5243e99a5d14
0
Anddos 103 Feb 04, 2010 at 20:31

@Reedbeta

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?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Feb 04, 2010 at 21:13

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. :D