【问题标题】:Andengine PhysicsHandler makes player look laggyAndengine PhysicsHandler 让玩家看起来很迟钝
【发布时间】:2015-10-19 16:12:04
【问题描述】:

嘿,我的问题是,在 PhysicsHandler 的 onUpdate 中,pSecondsElapsed 经常从 0.016... 跳到 0.038,这使得玩家移动得如此之大,以至于看起来玩家会滞后。 这里是来自 onUpdate 的重要代码:

@Override
protected void onUpdate(final float pSecondsElapsed, final IEntity pEntity) {
    if(this.mEnabled) {
        /* Apply linear acceleration. */
        final float accelerationX = this.mAccelerationX;
        final float accelerationY = this.mAccelerationY;
        if(accelerationX != 0 || accelerationY != 0) {
            this.mVelocityX += accelerationX * pSecondsElapsed;
            this.mVelocityY += accelerationY * pSecondsElapsed;
        }

        /* Apply angular velocity. */
        final float angularVelocity = this.mAngularVelocity;
        if(angularVelocity != 0) {
            pEntity.setRotation(pEntity.getRotation() + angularVelocity * pSecondsElapsed);
        }

        /* Apply linear velocity. */
        final float velocityX = this.mVelocityX;
        final float velocityY = this.mVelocityY;
        if(velocityX != 0 || velocityY != 0) {
            pEntity.setPosition(pEntity.getX() + velocityX * pSecondsElapsed, pEntity.getY() + velocityY * pSecondsElapsed);
        }
    }
}

顺便说一句,我只使用线速度。有人对此有解决方案吗?感谢您的帮助!

【问题讨论】:

  • 你不能忽略 pSecondsElapsed 并乘以某个常数吗?
  • @m.antkowicz 如果我这样做,游戏速度将取决于 fps

标签: java android andengine


【解决方案1】:

我继续浏览了该引擎的存储库,发现了一个可能对您有所帮助的类。基础引擎类有一个名为 Fixed Step Engine 的扩展,它应该允许您控制每帧的增量时间,这可能值得尝试(如果您还没有尝试过)以获得更流畅的物理效果。

【讨论】:

  • 谢谢!这实际上有助于获得恒定的 pSecondsElapsed,但现在整个游戏明显滞后,甚至玩家都无法顺利移动
【解决方案2】:

结合 FixedStepPhysicsWorld 您将获得最佳结果,但是:FixedStepPhysicsWorld 类内部存在一个小“错误”:

改变onUpdate如下:

...
while(this.mSecondsElapsedAccumulator >= stepLength && stepsAllowed > 0) {
    world.step(stepLength, velocityIterations, positionIterations);
    this.mSecondsElapsedAccumulator -= stepLength;
    stepsAllowed--;
}

this.mPhysicsConnectorManager.onUpdate(pSecondsElapsed);

...
while(this.mSecondsElapsedAccumulator >= stepLength && stepsAllowed > 0) {
    world.step(stepLength, velocityIterations, positionIterations);
    this.mSecondsElapsedAccumulator -= stepLength;
    stepsAllowed--;
    this.mPhysicsConnectorManager.onUpdate(stepLength);
}

原始代码中的问题是,PhysicsConnectors 在 PhysicsWorld 执行多个步骤(大多数情况下)后更新。我的代码解决了这个问题。每当 UiThread 绘制连接实体时,它们总是在其连接体所在的位置。他们不会跳过物理步骤。

【讨论】:

  • 嘿,谢谢你的回答!但问题是我没有使用 Physics box 2D 扩展。对不起,我之前没有提到。
猜你喜欢
  • 2018-12-16
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
  • 1970-01-01
相关资源
最近更新 更多