Skip to main content
gamedev.net
🔒 Locked

Rotating objects

Started by manhaeve 15 replies 1.5k views
Original Post
manhaeve
manhaeve
I used this code to rotate an object: glRotatef(90.0,0.0,1.0,0.0); but ot doesnt spin on it's y axis properly, it's like it turns in a circle around a center How do i solve this? Greetzzz Hyperion
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
Lord_Vader
Lord_Vader
Put glTranslate before glRotate, for example:

glLoadIdentity();
static int i = 0;

glTranslatef(0,0,-10);
glRotatef(++i,0,1,0);

DisplayModel();

and ensure that DisplayModel() hasn't any glTranslate and the model is centered.

[Edited by - Lord_Vader on February 21, 2007 3:52:18 PM]
relsoft
relsoft
The identity matrix resets your rotations.

Try

Loadidentity
pushmatrix
rotate
popmatrix

Hi.
manhaeve
manhaeve
Thx
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
manhaeve
manhaeve
But this doesnt help with my rotating problem: it doesnt spin correctly on its y axis, it spins in wide circcle around 1 point
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
relsoft
relsoft
Put glTranslate before glrotate.

Hi.
manhaeve
manhaeve
I did but it doesnt help
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
relsoft
relsoft
Can you post the whole code?
Hi.
manhaeve
manhaeve
#include <gl.h>
#include <glut.h>
#include <cstdlib>
float ypos = 0.0,xpos = 0.0,rotate=0.0;
void drawterrain(){
glColor3f(0,256,0);
glBegin(GL_POLYGON);
glVertex3f(0.1,0.9,0.0);
glVertex3f(0.9,0.9,0.0);
glVertex3f(0.9,0.1,0.0);
glVertex3f(0.1,0.1,0.0);
glEnd();
}
void drawobject(){
glColor3f(256,256,0);
glBegin(GL_POLYGON);
glVertex3f(0.4,0.6,0);
glVertex3f(0.6,0.6,0);
glVertex3f(0.6,0.4,0);
glVertex3f(0.4,0.4,0);
glEnd();
glColor3f(256,0,0);
glBegin(GL_POLYGON);
glVertex3f(0.4,0.6,0);
glVertex3f(0.6,0.6,0);
glVertex3f(0.6,0.6,0.1);
glVertex3f(0.4,0.6,0.1);
glEnd();
}
void display()
{
glLoadIdentity();
glClearColor(256,256,256,0);
glClear(GL_COLOR_BUFFER_BIT);
drawterrain();
glPushMatrix();
glTranslatef(xpos,ypos,0.0);
glRotatef(rotate,0.0,0.0,0.0);
drawobject();
glPopMatrix();
glFlush();
}

void init(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(0.0,1.0,-0.1,1.0,-1.0,0.0);
GLfloat lightpos[] = {0.5,0.5,-0.2,0.0};
GLfloat lightdir[] = {0,0,1};
GLfloat red[]={256,0,0};
GLfloat blue[]={0,0,256};
GLfloat white[]={256,256,256};
}
void keyboard(unsigned char key, int x,int y)
{
switch(key){
case 'z':
ypos = ypos+0.01;
display();
break;
case 's':
ypos = ypos - 0.01;
display();
break;
case 'q':
xpos =xpos - 0.01;
display();
break;
case 'd':
xpos =xpos + 0.01;
display();
break;
case 'a':
rotate = rotate +1.0;
display();
break;

}
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(800,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Titel");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
relsoft
relsoft
You didn't rotate on any axis.

void display(){glLoadIdentity();glClearColor(256,256,256,0);glClear(GL_COLOR_BUFFER_BIT);drawterrain();glPushMatrix();glTranslatef(xpos,ypos,0.0);glRotatef(rotate,0.0,0.0,0.0);   // this is the culpritdrawobject();glPopMatrix();glFlush();}

Here's a working one:

sub display()	glLoadIdentity()	glClearColor(0,0,0,0)	glClear(GL_COLOR_BUFFER_BIT)		glTranslatef(0,0,-5.0)	drawterrain()			glPushMatrix()		glTranslatef(xpos,ypos,1.0)				glRotatef(rotate,1.0,1.0,0.0)		drawobject()	glPopMatrix()	glFlush()end sub

Hi.
manhaeve
manhaeve
Free Image Hosting at www.ImageShack.us
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
relsoft
relsoft
What does that pic supposed to mean? My monitor is borked.
Hi.
manhaeve
manhaeve
What my object does when i use glrotatef
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
relsoft
relsoft
Did you try my code in the post above your the picture?

Hi.
manhaeve
manhaeve
It was just a mistake while copying it, i wanted to show my brother how glRotatef works, so it didnt help
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
Fimbulvetr
Fimbulvetr
I had this problem as well I hope what I'm about to tell you will help you...

You have the 3D cartesian system which has 3 axes, now when you spin around the axes, you take the closest vertex to the axe that you want to spin and spin the entire object while using this vertex as the pivot point, as far as I understand it, correct me if I'm wrong... now what you can do is make sure that the object is centered on the (0, 0, 0) point all the time, meaning, move it to the point you want to and then rotate it. Ok I'm not sure I'm clear enough so let's try some code:

float xRot = 0;float yRot = 0;float zRot = 0;// advance x, y, z rot every few milliseconds. . .//-------------------------------------------// Rendering codeglLoadIdentity();glClearColor(.0f, .0f, .5f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glLoadIdentity();glRotatef(45.0f + xRot, 1.0f,  .0f,  .0f);glRotatef(45.0f + yRot,  .0f, 1.0f,  .0f);glRotatef(45.0f + zRot,  .0f, .0f,  1.0f);glBegin(GL_QUADS);    glVertex3f(-0.5f,  0.5f, -10.0f);    glVertex3f( 0.5f,  0.5f, -10.0f);        glVertex3f( 0.5f, -0.5f, -10.0f);    glVertex3f(-0.5f, -0.5f, -10.0f);    glEnd();glPopMatrix();


My perspective is:
gluPerspective(45.f, (GLfloat)640/ (GLfloat)480, 1.f, 400.f);

So the basic idea is to keep the object centered on the axes...

Hope that helped...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.