问题
Hey my problem is that in the onUpdate in PhysicsHandler the pSecondsElapsed often jumps from 0.016... to 0.038 which makes the player move in such big steps that it looks like the player would lagg. Here to the importante Code from the 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);
}
}
}
btw I am only using the linear velocity. Does anyone have a solution for this? Thanks for your help!
回答1:
I went ahead and looked through the repository for this engine and I found a class that may be a help to you. The base engine class has an extension called Fixed Step Engine that should allow you to control the delta time per frame, which may be worth trying out (if you haven't already) for smoother physics.
回答2:
In combination with FixedStepPhysicsWorld you will get the best possible result but: There is a small "mistake" inside the FixedStepPhysicsWorld class:
Change onUpdate as follows:
...
while(this.mSecondsElapsedAccumulator >= stepLength && stepsAllowed > 0) {
world.step(stepLength, velocityIterations, positionIterations);
this.mSecondsElapsedAccumulator -= stepLength;
stepsAllowed--;
}
this.mPhysicsConnectorManager.onUpdate(pSecondsElapsed);
to
...
while(this.mSecondsElapsedAccumulator >= stepLength && stepsAllowed > 0) {
world.step(stepLength, velocityIterations, positionIterations);
this.mSecondsElapsedAccumulator -= stepLength;
stepsAllowed--;
this.mPhysicsConnectorManager.onUpdate(stepLength);
}
The problem in the original code is that the PhysicsConnectors get updated after the PhysicsWorld made multiple steps (most of the cases). My code fixes this issue. Whenever the UiThread draws the connected entities, they are always at the position where their connected bodies are. They do not skip a Physics-step.
来源:https://stackoverflow.com/questions/33219182/andengine-physicshandler-makes-player-look-laggy