We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Obtaining rotation matrix from a vector
Page Index Toggle Pages: 1
Obtaining rotation matrix from a vector? (Read 1243 times)
Obtaining rotation matrix from a vector?
Feb 14th, 2006, 2:00am
 
I need some help with 3d maths..

What I'd like to be able to do is to take a 3d vector, and use its values to rotate into the plane perpendicular to the vector, so that the Z-axis lines up with the vector,
and Y is in the same plane as the original y..

My approach so far has been to calculate the y-rotation from the vector x and z, and x-rotation from the vector y and z.. However combining these two rotations doesn't quite work.. ends up kind of wobbly.

I'm reading up on rotation matrices now.. (is it something to do with Euler angles? are those good?) but if anyone has pointers to a solution, I'd love to hear from you.

Thanks,
sam.

Re: Obtaining rotation matrix from a vector?
Reply #1 - Feb 15th, 2006, 8:04pm
 
what do you mean with:
>and Y is in the same plane as the original y.. ?
Re: Obtaining rotation matrix from a vector?
Reply #2 - Feb 16th, 2006, 1:02am
 
I have the same problem:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Contribution_3DOpenGL;action=display;num=1139678741

Basically I want to rotate things in relation to a 3D vector. I think I used a similar approach to you, but without any luck. I have been looking at the vecmath library which implements vectors and matrixes:
https://vecmath.dev.java.net/
Also I was trying to use the vec3D implementation by Marius Waltz
http://processing.unlekker.net/Vec3D.html
which allows you to rotate the vector, but it is still not looking right.

Let me know if you solve it
Re: Obtaining rotation matrix from a vector?
Reply #3 - Feb 17th, 2006, 3:37pm
 
What do you mean with:
> Basically I want to rotate things in relation to a 3D vector

i am not feeling like going through your code. please
state your problem in one clear sentence.
Re: Obtaining rotation matrix from a vector?
Reply #4 - Feb 18th, 2006, 1:43am
 
I have a line in 3D space defined by two points. I want to transform the x-axis of the world matrix to align with the line, so that the point1 is on (0,0,0) and point2 is on (lineLength,0,0). Then I want to draw an array of perpendicular lines by rotating the world matrix around the x-axis.

My problem is that the perpendicular lines I draw doesn't always appear perpendicular to the original line when I move it around in 3D space.
Re: Obtaining rotation matrix from a vector?
Reply #5 - Feb 18th, 2006, 10:34am
 
Sorry folks. On reflection my original post was not very clear. Blame late nights and a poor understanding of geometry :)

Managed to figure it out though. So here's my solution, since others have similar problems. Who would have thought one extra dimension would complicate things so?

Firstly, what I should have said.. Given a 3d vector, I wanted to calculate the angles of x and y rotation to make the Z axis line up along that vector. Also (as it turns out) I want the X axis to stay 'flat', i.e. no Z rotation.

Turns out this is reasonably simple so long as you rotate in Y first. Here's the solution..

float rx = asin(-vector.y);
float ry = atan2(vector.x, vector.z);
rotateY(ry);
rotateX(rx);

Works a charm.

http://www.flipcode.com/documents/matrfaq.html was pretty helpful.. thanks to whoever originally posted this link. (sorry, couldn't find the thread).

See ya. I'll endeavour to think things through a bit more before posting next time ;)

Re: Obtaining rotation matrix from a vector?
Reply #6 - Feb 19th, 2006, 10:16pm
 
samuel_bruce´s algorithm looks right to me
on a first glance. lars -just be careful, since
your vector has generically no unit length, you
have to normalize before taking the asin, i.e
float rx = asin((-vector.y)/(length of vector));
...and of course samuel_bruce is taking the
Z axis instead of the X axis...Smiley
if you want to understand whats going on then
rotateY(ry);
rotateX(rx); means actually rotate first around the
X-axis and THEN around the Y-Axis. the reverse order
of the command is due to the matrix product.
so what samuel_bruce means with "X axis to stay 'flat"
is that the X axis is staying in the X-Z-plane.
by the way i found the link not so overly useful,
it may be better to look into a math book.
Re: Obtaining rotation matrix from a vector?
Reply #7 - Mar 22nd, 2006, 12:30am
 
thanks for the help nad - sorry for being so slow to reply

I tried samuel_bruce's solution but am still having problems. Think I will have a look in a math book to get a better understanding of the problem.
Page Index Toggle Pages: 1