Sign in to follow this  
jsepia

Problem with transformation matrix

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 [url="http://www.juliosepia.com/demos/plane/index.html"]a demo[/url] 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 [url="http://www.songho.ca/opengl/gl_anglestoaxes.html"]http://www.songho.ca...glestoaxes.html[/url] ):
(the properties this.rx, ry, rz represent the angles of rotation around each axis in degrees)


[code]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);
}[/code]

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
[url="http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations"]http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations[/url]

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.

Share this post


Link to post
Share on other sites
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
[b]T[/b] * [b]S[/b]
and then multiplied by the combined R (presumably) on the right, yielding in
[b]T[/b] * [b]S[/b] * [b]R[/b]

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

Share this post


Link to post
Share on other sites
[quote name='Nanoha' timestamp='1302715162' post='4798027']
[url="http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations"]http://en.wikipedia....neral_rotations[/url]

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.
[/quote]

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


[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
[b]T[/b] * [b]S[/b]
and then multiplied by the combined R (presumably) on the right, yielding in
[b]T[/b] * [b]S[/b] * [b]R[/b]

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

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='jsepia' timestamp='1302728567' post='4798136']
[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
[b]T[/b] * [b]S[/b]
and then multiplied by the combined R (presumably) on the right, yielding in
[b]T[/b] * [b]S[/b] * [b]R[/b]

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

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

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

[i]But[/i], 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 [i]does[/i] 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

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