0
$\begingroup$

This is also programming related, but I think it's more about math than anything else.

I have an Object which I want to represent it's 3d position, rotation and scale with vectors, and then I need to transform everything into a single matrix4x4 representing all the object's transformations.

So, what I have:

Vector3(X, Y, Z) as Position
Vector3(X, Y, Z) as Scale
?? as Rotation

And from what I know about Matrices, this is what will give me an object's transformations:

Matrix4x4
[ scaleX,    0,      0,   positionX ]
[    0,   scaleY,    0,   positionY ]
[    0,      0,   scaleZ, positionZ ]
[    0,      0,      0,       1     ]
  • What do I need to have as "Rotation", and how does it related to the matrix's values in the example?
  • I've been suggested to use Quaternions to avoid Gimbal Lock, I've done some research on them, but I can't figure what values do I need to track and how can I insert these values in the above matrix, can you give me an idea of what should I do?
$\endgroup$
2
$\begingroup$

So I'm by no means an expert but this is how I understand it from the little computer graphics I've done (also I don't talk about quaternions here). I believe in this context you will be representing 3d vectors with 4 coordinates. The way the association works is that if I have a 4d vector

$\vec{w} = (a,b,c,d)$

then I always associate that to the 3d vector

$\vec{v} = (a/d,b/d,c/d)$.

What this is doing is projecting 4d space onto 3d space in a many-to-one fashion (except for d=0 of course). If you want to visualize what's going on here, it is helpful to imagine going from 3d space to 2d space: associate $(a,b,c)$ with $(a/c,b/c)$, and you can see that you're projecting every point $(a,b,c)$ in 3d space through the origin and onto the plane defined as the set of points where the z-coordinate is 1. The intersection will be the point $(a/c,b/c,1)$.

So what's the use of representing a 3d vector as some 4d vector? The reason that's helpful is we can represent certain transformations as a matrix when we couldn't do that before (and it's nice and orderly to keep all transformations in the same form, as a matrix, since to apply a string of them, you just multiply them out). For example, in 3d, we couldn't use a 3x3 matrix to represent translating the point by 1 in the x-direction. But now, if I have the point, say $(a,b,c)$, then I turn it into $(a,b,c,1)^T$ and multiply it on the left by

$\left[ \begin{array}{ccc} 1,0,0,1 \\ 0,1,0,0 \\ 0,0,1,0 \\ 0,0,0,1 \end{array} \right]$

which translates it by 1 in the x-direction.

Now, if you want to do a rotation in 3d space, you can just use a 3x3 matrix to do that already. So to incorporate it into the 4d transformation you already have for translation and scaling, we'll just figure out the 4d transformations that rotate just the same as the 3d ones and multiply the matrix you already gave.

So a rotation in 3d space is any 3x3 matrix $M$ where $M\cdot M^T = Id$. This definition may be somewhat useless to you, so it would be nice to parameterize these matrices. For example, 2d rotations can be parameterized very nicely with $\theta$ as

$\left[ \begin{array}{ccc} \cos(\theta) & -\sin(\theta) \\ \sin(\theta) & \cos(\theta) \end{array} \right]$

and 3d rotations can be parameterized in a number of ways. One way is as follows (source: http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation):

$R = \left[ \begin{array}{ccc} a^2 + b^2 -c^2 -d^2 & 2bc - 2ad & 2bd+2ac\\ 2bc+2ad & a^2-b^2+c^2-d^2 & 2cd-2ab \\ 2bd-2ac & 2cd+2ab & a^2-b^2-c^2+d^2 \end{array} \right] $

where $a^2+b^2+c^2+d^2=1$. So if you add a 3x1 column of zeros on the right, and a 1x3 column of zeros on the bottom, and a 1 in the lower right hand corner, then you have a 4x4 matrix $R'$ which performs a rotation on our vectors. Now to get the general form for a transformation, you multiply this $R'$ by the matrix you already have. Then you have a 4x4 matrix which can scale, translate, and rotate all in one.

EDIT

Another simpler way to talk about rotations is by describing how to rotate about the x, y, and z axes. Any rotation can be made from these basic ones, and they have an easier to understand form. You can see

$\left[ \begin{array}{ccc} \cos(\theta) &-\sin(\theta) &0 &0\\ \sin(\theta) &\cos(\theta) &0 &0 \\ 0 &0 &1 &0 \\ 0 &0 &0 &1 \end{array} \right]$

describes a rotation by angle $\theta$ about the $x$ axis, and you can make the rotations about the $y$ and $z$ axes in a very similar way. To combine them, just multiply the matrices.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.