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 › Calculating  local coords for a 3d vector
Pages: 1 2 
Calculating  local coords for a 3d vector (Read 5568 times)
Calculating  local coords for a 3d vector
Mar 31st, 2008, 3:10am
 
Hi everyone, i need help.  is there any way to obtain vectors that represent the main vectors local coordinate space? Basically If i have a 3d vector, how can i obtain a vector that is perpendicular to it that points to it's positive Y direction and another vector that points to it's Z or X direction.  Basically the main vector is used to plot a path similar to a ribbon or light trail.  I need the vectors perpendicular to it in order to give the final shape some width and height by offsetting the points a certain distance.  The end result is supposed to be a cube extruded along a spline.

Thanks Again
Stephen.
Re: Calculating  local coords for a 3d vector
Reply #1 - Mar 31st, 2008, 3:50am
 
hi sgsrules.
although not finished, is this any help for you?  
http://www.geocities.jp/classiclll_newweb/Deku10/applet/index.html
see the class "Karakuri" and the class "Loc".
Re: Calculating  local coords for a 3d vector
Reply #2 - Mar 31st, 2008, 11:17am
 
I think some code I wrote a few years ago should still be of use. The most recent copy on the boards I can find is here:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1159148328;start=0#0
Re: Calculating  local coords for a 3d vector
Reply #3 - Mar 31st, 2008, 6:05pm
 
Thanks for the sample code guys, this will be useful.  I ended up changing my approach.  Instead of drawing the shape based around the vector i just drew the shape and then made a function that repositions and rotates it around the given vector.
Re: Calculating  local coords for a 3d vector
Reply #4 - Mar 31st, 2008, 9:03pm
 
Sounds like exactly what I've just been doing.
Re: Calculating  local coords for a 3d vector
Reply #5 - Mar 31st, 2008, 11:44pm
 
For anyone that's interested here's the basic idea:

given vector with components (x,y,z);

rotateZ(atan2(y,x));
rotateY(atan2(z,x));

This will rotate everything to coincide with direction of the vector.
Re: Calculating  local coords for a 3d vector
Reply #6 - Apr 1st, 2008, 2:53pm
 
hi, ive been looking for the same effect, as in creating a trail in 3D space much like a ribbon.

would you have any example code to get the brain cogs turning?
Re: Calculating  local coords for a 3d vector
Reply #7 - Apr 1st, 2008, 10:46pm
 
@sgsrules:  you might want to check that atan2/atan2 really gives you the rotation you want.  I think what you want is a cartesian to spherical coordinate conversion to get those rotations.

It's a common misconception that to point at for example <1,1,1> you'd rotate z by PI/4 (atan2(y/x)), then rotate y by PI/4 (atan2(z/x)).  45 degrees around, then tilt 45 degrees up -- simple, right?  Yep, but wrong.  ;)

Here are two ways to draw a line from 0,0,0 to x,y,z:

Quote:

 // with no rotations, draw a cartesian coordinate line
 line(0,0,0, x,y,z);

 // with rotations, draw a spherical coordinate line
 pushMatrix();
 float r = sqrt(x*x+y*y+z*z); // radial distance
 float theta = atan2(y,x); // zenith angle, range -pi..pi
 float phi = acos(z/r); // azimuth angle, range -pi/2..pi/2
 rotateZ(theta); // "heading" or "around"
 rotateY(phi);  // "tilt" or "elevation"
 line(0,0,0, 0,0,r); // the z-axis now points "at" x,y,z
 popMatrix();



hope that helps
Re: Calculating  local coords for a 3d vector
Reply #8 - Apr 2nd, 2008, 2:18am
 
thank you thank you thank you thank you thank you

x e^(654,321x10^23)

very very useful math! =)

edit: so, now I can finally do my "thickLine" pipe-draw function
Re: Calculating  local coords for a 3d vector
Reply #9 - Apr 5th, 2008, 1:35am
 
Ok here's a quick function i made to rotate a point so that it lines up with a vector.

Void orient (Vec3D vec, float x, float y, float z){
  // get rotation amount
 float rotX=(atan2(vec.y,vec.z));
 float rotY=(atan2(vec.z,vec.x));
 float rotZ=(atan2(vec.y,vec.x));

  // rotation around x axis
  float y1 = cos(rotX)*y - sin(rotX)*z;
  float z1 = sin(rotX)*y + cos(rotX)*z;

  // rotation around y axis
  float finalZ = cos(rotY)*z1 - sin(rotY)*x;
  float x1 = sin(rotY)*z1 + cos(rotY)*x;

  // rotation around z axis
  float finalX = cos(rotZ)*x1 - sin(rotZ)*y1;
  float finalY = sin(rotZ)*x1 + cos(rotZ)*y1;

  x=finalX;
  y=finalY;
  z=FinalZ;
}

I thought this would work but i haven't tested it yet since i'm at work (I'm sure there's a couple of mistakes).  My trig is pretty rusty so i'm guessing this isn't right.  According to davbol, and i trust his judgment more than mine it should be:


Void orient (Vec3D vec, float x, float y, float z){

 float r = sqrt(vec.x*vec,x+vec.y*vec,y+vec.z*vec.z);
 
   // get rotation amount
 float rotY=(atan2(vec.z,vec.x));
 float rotZ=(acos(vec.z/r));

    // rotation around y axis
  float finalZ = cos(rotY)*z - sin(rotY)*x;
  float x1 = sin(rotY)*z + cos(rotY)*x;

    // rotation around z axis
  float finalX = cos(rotZ)*x1 - sin(rotZ)*y;
  float finalY = sin(rotZ)*x1 + cos(rotZ)*y;

  x=finalX;
  y=finalY;
  z=finalZ;
}

I'm using trig functions to handle the rotations because i can't use rotate and translate while im creating a shape with beginshape.  Will this work or am i going about this all wrong?  Once again thanks for all the help guys.
Re: Calculating  local coords for a 3d vector
Reply #10 - Apr 5th, 2008, 1:33pm
 
a point is only a position in space it has no direction.
if you're trying to rotate a vector or an object's orientation vector to follow another vector's orientation here goes 2 ways:

let V = V2 - V1;
U = (0, 1, 0);

1. normalize V vector
2. take the cross-product of U and V, this will give you the axis of rotation
3. take the dot-product of U and V. the arccosine of the result is the angle of rotation.
4. with this information you can already build a matrix or a quaternion and rotate your vector

just pass the axis and the angle and build a quaternion. rotate your vector/object

to build a matrix is the same as the camera matrix is built.
you have a U, V, N vectors. make the matrix out of it and use it to transform your data.
Re: Calculating  local coords for a 3d vector
Reply #11 - Apr 5th, 2008, 4:46pm
 
I'm rotating these points around the origin not themselves (that wouldn't accomplish much Tongue )  I kinda figured i'de have to use a matrix but was avoiding it,  time to do some more reading.  And do i really need to get into Quaternions?  Can't i just use vectors and matrices?
Re: Calculating  local coords for a 3d vector
Reply #12 - Apr 5th, 2008, 7:29pm
 
sure you can use just vectors and matrices.
Re: Calculating  local coords for a 3d vector
Reply #13 - Apr 5th, 2008, 11:28pm
 
I found this, which does exactly what i'm look for:

http://www.fundza.com/mel/axis_to_vector/align_axis_to_vector.html

This is pretty much what Davbol and some of the other guys have suggested.  This works but i think i'de rather use a matrix to do all my translations.  I have a vector that i'm using to specify the Z axis direction but i still need to obtain vectors for the X and Y axis which should be easy enough since they're at 90 degree angles from each other. This is what i've figured:

x axis = cross product of given Z vector and (0,1,0)
y axis = cross product of given Z vector and x axis

but since alot of my assumptions have been wrong so far i was going to check with the knowledgeable people on this board first. hopefully i've got it right this time so we can stop this thread Tongue
Re: Calculating  local coords for a 3d vector
Reply #14 - Apr 6th, 2008, 12:53pm
 
yes, here's something i use to compute local matrices for some project of mine. i hope this is what you really want to do.

void computeCSMatrix()
{
         // default up vector
         vec3 up = new vec3( 0, 1, 0 );

         // dir vector
   vec3 N = new vec3( z.x, z.y, z.z );
   N.normalize();

         // up vector
   vec3 U = up.cross( N );
 U.normalize();

         // right vector
 vec3 V = N.cross( U );
 V.normalize();

 mm[0] = U.x;
   mm[1] = U.y;
 mm[2] = U.z;
 mm[3] = 0;
 mm[4] = V.x;
 mm[5] = V.y;
 mm[6] = V.z;
 mm[7] = 0;
 mm[8] = N.x;
 mm[9] = N.y;
 mm[10] = N.z;
 mm[11] = 0;
 mm[12] = 0;
 mm[13] = 0;
 mm[14] = 0;
 mm[15] = 1;
}
Pages: 1 2