PDA

View Full Version : Simple game rotation problem



Random89
02-09-2009, 05:34 AM
I am trying to make a simple game with a character who walks round however I am having a problem with it. In order for the player to have a different experience each time the character starts in a random position each time. However when I am doing the movement and making the character turn I have the problem of not having a point to rotate round as the character is made using a display list. To make it simpler I will post the code in question:
[quote]
int mouse_x=0, mouse_y=0;
bool LeftPressed = false;
int screenWidth=480, screenHeight=480;
bool keys[256];
float spin=0;

const double PI = 3.1415926535897932384626433832795;

void drawChar(float c);
GLuint Character;

GLuint texName;
const int checkImageHeight = 64;
const int checkImageWidth = 64;
GLubyte checkImage[checkImageHeight][checkImageWidth][4];
/************************************************** ******/

//variables for moving object
float Xtri = 0;
float Ytri = 0;
float Vtri = 0;
float heading = 0;

void display();
void reshape();
void init();
void processKeys();

/************* START OF OPENGL FUNCTIONS ****************/
void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
glTranslatef(Xtri, Ytri, 0.0);
glRotatef(heading, 0,0,1);
glCallList(Character);
glPopMatrix();

spin+=0.1f;
if(spin>360)
spin=0;

Xtri += Vtri*cos((90+heading)*(PI/180.0f));
Ytri += Vtri*sin((90+heading)*(PI/180.0f));
}
void drawChar(float c)
{
float Hpos[12];
float u = 36;
for ( int i = 0; i < 12; i++ )
{
Hpos[i] = u;
u = u - 6.5;
}
float Vpos[4];
Vpos[0] = 10.5;
Vpos[1] = -7;
Vpos[2] = -24.5;
Vpos[3] = -42;
srand(time(NULL));
float Hno1 = Hpos[rand() % 12];
srand(time(NULL));
float Vno1 = Vpos[rand() % 4];
float Hno2 = Hno1 - 2.5;
float Vno2 = Vno1-2.5;
glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex2f((Hno1-3),(Vno1+0.7));
glVertex2f((Hno1+3),(Vno1+0.7));
glVertex2f((Hno1+3),(Vno1-0.7));
glVertex2f((Hno1-3),(Vno1-0.7));
glEnd();

glFlush();
}

void reshape(int width, int height)
{
screenWidth=width; screenHeight = height;

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(-50,50,-50,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0);

Character = glGenLists(1);
glNewList(Character, GL_COMPILE);
drawChar(10);
glEndList();


makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
}
void processKeys()
{
if(keys[VK_LEFT])
{
heading+=0.1f;
}
if(keys[VK_RIGHT])
{
heading-=0.1f;
}
if(keys[VK_UP])
{
Vtri+=0.001f;
}
if(keys[VK_DOWN])
{
Vtri-=0.001f;
}
}/quote]
I know quite a bit is unneeded it is going to be used later.

-Ekh-
02-09-2009, 10:18 AM
hi !

Try this instead:



glLoadIdentity();
glRotatef(heading, 0,0,1); // rotate first
glTranslatef(Xtri, Ytri, 0.0);
glCallList(Character);
glPopMatrix();

Random89
02-09-2009, 06:56 PM
Still rotates around the 0,0 axis. Is there anyway to save an int as the location where an object currently is? Like the position of an object in the character list as it won't let me refer to the vertex's directly as it is void.

-Ekh-
02-10-2009, 01:44 AM
Okay, I think I see what you mean. The trick is to center the drawing of your character so that when rotating, it rotates from its center instead from one of its angle. You might need to do some translation and push/pop matrix for that.

dletozeun
02-10-2009, 04:19 AM
-Ekh-, with the code you provided, the character will not rotate first but translate first. Remember that you are modifying a matrix, so all transformation are actually done in the reverse order.



glLoadIdentity();
glTranslatef(Xtri, Ytri, 0.0);
glRotatef(heading, 0,0,1);
glCallList(Character);
glPopMatrix();




However when I am doing the movement and making the character turn I have the problem of not having a point to rotate round as the character is made using a display list


Can you reformulate this? I am not sure to understand.



glLoadIdentity();
glTranslatef(Xtri, Ytri, 0.0);
glRotatef(heading, 0,0,1);
glCallList(Character);
glPopMatrix();


If you want to make the character rotate around its center this piece of code is correct except the strange call to glPopMatrix at the end.
Calling glPopMatrix will set the modelview matrix with the matrix stored at the top of the matrix stack. Thus there are two problems:

1) I don't see the corresponding glPushMatrix call so I am pretty sure an opengl error is thrown because you are popping an empty stack.

2) All previous modification to the modelview matrix may be canceled as long as the glPopMatrix call does not fail.

In conclusion try to remove the glPopMatrix call.