The idea is to use a standard rotation matrix. In 2D this is:
-- -- -- -- -- --
| x_new | | cos(a) -sin(a) | | x_old |
| | = | | | |
| y_new | | sin(a) cos(a) | | y_old |
--- -- -- -- -- --
Where a
is the angle by which you're rotating.
The idea is that you're translating each and every point to a new point using this transformatoin. To get a better picture of this, consider a unit circle (which I don't know how to draw with ASCII art), and ask yourself how you can move the point (0,1) to (sqrt(2)/2,sqrt(2)/2)
(a 45 degree rotation).
x_new = x_old * cos(45) - y_old * sin(45) = 1 * sqrt(2)/2 - 0 * sqrt(2)/2 = sqrt(2)/2
y_new = x_old * sin(45) + y_old * cos(45) = 1 * sqrt(2)/2 + 0 * sqrt(2)/2 = sqrt(2)/2
Now translate this to (1,0)
, another 45 degree rotation:
x_new = x_old * cos(45) - y_old * sin(45) = sqrt(2)/2 * sqrt(2)/2 - sqrt(2)/2 * sqrt(2)/2 = 0
y_new = x_old * sin(45) + y_old * cos(45) = sqrt(2)/2 * sqrt(2)/2 + sqrt(2)/2 * sqrt(2)/2 = 1
Extending this to 3D is pretty straightforward, all you have to do is use another mutliplication for the rotation along the XZ plane.
Nathan Fellman
2009-09-09 18:40:32