修改 * 解决。源代码已更新为工作版! *
我正在尝试使用OpenGL和Box2D为我的下一个游戏制作一个简单的游戏引擎。
问题是我无法很好地完成这两项工作。如果我关闭OpenGL旋转,一切看起来都像我真实的世界。问题在于旋转。
http://cl.ly/image/0I0M3C3K1c3c - 轮换关闭 http://cl.ly/image/421v321t201D - 轮换
我不知道它是Box2D还是OpenGL问题,但我认为如果它处于平静状态的平地上,或者它明显违反物理定律,则不应该旋转右框。这不仅是一个问题,而且只有图片清晰可见。我个人责备OpenGL:)
这是我的渲染代码
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
GLuint texture_handle;
/* texture load logic */
...
...
glTranslatef(this->xPos + this->width / 2, this->yPos + this->height / 2, 0.0f);
glRotatef(this->angle, 0.0f, 0.0f, 1.0f);
glTranslatef(-this->width / 2, -this->height / 2 , 0);
/* Creates 3D cube */
glBegin(GL_QUADS);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(this->GetWidth(), 0);
glTexCoord2f(1, 1);
glVertex2f(this->GetWidth(), this->GetHeight());
glTexCoord2f(0, 1);
glVertex2f(0, this->GetHeight());
glTexCoord2f(0, 0);
glEnd();
glDeleteTextures(1, &texture_handle);
glPopMatrix();
glFlush();
完成后,这里是Box2D部分
/* define body for box */
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(this->xPos / RATIO, this->yPos / RATIO);
bodyDef.angle = this->angle * (M_PI / 180);
body = this->world->CreateBody(&bodyDef);
b2PolygonShape box;
box.SetAsBox((this->width / 2) / RATIO, (this->height / 2) / RATIO);
b2FixtureDef shapeDef;
shapeDef.shape = &box;
shapeDef.density = 1f;
shapeDef.friction = 0.8f;
body->CreateFixture(&shapeDef);
this->body = body;
/* apply physics */
b2Vec2 position = this->body->GetPosition();
this->angle = this->body->GetAngle() / (M_PI / 180);
this->xPos = (position.x * RATIO);
this->yPos = (position.y * RATIO);