#### Archived

This topic is now archived and is closed to further replies.

# 3d rotation

This topic is 5609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello , i am trying to rotate some object with posx,posy,posz coordinates around center of coordinates with 2 angles alpha and beta , It seems simple but the methods i have found didn''t work , maybe someone got an idea

Code?

##### Share on other sites
Hmm i have used this code

	// rotating to the center of coordinates					GLfloat befx = xpos[activew];					GLfloat befz = zpos[activew];           	        					GLfloat angle = (GLfloat)(atan(befx/befz));//(float)(PI * alpha[activew])/180.0;					             	GLfloat kx = cos(angle / 100) ;//  - sin(beta[activew]);					GLfloat kz = sin(angle / 100) ;//  + cos(beta[activew]);														xpos[activew] = befx * kx + befz * kz;					zpos[activew] = -befx * kz + befz * kx;	   			// now rotating to beta angle          		     		angle = (float)(PI * 5.0)/180.0;										GLfloat kz = cos(angle / 100) ;//  - sin(beta[activew]);					ky = sin(angle / 100) ;//  + cos(beta[activew]);											befz = zpos[activew];					GLfloat befy = ypos[activew];           			           zpos[activew] = befz * kz - befy * ky;				     ypos[activew] = befz * ky + befy * kz;		           										       	// rotating back to default angle					befx = xpos[activew];					befz = zpos[activew];           	        					angle = (GLfloat)(atan(befx/befz));//(float)(PI * alpha[activew])/180.0;					             	kx = cos(angle / 100) ;//  - sin(beta[activew]);					kz = sin(angle / 100) ;//  + cos(beta[activew]);					           					xpos[activew] = befx * kx - befz * kz;					zpos[activew] = befx * kz + befz * kx;

##### Share on other sites
glRotatef(alpha,0,1,0);
glRotatef(beta,0,0,1);

I have no idea what''s going on in your code... but this will rotate...

##### Share on other sites
quote:
Original post by LlamaGuy
glRotatef(alpha,0,1,0);
glRotatef(beta,0,0,1);

I have no idea what's going on in your code... but this will rotate...

hmm this would rotate a whole scene but i want' to rotate 1 object with coords posx,posy,posz on a desired angle

Btw i just recently found this code but it doesn't work correctly :

void rotateR(float alpha,float beta,float theta,float &px,float &py,float &pz){    // temporary x,y,z	float x = px,y = py,z = pz;    	// now pre compute some values	float alpha2 = (float)(PI * alpha)/180.0;	float beta2 = (float)(PI * beta2)/180.0;	float theta2 = (float)(PI * theta2)/180.0;		float mx1= (float)sin(theta2)*sin(beta2)*sin(alpha2) + cos(theta2)*cos(alpha2);	float my1= (float)cos(beta2)*sin(alpha2);	float mz1= (float)sin(theta2)*cos(alpha2) - cos(theta2)*sin(beta2)*sin(alpha2);			float mx2= (float)sin(theta2)*sin(beta2)*cos(alpha2) - cos(theta2)*sin(alpha2);	float my2= (float)cos(beta2)*cos(alpha2);	float mz2= (float)-cos(theta2)*sin(beta2)*cos(alpha2) - sin(theta2)*sin(alpha2);			float mx3= (float)-sin(theta2)*cos(beta2);	float my3= (float)sin(beta2);	float mz3= (float)cos(theta2)*cos(beta2);    px =(float)(x*mx1 + y*my1 + z*mz1);    py =(float)(x*mx2 + y*my2 + z*mz2);    pz =(float)(x*mx3 + y*my3 + z*mz3);}

[edited by - DMINATOR on June 1, 2004 12:42:14 PM]

##### Share on other sites
glPushMatrix();
glRotatef(alpha,0,1,0);
glRotatef(beta,0,0,1);
drawObject();
glPopMatrix();

Zorx (a Puzzle Bobble clone)
Discontinuity (an animation system for POV-Ray)

##### Share on other sites
Dont know if this helps you, but put this together to calculate the required rotation angles from X and Y axis to get to a predetermined vector. I actually dont know if this is entirely correct myself, since I am just learning this stuff also, so take this with a pinch-of-salt!

eg. vector v = 0.0, -3.0, 1.0 (pointing down the -ve Y axis slightly towards you)

Calculate the angles from X and Y axis for the resulting vector. This vector is the direction of the destination point from the origin point in my case:

v = (dest-origin);                      // actual vectorangleX = atan2(v.y,v.x)*(180.0/M_PI);   // calculate angle (degrees) from +ve X axisangleY = atan2(v.z,v.y)*(180.0/M_PI);   // calculate angle (degrees) from +ve Y axis

This should give you -90o from +ve X and +161.57o from +ve Y axis (other ArcTan routines also calculate only positive rotations, in which case it would be +270o and +161.57o). You can basically mix these around to choose the axis from which you wish to rotate. Just change the calcs.

Then I perform the rotation using Push/PopMatrix to affect only the local coordinate system

glPushMatrix();  origin.Translate();  glRotatef(angleX,0.0f,0.0f,1.0f);  glRotatef(angleZ,1.0f,0.0f,0.0f);  // do whatever....glPopMatrix();

If you think about it, angleX is a rotation from the +ve X axis to the -ve Y axis, which is infact a rotation around the Z axis - hence the glRotate command having Z axis set. The same for the rotation from +ve Y to -ve Y being a rotation around the X axis. If you visualise it you will get it.

Again, I might be totally wrong here, but for my demo it works! Hopefully someone can put me straight if I''m wrong

F451

##### Share on other sites
K i have found my problem , i have typed incorrect angle that's why the function rotated even with 0 angles

float alpha2 = (float)(PI * alpha)/180.0;

// Should be float beta2 = (float)(PI * beta )/180.0;
// I used beta2 wich is actually some random value

float beta2 = (float)(PI * beta2)/180.0;

// Should be float theta2 = (float)(PI * theta )/180.0;
// I used theta2 wich is actually some random value
float theta2 = (float)(PI * theta2)/180.0;

Problem is solved , program works correctly , here is how it supposed to look :

Rotating by alpha

Rotating by beta

result - camera position

[edited by - DMINATOR on June 2, 2004 4:48:04 AM]

##### Share on other sites
Yeah ,knowing how the object is rotated is great ....

But if you''re gonna use that on many points ,you''ll see that glRotatef does that much faster ....

##### Share on other sites
Im new to all this so ive NO clue what the push/popmatrix();''s buisness is all about but from looking at Jeff''s examples so long as you have the glRotatef(); junk between the glBegin/end() and before each object your rotating you should be able to rotate each item independantly.

And it seems to me that you could rotate the whole schene at the same time by haveing the glRotatef();''s outside the glBegin/end()''s also.

I guess im looking more for clerification then I am answering your question. Heh.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 20
• 36
• 9
• 16