Jump to content
  • Advertisement
Sign in to follow this  
jsepia

Problem with transformation matrix

This topic is 2686 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'm making a lightweight 3D engine (nothing serious) in HTML5 and something is wrong with my rotations. I'm working with a Camera class that contains properties for rotation, translation and scaling in each of the tree axes. On every frame, the class creates a new transformation matrix from these properties, which is then applied to every point in order to draw them.

I have a demo that shows a simple 3D model with th camera rotating around the Y axis. The current transformation matrix can be seen overlaid on top of the canvas.


Scaling and translating work great so far, but rotations seem very wonky. I believe the problem lies in the function used to create the transformation matrix (which I took from http://www.songho.ca...glestoaxes.html ):
(the properties this.rx, ry, rz represent the angles of rotation around each axis in degrees)


this._calculateMatrix = function()
{
// reset to identity matrix
this.m = j3dMat.getIdentity();

// translation
this.m[3][0] = this.tx;
this.m[3][1] = this.ty;
this.m[3][2] = this.tz;

// scale
this.m[0][0] = this.sx;
this.m[1][1] = this.sy;
this.m[2][2] = this.sz;

// rotation begins here
this.rx = this.rx % 360;
this.ry = this.ry % 360;
this.rz = this.rz % 360;
var DEG2RAD = 3.141593 / 180;
var rm = j3dMat.getIdentity(); // a separate rotation matrix

// rotation angle about X-axis (pitch)
var theta = this.rx * DEG2RAD;
var sx = Math.sin(theta);
var cx = Math.cos(theta);

// rotation angle about Y-axis (yaw)
theta = this.ry * DEG2RAD;
var sy = Math.sin(theta);
var cy = Math.cos(theta);

// rotation angle about Z-axis (roll)
theta = this.rz * DEG2RAD;
var sz = Math.sin(theta);
var cz = Math.cos(theta);

// determine left axis
rm[0][0] = cy*cz;
rm[0][1] = sx*sy*cz + cx*sz;
rm[0][2] = -cx*sy*cz + sx*sz;

// determine up axis
rm[1][0] = -cy*sz;
rm[1][1] = -sx*sy*sz + cx*cz;
rm[1][2] = cx*sy*sz + sx*cz;

// determine forward axis
rm[2][0] = sy;
rm[2][1] = -sx*cy;
rm[2][2] = cx*cy;

// multiply trans matrix by rotation matrix
this.m = j3dMat.mult(this.m, rm);
}


What am I doing wrong? Any pointers/resources on how to do this the right way?

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Haven't looked at the rotational terms, but: The transformation order shown in the code snippet is unusual. Assuming that column vectors are used, then the matrix is initially set to
T * S
and then multiplied by the combined R (presumably) on the right, yielding in
T * S * R

This means scaling is applied along the already rotated model axes, what is probably not wanted. The usual order is T * R * S instead.

Share this post


Link to post
Share on other sites

http://en.wikipedia....neral_rotations

Your terms appear to be slighly different, I'm looking at the second column, top row and I don't see a similar term in yours (-cos*sin + sin*sin*cos). Check all your terms again.


This helped a lot. My terms were wrong. Thanks!



Haven't looked at the rotational terms, but: The transformation order shown in the code snippet is unusual. Assuming that column vectors are used, then the matrix is initially set to
T * S
and then multiplied by the combined R (presumably) on the right, yielding in
T * S * R

This means scaling is applied along the already rotated model axes, what is probably not wanted. The usual order is T * R * S instead.


Both work the same for me, even with scaling != 1. :mellow: I guess something else must be wrong, but I'll get to that bridge when I cross it.

Share this post


Link to post
Share on other sites

[quote name='haegarr' timestamp='1302715998' post='4798032']
Haven't looked at the rotational terms, but: The transformation order shown in the code snippet is unusual. Assuming that column vectors are used, then the matrix is initially set to
T * S
and then multiplied by the combined R (presumably) on the right, yielding in
T * S * R

This means scaling is applied along the already rotated model axes, what is probably not wanted. The usual order is T * R * S instead.


Both work the same for me, even with scaling != 1. ...
[/quote]
If sx==sy==sz==1, then S is the identity matrix, and then of course T * S * R == T * R * S, because M * I == M for any M.

If sx==sy==sz where sx!=1 (i.e. uniform scaling), then again T * S * R == T * R * S.

But, if sx!=sy, sy!=sz, or sx!=sz (i.e. non-uniform scaling, what I assume is allowed because you use 3 different variables here) then the order does have an effect. For example, if you have an airplane that should be made longer, and you apply the scaling on the rotated model, then the airplane gets e.g. wider instead of longer.

This may not be the primary issue you're currently facing, but it will pop up later.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!