• Create Account

# Problem with transformation matrix

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1jsepia  Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 April 2011 - 10:55 AM

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)
var sy = Math.sin(theta);
var cy = Math.cos(theta);

// rotation angle about Z-axis (roll)
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!

### #2Nanoha  Members   -  Reputation: 2299

Like
0Likes
Like

Posted 13 April 2011 - 11:19 AM

http://en.wikipedia.org/wiki/Rotation_matrix#General_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.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

### #3haegarr  Crossbones+   -  Reputation: 7158

Like
1Likes
Like

Posted 13 April 2011 - 11:33 AM

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.

### #4jsepia  Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 April 2011 - 03:02 PM

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. I guess something else must be wrong, but I'll get to that bridge when I cross it.

### #5haegarr  Crossbones+   -  Reputation: 7158

Like
0Likes
Like

Posted 14 April 2011 - 01:28 AM

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. ...

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS