【问题标题】:LibGDX box2d detection of specific instance of an ObjectLibGDX box2d 检测对象的特定实例
【发布时间】:2018-05-28 09:55:55
【问题描述】:

我对 box2d 的 beginContact 方法有点困惑。

我有一个 Runner 类,它会在游戏中生成一个跑步者。在游戏中,我有多个跑步者,我想检测跑步者的特定实例与障碍物之间的碰撞。 在 beginContact() 中,我想为被击中的跑步者启动 hit() 方法。

public void beginContact(Contact contact) {

    final Body a = contact.getFixtureA().getBody();
    final Body b = contact.getFixtureB().getBody();
    if ((BodyUtils.bodyIsRunner(a) && BodyUtils.bodyIsEnemy(b)) ||
            (BodyUtils.bodyIsEnemy(a) && BodyUtils.bodyIsRunner(b))) {
        Runner c;
        if(BodyUtils.bodyIsRunner(a)) c = (Runner) a.getUserData();
        else c = (Runner) b.getUserData();
        c.hit();

但是在这一行:

if(BodyUtils.bodyIsRunner(a)) c = (Runner) a.getUserData();

游戏因异常而崩溃:

com.pl.runner.box2d.RunnerUserData cannot be cast to com.pl.runner.entities.Runner

我现在不知道如何处理这个问题,所以如果有人可以提供建议或解决方案,我将非常感激。我可能缺少一些基本的东西,我被这段代码卡住了太久了。

这是 RunnerUserData 类:

public class RunnerUserData extends UserData {

    private final Vector2 runningPosition = new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y);
    private final Vector2 dodgePosition = new Vector2(Constants.RUNNER_DODGE_X, Constants.RUNNER_DODGE_Y);
    private Vector2 jumpingLinearImpulse;


    public RunnerUserData(float width, float height) {
        super(width,height);
        jumpingLinearImpulse = Constants.RUNNER_JUMPING_LINEAR_IMPULSE;
        userDataType = UserDataType.RUNNER;
    }

    public Vector2 getJumpingLinearImpulse() {
        return jumpingLinearImpulse;
    }

    public void setJumpingLinearImpulse(Vector2 jumpingLinearImpulse) {
        this.jumpingLinearImpulse = jumpingLinearImpulse;
    }

    public float getHitAngularImpulse() {
        return Constants.RUNNER_HIT_ANGULAR_IMPULSE;
    }

    public float getDodgeAngle() {
        // In radians
        return (float) (-90f * (Math.PI / 180f));
    }

    public Vector2 getRunningPosition() {
        return runningPosition;
    }

    public Vector2 getDodgePosition() {
        return dodgePosition;
    }
}

【问题讨论】:

    标签: java libgdx box2d user-data


    【解决方案1】:

    异常简单地描述了您的问题,您试图将RunnerUserData 转换为Runner,这是您无法做到的,因为Runner 不是RunnerUserData 的实例。

    解决此问题的方法是将实际的 Runner 对象作为用户数据传递,例如body.setUserdata(this)(在 Runner 类中)。

    您可以使用以下内容来确定夹具的用户数据是否为Runner 对象:

    if (body.getUserData() instanceof Runner) {}
    

    我建议将用户数据作为您拥有的每个身体的对象,这样会更容易找到它。

    【讨论】:

    • 感谢您的回答,if (body.getUserData() instanceof Runner) {} 现在可以正常工作,但我仍然无法在这种情况下触发 body.hit(),因为它根本不是亚军。当我尝试这样做时: Runner 类中的 body.setUserdata(this) 它说我不能将 Runner 转换为 UserData。
    【解决方案2】:

    因为我的 RunnerUserData 在我的游戏中的角色有点不同,所以我根本无法通过 body.setUserdata(runner) 中的 Runner

    所以我有一个小解决方法,它工作正常,也许有一天它会帮助某人: 在 beginContact() 方法中:

                Body c;
                if(a.getUserData() instanceof RunnerUserData) c = a;
                else c = b; //checks which body is a runner
                for (Runner r : runners){
                    if(r.getUserData() == c.getUserData()){
                        runners.remove(r); 
                        break; //do something if you detect proper runner and quit the loop
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多