Sign in to follow this  
chillypacman

Combinging rotation/scaling/translation matrices?

Recommended Posts

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[i].body->getGlobalPosition().x / (scale.x * 0.75),
		monsters[i].body->getGlobalPosition().y / (scale.y * 0.75),
		monsters[i].body->getGlobalPosition().z / (scale.z * 0.75));

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

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

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

	monsters[i].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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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[i].body->getGlobalPosition();

D3DXMATRIX pos;
D3DXMatrixTranslation(&pos, position.x, position.y, position.z);

D3DXMATRIX rot;
D3DXMatrixRotationY(&rot, 89.5);
D3DXMATRIX final = rot * pos * scaler;

monsters[i].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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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