PDA

View Full Version : Trouble Rotating



Trek800
06-17-2012, 01:09 PM
I have a program that displays a 2D tanklike-object and has it move like a tank. The one problem I have is getting it to rotate like a tank. Every time it is rotated, it rotates, along the center of the screen, I want it to ratate along the center of the tank. Is there any thing I can do to fix this? the code for the program is below.


#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <GL/glut.h>

float zRotationAngle = 0.0f;

//Shared variables
static float sbdiv = .9;
static float sldiv = 1.1;

//Main Square Coordinates
static float bbottom = -.9;
static float brx = .6;
static float blx = -.6;

//*
//Smaller attatchment Coordinates
//*
static float srx = .2;
static float slx = -.2;

//*
//Even smaller attatchment
//*
static float ltop = 1.8;
static float lrx = .05;
static float llx = -.05;

void mydisplay(void)
{
glRotatef(zRotationAngle, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);


glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 0.0);
glVertex2f(brx, sbdiv);
glColor3f(0.0, 0.7, 0.0);
glVertex2f(brx, bbottom);
glColor3f(0.0, 0.0, 0.0);
glVertex2f(blx, bbottom);
glColor3f(0.0, 0.7, 0.0);
glVertex2f(blx, sbdiv);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 0.0);
glVertex2f(srx, sldiv);
glColor3f(0.0, 0.7, 0.0);
glVertex2f(srx, sbdiv);
glColor3f(0.0, 0.0, 0.0);
glVertex2f(slx, sbdiv);
glColor3f(0.0, 0.7, 0.0);
glVertex2f(slx, sldiv);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 0.0);
glVertex2f(lrx, ltop);
glColor3f(0.0, 0.7, 0.0);
glVertex2f(lrx, sldiv);
glColor3f(0.0, 0.0, 0.0);
glVertex2f(llx, sldiv);
glColor3f(0.0, 0.7, 0.0);
glVertex2f(llx, ltop);
glEnd();

zRotationAngle = 0;
glutSwapBuffers();
}

void spinDisplay(void)
{
glutPostRedisplay();
}

void myinit(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-4.0, 4.0, -4.0, 4.0);
}

static void key(unsigned char key, int x, int y)
{
switch(key){
case'w':
sbdiv += .05;
bbottom += .05;
sldiv += .05;
ltop +=.05;
break;
case 's':
sbdiv -= .05;
bbottom -= .05;
sldiv -= .05;
ltop -= .05;
break;
case'a':
zRotationAngle += .5;
break;
case'd':
zRotationAngle -= .5;
break;
case 27:
case 'q':
exit(0);
break;
}
glutPostRedisplay();
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(310, 0);
glutCreateWindow("Moving Square");

myinit();
glutDisplayFunc(mydisplay);
glutIdleFunc(spinDisplay);
glutKeyboardFunc(key);
glClearColor(1,1,1,1);
glutMainLoop();
}

kowal
06-18-2012, 08:25 AM
Translate your object using matrices (glTranslate or custom uniform matrix passed to vertex shader)
instead of modifying coordinates directly.

In order to work as You expect program must do the object transformations in proper order:
rotation and then translation. If You modify coordinates directly, then use glRotate and draw,
rotation is performed last (translation is already pre-applied in coordinates).

Trek800
06-20-2012, 03:15 PM
@kowal
Thank you, I adjusted it to translate using matrices and it works perfectly.