Combinging rotation/scaling/translation matrices?

Started by
6 comments, last by chillypacman 16 years, 3 months ago
I know I'm doing something wrong here but I'm not sure what. Everytime I try to combine the rotaion matrice with the other matrices I end up with strange results. this is essentially the code block I'm having problems with:
void Monster::UpdateAnim(int i)
{
	D3DXMATRIX pos;
	D3DXMatrixTranslation(&pos, monsters.body->getGlobalPosition().x / (scale.x * 0.75),
		monsters.body->getGlobalPosition().y / (scale.y * 0.75),
		monsters.body->getGlobalPosition().z / (scale.z * 0.75));

	D3DXMATRIX scaler;
	D3DXMatrixScaling(&scaler, scale.x, scale.y, scale.z); //set scaler matrix

	
	NxVec3 vel =  monsters.body->getLinearVelocity();	

	D3DXMATRIX ultimate = pos;
	D3DXMATRIX rot;
	D3DXMatrixRotationY(&rot, 10);
	D3DXMatrixMultiply(&pos, &rot, &pos);
	D3DXMatrixMultiply(&pos, &rot, &scaler);

	monsters.anim->FrameMove(GAME::td, &pos);
}
if I simply apply the scaler + translation it works, however when I try to apply the rotation it starts doing wierd things (basically it starts moving in strange angles). The problem is only with the rotation matrice, everything else is fine, I know that for sure.
Advertisement
Your matrix multiplies turn out to be equal to this, I think:

pos = rot * pos * rot * scaler;

You're multiplying rot in twice. You're also rotating and scaling the translation - which is okay, as long as that's what you want. I think this may be what you actually want:

ultimate = rot * scaler * pos;

Then ultimate will be the fully combined matrix. That amounts to this, I think:

D3DXMatrixMultiply(&ultimate, &rot, &scaler);
D3DXMatrixMultiply(&ultimate, &ultimate, &pos);

If I rememeber D3DXMatrixMultiply correctly.
Composing a rotation matrix will have the effect of rotating about the accumulated origin, wherever that may be. Typically, we apply the rotation before translation so as to use the object-space origin. Conversely, if the rotation is to be performed about the world origin then apply it last. In order to rotate about an arbitrary origin you have to translate, rotate, then translate back. Does this make the problem any clearer?
Ring3 Circus - Diary of a programmer, journal of a hacker.
aaah, well I'm not sure, maybe there's a problem with whats happening in FrameMove. I didn't actually right the class itself, got it from here: http://www.toymaker.info/Games/html/load_x_hierarchy.html

I tried doign what Kest is but still the same problems. Here's what I'm doing now:

void Monster::UpdateAnim(int i){	D3DXMATRIX scaler;	D3DXMatrixScaling(&scaler, scale.x, scale.y, scale.z);	NxVec3 position = monsters.body->getGlobalPosition();	D3DXMATRIX pos;	D3DXMatrixTranslation(&pos, position.x, position.y, position.z);	D3DXMATRIX rot;	D3DXMatrixRotationY(&rot, 89.5);	D3DXMATRIX final = rot * pos * scaler;	monsters.anim->FrameMove(td, &final);}


still not sure really :-\ I'm sure I've managed to make it work before with the scaler and everything using the method above but for some reason it isn't working anymore.
Quote:Original post by chillypacman
I tried doign what Kest is but still the same problems. Here's what I'm doing now:

...
D3DXMATRIX final = rot * pos * scaler;
...

That's not what I did [smile]

D3DXMATRIX final = rot * scaler * pos;
The "normal" (i.e. those w/ the lowest annoyance of any particular transformation and the easiest to understand behaviour) chain would be
D3DXMATRIX final = scale * rot * pos;
That scales along the principal axes and w.r.t. the object's local origin, rotates the scaled object, and finally translates the rotated and scaled object to pos.

Althoug principally okay, the chain
D3DXMATRIX final = rot * scale * pos;
scales along "rotated" directions w.r.t. the object, and hence may behave not as wanted/expected.

As TheAdmiral has already stated, if other origins should be used, or if other axes should be used, then "temporary" translations and/or rotations has to be inserted. Here "temporary" means that their effect should take place for only the scaling or rotation, resp. But I assume that is out of the scope of the OP.
If you want to better understand the effect of combining different transformation matrices, take a look at my post here.
ok, I just figured it out, there's something going on with the animation class and the way in which it derives the matrix from boned animated .x files.

I know because I have other animated x files that don't use bones which scale and run fine.

So I'm going to have to find a way to work around the bones thing, apparently it isn't scaling so well because of them.

This topic is closed to new replies.

Advertisement