• Advertisement
Sign in to follow this  

What on earth is wrong with this really basic rotation?

This topic is 1715 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

I have a nice little fireworks demo running, and I thought for the heck of it I'd spin the "ball of particles" that comprises the fireworks.  To do this, I assumed that the steps were:

 

- Translate back to origin from where it lives in space

- Apply rotation matrices for X, Y, Z

- Translate back to where object lived in space

- Set that as the World matrix before drawing the object (leaving projection and view completely alone)

 

It does not work as I would expect (or at all, really).  It just jumps around a bunch but largely stays put (no rotation).  I'm placing that new world matrix into the shader before drawing the instance.  I know all that works, as it draws them without rotation exactly as I'd hoped.  It's just my application of rotation that has broken everything...

 

Either I've got a really ugly bug or a very poor grasp of how to compose such a matrix.  A prod in either direction would be very much appreciated!

D3DXMATRIX matrixTranslateBefore;
D3DXMATRIX matrixRotationX;
D3DXMATRIX matrixRotationY;
D3DXMATRIX matrixRotationZ;
D3DXMATRIX matrixTranslateAfter;

float rads = fElapsedTime * 50.0f;

D3DXMatrixTranslation(&matrixTranslateBefore,  -pExplosion->_centerPosition.x, 
                      -pExplosion->_centerPosition.y, -pExplosion->_centerPosition.z);
D3DXMatrixRotationX(&matrixRotationX, rads);
D3DXMatrixRotationY(&matrixRotationY, rads);
D3DXMatrixRotationZ(&matrixRotationZ, rads);
D3DXMatrixTranslation(&matrixTranslateAfter, pExplosion->_centerPosition.x,
pExplosion->_centerPosition.y, pExplosion->_centerPosition.z);

pCameraManager->_worldMatrix = matrixTranslateBefore * matrixRotationX * matrixRotationY * matrixRotationZ * matrixTranslateAfter; 

 

Share this post


Link to post
Share on other sites
Advertisement

How exactly are you using the world matrix in your shader? It may be you're doing it wrong there and this more complex transformation has revealed a bug.

 

Also, are you sure your centerPosition field is correct, have you checked it? 

Share this post


Link to post
Share on other sites

What is in matrixTranslateBefore? Is it just translation or is there scaling, rotation, etc? Just to test, you might want to try:

D3DXVECTOR4 pos(0.0f, 0.0f, 0.0f, 1.0f);
D3DXVec4Transform(&pos, &pos, &(matrixTranslateBefore * matrixTranslateAfter));
// pos should be (0.0f, 0.0f, 0.0f, 1.0f) after this

If matrixTranslateBefore is just a translation, why don't you just use pExplosion->_centerPosition to translate once?

 

Also, you could simplify this:

D3DXMATRIX matrixRotationX;
D3DXMATRIX matrixRotationY;
D3DXMATRIX matrixRotationZ;

float rads = fElapsedTime * 50.0f;

D3DXMatrixRotationX(&matrixRotationX, rads);
D3DXMatrixRotationY(&matrixRotationY, rads);
D3DXMatrixRotationZ(&matrixRotationZ, rads);

into this:

D3DXMATRIX matrixRotation;

float rads = fElapsedTime * 50.0f;

D3DXMatrixRotationYawPitchRoll(&matrixRotation, rads, rads, rads);

Share this post


Link to post
Share on other sites

50 radians per second is really fast. Have you tried doing something like 1.0 radian per second?

 

Are you sure you want to rotate on all three axes like that? The results can be pretty unpredictable. Have you tried rotating on just one axis to start?

 

I am not sure I understand why you want to pre-translate (matrixTranslateBefore). If your verts are defined in model space, then you shouldn't need to do this. In fact, it would make things look glitchy, because you'd be translating away from the origin, rotating, and then translating again. Unless you have a really good reason to do this, consider removing that pre-translation.

Share this post


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

  • Advertisement