0

编辑 *已解决。源代码更新到工作版本!*

我正在尝试使用 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);
4

0 回答 0