Rotate an opengl shape as box2d body rotates

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Rotate an opengl shape as box2d body rotates

bobbyrne01
Full question with images:
http://stackoverflow.com/questions/22924096/rotate-an-opengl-shape-as-box2d-body-rotates


I have a simple Java app setup which uses JOGL and JBox2D.
3 Rectangles, 2 are Dynamic and 1 is Static i.e. the ground

I can move 1 of the Rectangles left, right or by jumping using a KeyListener and adjusting the bodies BodyLinearVelocity accordingly.
But if the Rectangle (which i can move) falls off the other dynamic Rectangle, the graphical representation of the Box2D Body does not rotate correctly.
It looks like the Box2D Body is rotating but the GL2.GL_QUADS remains standing.
See empty space in below pic, I imagine the Rectangle should look like its standing on one of its edges if it rotated correctly?

How can I achieve this mapping between JOGL and JBox2D? I believe it's my opengl translate/rotate causing issue.


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




This is the Rectangle definition:

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();
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Rotate an opengl shape as box2d body rotates

gouessej
Administrator
Hi

Does this.body.getAngle() return an angle in degrees or in radians?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Rotate an opengl shape as box2d body rotates

bobbyrne01
It returned angle in radians, so i converted to degrees and it works now ..

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