Box2D Rotation

Hi,

I know this is a Box2D discussion forum, but I have an OpenGL issue that is only related to Box2D. Maybe someone can help me out...

I'm using OpenGL to render Box2D and everything works well except for rotations. What i'm doing is actually drawing a sprite and all I want to do is glRotate that sprite according to the values returned from Box2d. Here's what i'm doing:

Box Class - create class with initial x, y and angular position. My class also has a draw method that just basically takes that x, y and angle and does this in OpenGL:

Code:
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


Each time Box2D steps through i rerun the previous OpenGL code with new coordinates like so:

Code:
vecs[0] = b2Vec2(level.body->GetPosition().x * 30.0f,level.body->GetPosition().y * 30.0f);
vecs[1] = b2Vec2(level.body->GetPosition().x * 30.0f + 10.0f, level.body->GetPosition().y * 30.0f);
vecs[2] = b2Vec2(level.body->GetPosition().x * 30.0f + 10.0f, level.body->GetPosition().y * 30.0f + 10.0f);
vecs[3] = b2Vec2(level.body->GetPosition().x * 30.0f,level.body->GetPosition().y * 30.0f + 10.0f);
   
draw->DrawSolidPolygon(vecs, 4, color);


This all works great until I try to rotate with glRotate with:
Code:
glMatrixMode(GL_MODELVIEW);
glRotatef([enter angle here], 0.0f, 0.0f, 1.0f);

- then redraw again with new level.body->GetPosition()'s


Now what happens is that not only the texture rotates but also the positions that I get from Box2D...they rotate as well. I understand what's happening. Basically by doing a glRotatef i'm rotating the coordinate system in OpenGL. And when Box2D tells me to place something at (5,2,0), for example, and i tell opengl to draw at (5,2,0)...it's already adjusted for the previous rotation command. Problem is I don't understand how to make this work where I can just pass x,y,angle to opengl straight from box2d.

How does everyone here handle rotations in OpenGL?

Thanks for the help,
Felix
Code:
glPushMatrix();
glRotatef(...);
glDrawArrays(...);
glPopMatrix();


should work.

If you're just doing sprites, you might try using a library like Haaf's (DirectX) or SFML (OpenGL). In my opinion, they have performance and style issues, but if you're not terribly familiar with OpenGL you can get to the meat of making a game as opposed to spending tons of time drawing sprites and creating windows.

Original thread: https://box2d.org/forum/viewtopic.php?t=3537