# Thread: Silvercord: Rotate around an arbitrary axis

1. ## Silvercord: Rotate around an arbitrary axis

I addressed this to SC but anybody who can help is welcome since I'm frickin' lost. What I want to do seems fairly simple but I just can't seem to get it right.

I have a vector that I want to rotate about (0, 0, 0); kind of like a clock hand rotating. Here's what I tried:

original vector: (0, 1, 0)
angle to rotate 90
rotating around z axis at (0, 0, 0)

rotated vector.x = cos(90) * original.x + sin(90) * original.y
rotated vector.y = cos(90) * original.y - sin(90) * original.x

I'm sure that I'm just doing something wrong in my algorithm or missing a step somewhere.

Jason

2. Thanks, FYB. I wasn't converting from degrees to radians. That fixed it.

3. Also, your Z axis would be <0,0,1>, not (0,0,0)

4. I assumed he meant that the object was centered at 0, 0, 0. You do need a center point in addition to a vector to do an arbitrary axis rotation.

5. I am centered around (0,0,0). I probably worded my original question wrong. I am doing a rotation just like a clock hand rotating around the center of the screne. It's working fine now. Thanks again.

6. I'm confused, because an arbitrary axis rotation shouldn't refer to x or y components, in fact I was always under the impression that there is no x or y, that the x is really the component of the vector you are trying to rotate perpendicular to the axis you are rotating about, and the 'y' is really the crossproduct between the perpendicular component and the axis. I'll draw a picture:

http://www.thedevelopersalliance.com...aarotation.JPG

EDIT: and here's how you calculate parallel and perpendicular components.

the parallel component of vector P onto vector A (A means Axis) is:
(DotProduct(A, P) / ||A|| ^ 2) * A
if they are already unit vectors (as they SHOULD be!) it boils down to:

DotProduct(A, P) * A

Interestingly the dotproduct between two unit vectors is the cosine of the angle between the two

The perpendicular component is P - the parallel component

P` = P - (DotProduct(A, P)*A)

EDIT1: and jdinger you are completely right about being centered at the origin, that's how I always perform these rotations, in fact almost every pertinent calculation for games whether it be BSP traversal by examining plane normals or bounding box offset calculations are all done at the origin. (with plane normals you do need a real distance from the origin so that you can determine if your real position is in front or behind a plane that isn't actually at the origin, but the plane normal itself is at the origin)