bobbyrne01 bobbyrne01 - 2 months ago 14
Java Question

Rotate an opengl shape as box2d body rotates

I have a simple Java app setup which uses JOGL and JBox2D.

Below is how the app renders .. 3

Rectangle
s, 2 are Dynamic and 1 is Static i.e. the ground.


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

I can move 1 of the
Rectangle
s 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?

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


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

Answer

this.body.getAngle() returned angle in radians, so i converted to degrees and rotated on Z-axis.
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(); 
Comments