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 › 3D rotation of the coordinate system
Page Index Toggle Pages: 1
3D rotation of the coordinate system (Read 2851 times)
3D rotation of the coordinate system
Jul 20th, 2009, 3:42pm
 
Hi everybody,
I would like to rotate the coordinate system (using rotateX, rotateY, rotateZ) so that a vector (generated from an accelerometer input) always points into the same direction (ie: down).

My initial thought was to figure out the angle between the input vector and the three axis, and basically 'undo' each with a counter rotations:

float angleX, angleY, angleZ;
PVector v = new PVector(2,4,3);

angleAroundX = atan2(v.y,v.z);
angleAroundY = atan2(v.x,v.z);
angleAroundZ = atan2(v.x,v.y);

rotateX(-angleAroundX);
rotateY(-angleAroundY);
rotateZ(-angleAroundZ);

After reading up on the problem online, I now believe that I can't do the rotation in three steps, since each step affects the basis of the next rotations. Since there isn't a single 'rotate(x_angle, y_angle, z_angle)' function, I am a bit at a loss.

Any thoughts and pointers are much appreciated.
Thanks // pascal

Re: 3D rotation of the coordinate system
Reply #1 - Jul 21st, 2009, 6:00am
 
What you'll need to do is take the cross product of your input vector
and the axis you want to point along the input vector.

Quote:
PVector axis = new PVector(0, 0, 1);
PVector crss = input.cross(axis);



The cross-product has the nifty property of being perpendicular to both your input vector and the z-axis. What this means is that you can rotate your coordinate system about crss, by the angle between your input and the z axis, and the two will then line up.

Quote:
float theAngle = PVector.angleBetween(input, axis);
crss.normalize();
rotate(-theAngle, crss.x, crss.y, crss.z);



Of course, you'll need this rotate function to rotate around an arbitrary axis (math from the OpenGL Red Book):
Quote:
void rotate (float r, float x, float y, float z) {
 float d = sqrt(x*x+y*y+z*z);
 float a = x/d, b = y/d, c = z/d;
 float t[][]={{a*a,a*b,a*c},{b*a,b*b,b*c},{c*a,c*b,c*c}};
 float s[][]={{0,-c,b},{c,0,-a},{-b,a,0}};
 float cosr=cos(r);
 float sinr=sin(r);
 float m[][]={{1,0,0},{0,1,0},{0,0,1}};
 for (int i=0; i<3; i++) {
    for (int j=0; j<3; j++) {
       m[i][j] = t[i][j] + cosr*(m[i][j]-t[i][j])+sinr*s[i][j];
    }
 }
 
 applyMatrix (m[0][0],m[0][1],m[0][2],0,
                     m[1][0],m[1][1],m[1][2],0,
                     m[2][0],m[2][1],m[2][2],0,
                     0,0,0,1);
}

Re: 3D rotation of the coordinate system
Reply #2 - Jul 21st, 2009, 8:07am
 
dlp wrote on Jul 21st, 2009, 6:00am:
Of course, you'll need this rotate function to rotate around an arbitrary axis (math from the OpenGL Red Book):


I think it may be missing from the reference, but an arbitrary axis rotate *IS* present in the Processing API, no need to roll your own.  :)
Re: 3D rotation of the coordinate system
Reply #3 - Jul 21st, 2009, 10:47am
 
This was exactly what I needed!
Thank you very much. All is working now.
Best regards // pascal
Re: 3D rotation of the coordinate system
Reply #4 - Jul 21st, 2009, 7:30pm
 
davbol wrote on Jul 21st, 2009, 8:07am:
dlp wrote on Jul 21st, 2009, 6:00am:
Of course, you'll need this rotate function to rotate around an arbitrary axis (math from the OpenGL Red Book):


I think it may be missing from the reference, but an arbitrary axis rotate *IS* present in the Processing API, no need to roll your own.  :)


Heh, oops. I've been using straight JOGL for so long I forget what the built in stuff does. :)
Re: 3D rotation of the coordinate system
Reply #5 - Jul 22nd, 2009, 8:05am
 
I'm looking to do something similar (so that the Z axis always points to the camera) and I'd like to borrow this function. Can you give an example of what rotating around an arbitrary axis would look like using just the Processing language? (OpenGL and I are having a disagreement)

Much obliged - this could save me a huge headache.
Re: 3D rotation of the coordinate system
Reply #6 - Jul 23rd, 2009, 2:25pm
 
The built-in function works just like the function I've posted (which, by the way, doesn't use OpenGL anyways, the math is just from an OpenGL reference book).

rotate(float theta, float x, float y, float z)

Where theta is angle in radians, and x, y, and z are components of the arbitrary normalized vector to rotate about.
Page Index Toggle Pages: 1