我在尝试延长org.jbox2d.dynamics.Body
时遇到问题
我已将Rectangle
设置为展开org.jbox2d.dynamics.Body
并将其设置为DYNAMIC
,但Rectangle
不会移动。
如果我创建多个Rectangle
,它们都位于相同的位置,即(0.0f,0.0f),为什么?
Rectangle
定义为:
public class Rectangle extends Shape {
private PolygonShape blockShape;
private Body body;
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 world,
BodyDef bd){
super(bd,world);
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;
this.bodydef.type = BodyType.DYNAMIC;
this.bodydef.position.set(centerPoint.x, centerPoint.y);
this.bodydef.angle = angle;
world.createBody(this.bodydef);
createFixture(fixtureDef);
setType(bt);
};
Shape
遵循以下定义:
public abstract class Shape extends org.jbox2d.dynamics.Body{
protected World world;
protected BodyDef bodydef;
public Shape(BodyDef bd, World w) {
super(bd, w);
this.world = w;
this.bodydef = bd;
}
protected abstract void update();
protected abstract void draw(GLAutoDrawable gLDrawable);
}
扩展Level.java
的{{1}}中
我创建了org.jbox2d.dynamics.World
并致电Rectangle
Rectangle.draw(gLDrawable)
答案 0 :(得分:0)
决定使用UserData
org.jbox2d.dynamics.Body
示例:
org.jbox2d.dynamics.Body body = level.createBody(bodydef);
body.setUserData(
new Shape(parameters)
);