|
-
Dec 22nd, 2010, 09:08 AM
#1
[RESOLVED] 3D rotation
So I am facing this problem that I KNOW I have faced before...the solution can probably be found somewhere in a couple of my old projects.
I have a unit vector that describes a direction.
I have an object that needs to be rotated accordingly. When no rotation is applied, the object points straight down (0, -1, 0).
So, as an example, if this direction vector is (0, 1, 0), the object could be rotated 180 degrees on the X or Z axis in order to point in the diretion of the vector.
I'm looking for the best way retrieve the radians to rotate the object to point in the direction of said unit vector.
If there are plenty ways to skin this cat, I'd really like the most "efficient" way to calculate this.
-
Dec 22nd, 2010, 10:49 AM
#2
Re: 3D rotation
There are several solutions, depending on precisely what you want and have.
- If you have a start and end direction vector, the easiest solution is a linear interpolation between the two--no angles or trig required. That might be problematic since a linear interpolation of the direction vectors is a nonlinear interpolation when the intermediate vectors are projected back onto the unit sphere by normalizing. This can be overcome with a little bit of calculus and trig. Another problem is that your intermediate vectors may sometimes be the 0 vector, which generally isn't a valid direction.
- If you just want the angle between two vectors, the dot product formula is easiest. I explained it in this post.
- If you have an axis-angle rotation routine (like the one I described in this thread, using quaternions) you can generate the relevant axis by taking the cross product of your input vectors and calculating the angle using (2). There are degenerate cases like your example when a unique rotation isn't determined, which corresponds to the cross product being the 0 vector. These need to be special-cased; one technique that comes to mind is converting to spherical, where the special cases are all simple.
(1) is quite elegant if you don't mind a nonlinear rotation. (3) is quite powerful but complicated if you don't already have an axis-angle rotation routine. (2) answers "the best way retrieve the radians to rotate the object", which might not have said precisely what you meant it to say.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Dec 22nd, 2010, 01:34 PM
#3
Re: 3D rotation
Thanks for your reply jemidiah.
I am actually very proud of myself right now. I figured out the solution you mention as #3 before reading your response. It looks like this at the moment:
Code:
Vector3 axis = Vector3.Cross(this.direction, Vector3.Up);
axis.Normalize();
float dot = Vector3.Dot(this.direction, Vector3.Up);
this.world = Matrix.CreateScale(new Vector3(triangleBase, length, triangleBase)) *
Matrix.CreateFromAxisAngle(axis, -(float)Math.Acos(dot)) * Matrix.CreateTranslation(this.position);
Thank you very much for your help!
-
Dec 22nd, 2010, 07:08 PM
#4
Re: [RESOLVED] 3D rotation
apparently I must spread before I can rep you again Jed but excellent post
-
Dec 23rd, 2010, 08:58 AM
#5
Re: [RESOLVED] 3D rotation
 Originally Posted by Milk
apparently I must spread before I can rep you again Jed but excellent post
Same here unfortunately
-
Dec 31st, 2010, 07:51 AM
#6
Re: [RESOLVED] 3D rotation
BTW, there's a link in my sig to my old Coordinates, Vectors and 3D Volumes project, which has got a maths library and some functions to do a load of this stuff, if you're interested.....
-
Dec 31st, 2010, 08:34 AM
#7
Re: [RESOLVED] 3D rotation
Thanks zaza, I'll definitely check it out!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|