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.
Page Index Toggle Pages: 1
Rotation (Read 544 times)
Rotation
Feb 28th, 2006, 6:39pm
 
is it possible to rotate a box (made from 6 quads) without using any of the rotate methods.

i just cant get my head around trigonometry

any examples would very greatly appriciated

Graham.
Re: Rotation
Reply #1 - Feb 28th, 2006, 11:29pm
 
The short short answer:
Yes, yes it is Smiley

The short, slightly more helpful answer:
read http://www.hardcorepawn.com/CoOrds/VectorMaths.pde and find the relevant chunks, and fix it to work properly (some functions return normalised results when they technically shouldn't)

The long, useful answer:
This is a simplified version of some code I wrote recently. The three rotate functions take a 3D point, rotate it, and return the rotated point. These do assume rotating around the origin though.

Code:

class Vector // for storing your verticies simply.
{
float x,y,z;
Vector(float _x, float _y, float _z)
{
x=_x;
y=_y;
z=_z;
}
}
Vector rotateX(Vector a,float ang)
{
float mz=a.z*cos(ang)+a.y*sin(ang);
float my=a.y*cos(ang)-a.z*sin(ang);
return new Vector(a.x,my,mz);
}
Vector rotateY(Vector a,float ang)
{
float mz=a.z*cos(ang)-a.x*sin(ang);
float mx=a.x*cos(ang)+a.z*sin(ang);
return new Vector(mx,a.y,mz);
}
Vector rotateZ(Vector a,float ang)
{
float mx=a.x*cos(ang)-a.y*sin(ang);
float my=a.y*cos(ang)+a.x*sin(ang);
return new Vector(mx,my,a.z);
}


So to use it you would do something like:
Code:

Vector[] corners;

... setup ...
corners=new Vector[8];
corners[0]=new Vector(-20,-20,-20);
corners[1]=new Vector(-20,-20,20);
corners[2]=new Vector(-20,20,-20);
corners[3]=new Vector(-20,20,20);
corners[4]=new Vector(20,-20,-20);
corners[5]=new Vector(20,-20,20);
corners[6]=new Vector(20,20,-20);
corners[7]=new Vector(20,20,20); //these points are written of the top of my head, one or two might be wrong...
...

... draw ...
for(int i=0;i<corners.length;i++)
{
corners[i]=rotateX(corners[i],0.1);
}
// Using these points to draw a cube is left as an exercise for the reader.


If you want to rotate a 3D point about an arbitary vector you'll have to look at http://www.hardcorepawn.com/CoOrds/VectorMaths.pde because it needs quite a few other functions I've defined to go with it.
It should be noted that that file is NOT of production quality.. there's a few idiosyncracies because it's evolved from my needs over a while, and some functions return a normalised result when they really shouldn't.
Re: Rotation
Reply #2 - Mar 1st, 2006, 12:17pm
 
Thanks for the code but im finding if fairly difficult to understand

I have no idea on how to draw the cude using the Vectors
i tried putting the vectors into a vertex() but that did not work.

Im fairly new to 3d graphics (only been working on it for about 2 months)

can you please show me how to draw the quads to create the cube

Thanks Graham
Re: Rotation
Reply #3 - Mar 1st, 2006, 12:45pm
 
There's 2 ways of doign this, either use all the x/y/z coordinates by hand:
Code:

beginShape(QUADS);

//Face 1
vertex(corners[0].x,corners[0].y,corners[0].z);
vertex(corners[1].x,corners[1].y,corners[1].z);
vertex(corners[2].x,corners[2].y,corners[2].z);
vertex(corners[3].x,corners[3].y,corners[3].z);

//Face 2
vertex(corners[4].x,corners[4].y,corners[4].z);
vertex(corners[5].x,corners[5].y,corners[5].z);
vertex(corners[6].x,corners[6].y,corners[6].z);
vertex(corners[7].x,corners[7].y,corners[7].z);

//Face 3
vertex(corners[0].x,corners[0].y,corners[0].z);
vertex(corners[1].x,corners[1].y,corners[1].z);
vertex(corners[4].x,corners[4].y,corners[4].z);
vertex(corners[5].x,corners[5].y,corners[5].z);
//etc, etc...


You may have to experiment to find the correct order to use the various corners.

The second method is to use an overloaded version of the vertex() function to take a Vector, to make things easier:
Code:

void vertex(Vector a)
{
vertex(a.x,a.y,a.z);
}

// ...

beginShape(QUADS);

//Face 1
vertex(corners[0]);
vertex(corners[1]);
vertex(corners[2]);
vertex(corners[3]);
//etc, etc...
Re: Rotation
Reply #4 - Mar 1st, 2006, 1:26pm
 
Thank you very much this is exactly what i needed

Graham.
Page Index Toggle Pages: 1