当box2d体旋转时旋转opengl形状

时间:2014-04-07 22:14:17

标签: java opengl box2d jogl jbox2d

我有一个简单的Java应用程序设置,它使用JOGL和JBox2D 以下是应用呈现的方式.. 3 Rectangle s,2为动态,1为静态即地面。

3 Rectangles, 2 are Dynamic and 1 is Static i.e. the ground

我可以向左,向右移动Rectangle中的一个,或者使用KeyListener跳跃并相应地调整主体BodyLinearVelocity
但是,如果Rectangle(我可以移动)与另一个动态Rectangle脱离,则Box2D Body的图形表示无法正确旋转。
看起来Box2D Body正在旋转,但GL2.GL_QUADS仍然存在。
看到下图中的空白区域,我想Rectangle如果正确旋转,它应该看起来像是站在它的一条边上了吗?

如何在JOGL和JBox2D之间实现这种映射?

Graphical model not rotating correctly

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();
    }
}

2 个答案:

答案 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();