Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rotate point around vector

Started by Alessandro Apr 10, 2012 at 5:46 PM 12 replies 2.1k views
Original Post
Alessandro
Alessandro
I hope I can explain what I need to do, I also attach this image, which should be helpful:

trouble1.jpg

I have a point A (with coordinates A.x, A.y, A.z) and a point B (B.x, B.y, B.z).

I'd like to rotate point B around the local X axis (1,0,0) of point A.

Is this clear enough?

Thanks again for any kind of support!

EDIT: just posted a better image, the previous was a bit cryptic...
alvaro
alvaro
If we knew how to rotate around the origin, we could solve your problem like this:

B'' = A + rotate_around_origin(B' - A)

So I'll just concentrate on rotating a point P around the origin, giving P' as result.

Now, how would we go about rotating around the X axis? Well, this is particularly simple because we'll just leave the x coordinate alone and do a 2D rotation on coordinates y and z

P'.x = P.x
P'.y = cos(alpha)*P.y - sin(alpha)*P.z
P'.z = sin(alpha)*P.y + cos(alpha)*P.z

If you want to rotate around a general axis, it gets trickier. You can do it using quaternions, which are not easy to understand but worth the trouble. Here's some code you can use.
Alessandro
Alessandro
I tried the quaternion solution posted by Alvaro, related to point rotation around vector and it works (tried some debug values).

However, if you take a look at the attached picture (it's updated), I need to rotate point B around the X local axis of point A (or general axis, as Alvaro calls it).

What I tried to do, is to Normalize vector A, and calculate the cross product of such normalized vector with (0,1,0). That way, I get the perpendicular vector to those; however this vector is normalized, and a rotation of point B around it will be done relative to origin (0,0,0), and not point A.
So how'd I account for that?

trouble1.jpg
alvaro
alvaro
Read the first two lines of my original response.

EDIT: Wait, what do you mean by "the X local axis of point A"? In your diagram, that doesn't seem to be parallel to the x axis...
Alessandro
Alessandro
Sorry for the mess: I made a new image that should explain better.

Say you have a line in 3D space made of 4 points, ABCD. You want to rotate it so that A is the origin, and more precisely you want to rotate it around the local X axis of point A (the one represented as a yellow line). And finally get A'B'C'D'.

I included both a perspective and a top view that should show better how points are disposed in 3D space...

trouble2.jpg
alvaro
alvaro
You are still not explaining what "the local X axis of point A" means. I asked for clarification and you simply used the exact same expression again.
Alessandro
Alessandro
You're right, sorry. The "local X axis of point A", represented by the yellow line in the above picture, is a vector perpendicular to AB and 0,1,0 (so the cross product of those two vector). It's the axis that I'd like to use to rotate point B,C,D around.
I hope this is more clear...
alvaro
alvaro
I already gave you your answer...
Alessandro
Alessandro
True, the quaternion solution used to define a rotation worked perfectly.
The problem subsequent to that, and that I couldn't quite explain was how to position back the rotated point (since the rotation was performed around a normalized vector). A simple vector sum did it.
Thanks for your help Alvaro. wink.png
taby
taby
MY DEAR SWEET GOD, SOMEONE ON GAMEDEV.NET THANKED SOMEONE FOR HELPING THEM. ;)
alvaro
alvaro
I don't know what Taby's experience has been on this website, but people thank me all the time around here. Indicating posts as useful is another way of thanking, and that happens a lot too.
taby
taby
I WAS TEASING. ;) It's a long story, and where's Owl?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.