JOGL 및 JBox2D를 사용하는 간단한 Java 응용 프로그램 설정이 있습니다.
다음은 앱 렌더링 방법입니다. 3 Rectangle
, 2는 동적이고 1은 정적, 즉 지상입니다. box2d 본문이 회전 할 때마다 OpenGL 모양 회전
KeyListener
점프를 사용하고 이에 따라 바디
BodyLinearVelocity
을 조정함으로써,
Rectangle
의 좌측의 1을 이동시킬 수있다.
그러나
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();
}
}
그것은 지금 작동
.. ... : http://forum.jogamp.org/Rotate-an-opengl-shape-as-box2d-body-rotates-tp4032158p4032161. HTML – gouessej