libGDX - 如何在scene2d中向Actor添加(鼠标)关节

时间:2014-10-06 21:02:03

标签: java libgdx box2d scene2d

如果我有以下的GameStage:

...
public GameStage() {
    world = WorldUtils.createWorld();
    setUpWorld();
    setupCamera();
    debugRenderer = new Box2DDebugRenderer();
}

private void setUpWorld() {
    world = WorldUtils.createWorld();
    world.setContactListener(this);
    setupGround();
    setupBall();
    setupJoint();
}

private void setupBall() {
    ball = new Ball(WorldUtils.createBall(world));
    addActor(ball);
}

private void setupGround() {
    ground = new Ground(WorldUtils.createGround(world));
    addActor(ground);
}

private void setupJoint() {
    jointDef = new MouseJointDef();
    jointDef.bodyA = ???
    jointDef.collideConnected = true;
    jointDef.maxForce = 50f;
}
...

如何引用每个actor中的实体,以便我可以在两个实体之间添加关节。具体来说,我试图在球体上添加一个鼠标点,以便我可以添加一个方向性的冲动(鼠标点在触摸时被破坏或者如果达到最大冲量)。我在setupJoint方法中将地面设置为bodyA。在触地得分方法中,我将bodyB设置为球。然后,我从触摸中找到x和y值(首先取消它们从世界坐标移动到局部坐标)并使用jointDef.target.set设置鼠标的位置。我的问题是我无法弄明白从演员那里得到身体。以下是WorldUtils类中我的createBall方法的示例。

...
public static Body createBall(World world) {
    // BALL
    // Body Definition
    BodyDef ballDef = new BodyDef();
    ballDef.type = BodyDef.BodyType.DynamicBody;
    ballDef.position.set(0, 10); // 1 Meter up not 1 pixel

    // Ball shape
    CircleShape ballShape = new CircleShape();
    ballShape.setRadius(.5f);

    // Fixture Definition
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = ballShape;
    fixtureDef.density = 2.0f;
    fixtureDef.friction = .5f; // 0 to 1
    fixtureDef.restitution = .5f; // 0 to 1

    Body body = world.createBody(ballDef);
    body.createFixture(fixtureDef);
    body.resetMassData();
    body.setUserData(new BallUserData());
    ballShape.dispose();
    return body;
}
...

我的球和地面定义如下

...
public class Ground extends GameActor {
  public Ground(Body body) {
    super(body);
  }

  @Override
  public GroundUserData getUserData() {
    return (GroundUserData) userData;
  }
}

我确信我正在做一些愚蠢的事情,或者我误解了scene2d和box2d之间的联系是如何工作的。

由于

0 个答案:

没有答案