我有一个简单的Java应用程序设置,它使用JOGL和JBox2D
以下是应用呈现的方式.. 3 Rectangle
s,2为动态,1为静态即地面。
我可以向左,向右移动Rectangle
中的一个,或者使用KeyListener
跳跃并相应地调整主体BodyLinearVelocity
。
但是,如果Rectangle
(我可以移动)与另一个动态Rectangle
脱离,则Box2D Body
的图形表示无法正确旋转。
看起来Box2D Body
正在旋转,但GL2.GL_QUADS
仍然存在。
看到下图中的空白区域,我想Rectangle
如果正确旋转,它应该看起来像是站在它的一条边上了吗?
如何在JOGL和JBox2D之间实现这种映射?
public class Level extends org.jbox2d.dynamics.World {
private Vector<Rectangle> levelObjects = new Vector<Rectangle>();
public Level(Vec2 gravity) {
super(gravity);
this.setGravity(gravity);
levelObjects.add(
new Rectangle(
new Vec2(17.0f, 10.0f),
0.0f,
2.0f,
2.0f,
BodyType.DYNAMIC,
1.0f,
0.8f,
0.3f,
this));
levelObjects.add(
new Rectangle(
new Vec2(22.0f, 10.0f),
0.0f,
2.0f,
2.0f,
BodyType.DYNAMIC,
1.0f,
0.8f,
0.3f,
this));
}
protected void draw(GLAutoDrawable gLDrawable){
gLDrawable.getGL().getGL2().glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
for (Rectangle object : levelObjects){
object.draw(gLDrawable);
}
}
}
这是Rectangle
定义:
public class Rectangle {
private Vec2 centerPoint;
private PolygonShape blockShape;
private BodyDef bodydef;
private Body body;
private World world;
private float width;
private float height;
public Rectangle (Vec2 centerPoint,
float angle, float width, float height,
BodyType bt, float density, float friction, float restitution,
World w) {
this.world = w;
this.angle = angle
this.width = width;
this.height = height;
blockShape = new PolygonShape();
blockShape.setAsBox(this.width, this.height);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = blockShape;
fixtureDef.density = density;
fixtureDef.friction = friction;
fixtureDef.restitution = restitution;
bodydef = new BodyDef();
bodydef.type = BodyType.DYNAMIC;
bodydef.position.set(this.centerPoint.x, this.centerPoint.y);
body = world.createBody(bodydef);
body.createFixture(fixtureDef);
body.setType(bt);
};
void draw(GLAutoDrawable gLDrawable){
gLDrawable.getGL().getGL2().glLoadIdentity();
gLDrawable.getGL().getGL2().glColor3f(0.0f, 60.0f, 120.0f);
gLDrawable.getGL().getGL2().glTranslatef(this.body.getPosition().x, this.body.getPosition().y, -6.0f);
gLDrawable.getGL().getGL2().glRotatef(this.body.getAngle(), 0, 1, 0);
gLDrawable.getGL().getGL2().glBegin(GL2.GL_QUADS);
gLDrawable.getGL().getGL2().glTexCoord2f(0.0f, 0.0f);
gLDrawable.getGL().getGL2().glVertex3f(-this.width, -this.height, 0.0f);
gLDrawable.getGL().getGL2().glTexCoord2f(0.0f, 1.0f);
gLDrawable.getGL().getGL2().glVertex3f(-this.width, this.height, 0.0f);
gLDrawable.getGL().getGL2().glTexCoord2f(1.0f, 1.0f);
gLDrawable.getGL().getGL2().glVertex3f(this.width, this.height, 0.0f);
gLDrawable.getGL().getGL2().glTexCoord2f(1.0f, 0.0f);
gLDrawable.getGL().getGL2().glVertex3f(this.width, -this.height, 0.0f);
gLDrawable.getGL().getGL2().glEnd();
gLDrawable.getGL().getGL2().glFlush();
}
}
答案 0 :(得分:0)
glRotatef在输入中采用度数角度,您应确保旋转轴正确定向和标准化。我建议你在our official forum上询问有关JOGL的问题。
答案 1 :(得分:0)
this.body.getAngle()
以弧度为单位返回角度,因此我转换为度数并在Z轴上旋转
它现在有效..
gLDrawable.getGL().getGL2().glTranslatef(this.body.getPosition().x, this.body.getPosition().y, -6.0f);
gLDrawable.getGL().getGL2().glRotated(Math.toDegrees(this.body.getAngle()), 0, 0, 1);
gLDrawable.getGL().getGL2().glBegin(GL2.GL_QUADS);
gLDrawable.getGL().getGL2().glTexCoord2f(0.0f, 0.0f);
gLDrawable.getGL().getGL2().glVertex3f(-this.width, -this.height, 0.0f);
gLDrawable.getGL().getGL2().glTexCoord2f(0.0f, 1.0f);
gLDrawable.getGL().getGL2().glVertex3f(-this.width, this.height, 0.0f);
gLDrawable.getGL().getGL2().glTexCoord2f(1.0f, 1.0f);
gLDrawable.getGL().getGL2().glVertex3f(this.width, this.height, 0.0f);
gLDrawable.getGL().getGL2().glTexCoord2f(1.0f, 0.0f);
gLDrawable.getGL().getGL2().glVertex3f(this.width, -this.height, 0.0f);
gLDrawable.getGL().getGL2().glEnd();
gLDrawable.getGL().getGL2().glFlush();