正确的方法来扩展org.jbox2d.dynamics.Body

时间:2014-04-14 18:11:01

标签: java inheritance box2d jbox2d

我在尝试延长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)

1 个答案:

答案 0 :(得分:0)

决定使用UserData

org.jbox2d.dynamics.Body

示例:

org.jbox2d.dynamics.Body body = level.createBody(bodydef);
body.setUserData(
    new Shape(parameters)
);