Reputation: 3182
I have defined an object in 3D space with position, rotation and scale values (all defined as 3D vectors). It also has upwards and forwards direction vectors. When I rotate the object, I need these direction vectors to rotate with it.
Assuming my up vector is (0, 1, 0) and my forwards vector is (0, 0, 1) at zero rotation, how might I achieve this?
Upvotes: 4
Views: 12036
Reputation: 8733
Just rotate each of these vectors by the same angle as your object is rotated with. Let's say you are rotating around z axis (that is (0, 0, 1))
Equations will be:
x' = x cos(angle) + y sin(angle)
y' = -x sin(angle) + y cos(angle)
z' = z
Your "up" vector is (0, 1, 0), thus;
x' = 0 * cos(angle) + 1 * sin(angle) = sin(angle)
y' = -0 * sin(angle) + 1 * cos(angle) = cos(angle)
z' = 0
You "forwards" vector is (0, 0, 1), so:
x' = 0
y' = 0
z' = 1
It won't rotate since we were rotating around z axis, which is parellel to your forwards vector
Upvotes: 1
Reputation: 1812
You can multiply the current vector with the rotation matrix (Wikipedia entry, under 'basic rotations'). If the rotation is in 2 or more axis, just multiply by the appropriate matrices. For example, if you rotate by 30 degrees in the X axis and 60 in the Y axis, multiply by
| 1 0 0 |
| 0 cos(pi/6) -sin(pi/6) |
| 0 sin(pi/6) cos(pi/6) |
and then by
| cos(pi/3) 0 sin(pi/3) |
| 0 1 0 |
| -sin(pi/3) 0 cos(pi/3) |
Upvotes: 4