3

Alright, first off, I know similar questions are all over the web, I have looked at more than I'd care to count, I've been trying to figure it out for almost 3 weeks now (not constantly, just on and off, hoping for a spark of insight).

In the end, what I want to get, is a function where you pass in how much you want to rotate by (currently I'm working in Radian's, but I can go Degrees or Radians) and it returns the rotation matrix, preserving any translations I had.

I understand the formula to rotate on the "Z" axis in a 2D cartesian plane, is:

[cos(radians)    -sin(radians)    0]
[sin(radians)     cos(radians)    0]
[0                0               1]

I do understand Matrix Maths (Addition, Subtraction, Multiplication and Determinant/Inverse) fairly well, but what I'm not understanding, is how to, step-by-step, make a matrix I can use for rotation, preserving any translation (and whatever else, like scale) that it has.

From what I've gathered from other examples, is to multiply my current Matrix (whatever that may be, let's just use an Identity Matrix for now), by a Matrix like this:

[cos(radians) - sin(radians)]
[sin(radians) + cos(radians)]
[1]

But then my original Matrix would end up as a 3x1 Matrix instead of a 3x3, wouldn't it? I'm not sure what I'm missing, but something just doesn't seem right to me. I'm not necessarily looking for code for someone to write for me, just to understand how to do this properly and then I can write it myself. (not to say I won't look at other's code :) )

(Not sure if it matters to anybody, but just in-case, using Windows 7 64-bit, Visual Studio 2010 Ultimate, and I believe OpenGL, this is for Uni)

While we're at it, can someone double check this for me? Just to make sure it seems right.

A translation Matrix (again, let's use Identity) is something like this:

[1, 0, X translation element]
[0, 1, Y translation element]
[0, 0, 1]
user2368229
  • 150
  • 1
  • 2
  • 7

3 Answers3

2

First, You can not have translation 3x3 matrix for 3D space. You have to use homogeneous 4x4 matrices.

After that create a separate matrix for each transformation (translation, rotation, scale) and multiply them to get the final transformation matrix (multiplying 4x4 matrix will give you 4x4 matrix)

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • No no, sorry @Andrew, in the end, I will have it for 3x3 and 4x4 matrices, 3x3 for 2D rotation, and 4x4 for 3D. I'm just trying to get my head around the 3x3 rotation right now, and when I understand it more, make it work with 4x4 as well. I'm not sure why you thought I was using a 3x3 for a 3D cartesian plane, unless it's when I mentioned rotation on the "Z" axis, all I meant by that was rotating it as you would normally rotate something in 2D, and not rotating on the X or Y (which, in turn, would turn say standard side-by-side Mario into Paper Mario, if you understand what I mean) – user2368229 May 21 '13 at 06:53
  • @user2368229: Then just multiply you current (3x3) matrix by rotation matrix (3x3) and you'll get 3x3 matrix – Andrew May 21 '13 at 06:59
1

Lets clear some points:

Your object consists of 3D points which are basically 3 by 1 matrices.

You need a 3 by 3 rotation matrix to rotate your object: R but if you also add translation terms, transformation matrix will be 4 by 4:

[R11, R12, R13 tx]
[R21, R22, R23 ty]
[R31, R32, R33 tz]
[0,   0,   0,   1]

For R terms you can have look at :http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/, they are dependent on the rotation angles of each axis.

In order to rotate your object, every 3D point is multiplied by this rotation matrix. For every 3 by 1 point you also need to add a 4th term(scale factor) which is 1 assuming fixed scale:

[x y z 1]'

Resulting product vector will be 4 by 1 and the last term is the scale term which is 1 again and can be removed.

Resulting rotated object points are these new 3D product points.

Andrew
  • 24,218
  • 13
  • 61
  • 90
fatihk
  • 7,789
  • 1
  • 26
  • 48
1

I faced the same problem and found a satisfying formula in this SO question.
Let (cos0, sin0) be respectively the cosine and sine values of your angle, and (x0, y0) the coordinates of the center of your rotation.
To transform a 2d point of coordinates (x,y), you have to multiply its homogeneous 3x1 coordinates (x,y,1) by this 3x3 matrix:

[cos0,    -sin0,   x0-(cos0*x0 - sin0*y0)]
[sin0,     cos0,   y0-(sin0*x0 + cos0*y0)]
[   0,        0,                       1 ]

The values on the third column are the amount of translation necessary to apply when you rotation center is not the origin of the system.

Community
  • 1
  • 1
wip
  • 2,313
  • 5
  • 31
  • 47