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 & HelpSyntax Questions › 3D Rotation around an arbitrary axxis
Page Index Toggle Pages: 1
3D Rotation around an arbitrary axxis (Read 2960 times)
3D Rotation around an arbitrary axxis
Dec 4th, 2008, 11:48am
 
Hello,

i want to rotate an object around a point into the position of another object in 3d.
the angle i´m calculating, is the angleBetween the two normals of the two facing faces.

So, now i have the angle and i have an arbitrary axxis arround my object should rotate. But which technique/class i can use, to rotate my object?

I used to script in mel, and this is the way how i would make it there (e.g. rot command..). Or should i think in processing in a totally different way/concept?
I would be very pleased, if somebody could help me..


PVector v1 = new PVector(10, 20, 30);
PVector v2 = new PVector(60, 80, 33);

//calculate the angle between
float a = PVector.angleBetween(v1, v2);

//rotation around the axxis:
PVector v3 = v1.cross(v2);

//but how to rotate a vector around an arbitrary axxis?
Re: 3D Rotation around an arbitrary axxis
Reply #1 - Dec 4th, 2008, 12:09pm
 
Here's some code I wrote to rotate a point (v) around an arbitrary axis(passing through the origin) by a given angle:

This doesn't use processing's new PVector class, but should give you the steps you need. nx/ny/nz are normalised values of the x/y/z values.

Vector rotate(Vector v, Vector _axis,float ang)
{
 Vector axis=new Vector(_axis.nx(),_axis.ny(),_axis.nz());
 Vector vnorm=new Vector(v.nx(),v.ny(),v.nz());
 float _parallel=Dot(axis,v);
 Vector parallel=multiply(axis,_parallel);
 Vector perp=subtract(parallel,v);
 Vector Cross=cross(v,axis);
 Vector result=add(parallel,add(multiply(Cross,sin(-ang)),multiply(perp,cos(-ang))));
 return result;
}
Re: 3D Rotation around an arbitrary axxis
Reply #2 - Dec 4th, 2008, 12:32pm
 
ok, thank you very much. Now i understand, how to rotate one point around any axis.

But in my case, i already know the resulting vector and i want to rotate an object with the calculated angle around the calc. axis. so i need some code/techique how to transfer angle+axis in x,y,z rotation for the object..
Is this possible? Where i can look how?
..and thank you for answering..
Re: 3D Rotation around an arbitrary axxis
Reply #3 - Mar 28th, 2009, 9:50am
 
I think you would probably need a function similar to the one below.
I got the math from the OpenGL "red book". It's a shame the processing guys didn't include it in the set of transformation functions.
Code:

// Applies a general rotation of r radians around axis x,y,z
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);
}


Cheers,

--Claudio
Re: 3D Rotation around an arbitrary axxis
Reply #4 - Oct 26th, 2009, 2:18am
 
I think it is there: http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PMatrix3D.java?rev=4997&view=markup
Code:
  public void rotate(float angle, float v0, float v1, float v2) {
   // TODO should make sure this vector is normalized

   float c = cos(angle);
   float s = sin(angle);
   float t = 1.0f - c;

   apply((t*v0*v0) + c, (t*v0*v1) - (s*v2), (t*v0*v2) + (s*v1), 0,
(t*v0*v1) + (s*v2), (t*v1*v1) + c, (t*v1*v2) - (s*v0), 0,
(t*v0*v2) - (s*v1), (t*v1*v2) + (s*v0), (t*v2*v2) + c, 0,
0, 0, 0, 1);
 }


I think it's undocumented because rotate(Float) is already used for 2D XY rotations. rotate(Float,FloatFloat,Float) should work everywhere, it's in the PGraphics.
Page Index Toggle Pages: 1