Sign in to follow this  
technorabble

Rotating the location of an array of objects around a fixed point

Recommended Posts

I have an array of objects in my scene. They are all in a line starting at my camera and progressing along the Z axis. What I would like to do is move them so they're off to the left by about 30 degrees (still radiating out from the location of the camera. Is there an easy way to do this using matrix rotation? e.g go from:

beginScene();
renderObjects();
doOtherStuff();
endScene();

to

beginScene();
// apply rotation
renderObjects();
// rotate back
doOtherStuff();
endScene();

Forgive me if this is a newbie question. I'm not very good with my matrices.

Share this post


Link to post
Share on other sites
Rotation about a point can be done as follows:

make rotation point the origin:
translate by -camera position
Apply rotation:
basic rotation...
Move everything back:
translate by camera position

Essentially you have 3 matrices, a pre translation which would translate by -rotation point, a rotation matrix which does the actual rotation and finally a post translation that put everyhting back (which is translation by the rotation point)

// semi psuedo code
preTranslation = Matrix.Translation(-rotationPoint)
postTranslation = Matrix.Translation(rotationPoint)
rotation = Matrix.AxisAngle(angle, yAxis)

You can multiply the 3 matrices together to get a single matrix

final = postTranslation *rotation*preTranslation;

if that doens't work swap the order (not sure how dx handles matrix multiplication order)

your code should then look like:
beginScene();
// apply final transform
renderObjects();
// reset transform (how thats done is up to you)
doOtherStuff();
endScene();

Share this post


Link to post
Share on other sites
What you need is to create a rotation matrix and multiply that matrix with the translation matrix of the objects and set your world matrix to the new matrix you get from the multiplication. You will have to do this for every objects in the scene: calculate the world matrix by multiplying the rotation matrix with the translation matrix and set your world transform to that matrix and draw the object.

The order in which you multiply matrices makes a difference so matRotation*matTranslation != matTranslation*matRotation, play with it to see what the difference is.

Share this post


Link to post
Share on other sites
Thanks. I'm working on this, but so far it's all a bit scary. I managed to attach the objects to my camera with this code, so still work to do:


g_pDirectXDevice->BeginScene();

D3DXMATRIX mWorldViewProj = matView * matProj;

D3DXMATRIX m_Rotation;
D3DXMatrixRotationY(&m_Rotation, 45.0);
g_pDirectXDevice->SetTransform(D3DTS_VIEW, &m_Rotation);
RenderTrees();
D3DXMatrixRotationY(&m_Rotation, -45.0);
g_pDirectXDevice->SetTransform(D3DTS_VIEW, &m_Rotation);

RenderGrass();
g_pDirectXDevice->EndScene();

I'll get there

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