I have a problem with physics in AndEngine. My sprite shakes while moving and sometimes jumps. I made my ‘floor’ of boxes 32×32 and everyone is another body, so I thought this is the issue (some collision with their corners or something). I’ve tried to use one shape instead of those boxes, but the problem remains.
It looks like the surfece of my floor is not straight but somehow irregular…
My question is, how to make my character move smoothly on this surface?
And by the way, how to make my character heavier?
My whole code is here:
private final int CAMERA_WIDTH = 800;
private final int CAMERA_HEIGHT = 480;
enum PlayerAction {
RUN,
NONE,
JUMP
}
enum Direction {
RIGHT,
LEFT,
NONE
}
enum BodyId {
PLAYER,
BOX
}
Direction mPlayerDirection = Direction.NONE;
Direction mLastPlayerDirection = Direction.NONE;
private Camera mCamera;
private Scene mGameScene;
private PhysicsWorld mPhysicsWorld;
private BitmapTextureAtlas mBoxBitmapTextureAtlas;
private BitmapTextureAtlas mPlayerBitmapTextureAtlas;
private ITextureRegion mBoxTextureRegion;
private TiledTextureRegion mPlayerTextureRegion;
private AnimatedSprite mPlayer;
private Body mPlayerBody;
private BitmapTextureAtlas mOnScreenControlTexture;
private ITextureRegion mOnScreenControlBaseTextureRegion;
private ITextureRegion mOnScreenControlKnobTextureRegion;
private AnalogOnScreenControl mAnalogOnScreenControl;
@Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.mCamera);
mEngineOptions.getTouchOptions().setNeedsMultiTouch(true);
return mEngineOptions;
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBoxBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
this.mBoxTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBoxBitmapTextureAtlas, this, "box.png", 0, 0);
this.mBoxBitmapTextureAtlas.load();
this.mPlayerBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mPlayerBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
this.mPlayerBitmapTextureAtlas.load();
this.mOnScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
this.mOnScreenControlTexture.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
this.mGameScene = new Scene();
initPhysics(this.mGameScene);
for(int i = 0; i <= 50; i++) {
Sprite sprite = new Sprite(i*32, this.mCamera.getHeight() - this.mBoxTextureRegion.getHeight(), this.mBoxTextureRegion, this.getVertexBufferObjectManager());
createBody(sprite, BodyType.StaticBody, 1, 0, 0, BodyId.BOX);
this.mGameScene.attachChild(sprite);
}
/* Calculate the coordinates for the face, so its centered on the camera. */
final float playerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getWidth()) / 2;
final float playerY = CAMERA_HEIGHT - this.mPlayerTextureRegion.getHeight() - 128;
this.mPlayer = new AnimatedSprite(playerX, playerY, this.mPlayerTextureRegion, this.getVertexBufferObjectManager()) {
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
super.onManagedUpdate(pSecondsElapsed);
switch (mPlayerDirection) {
case RIGHT:
if (!mPlayerDirection.equals(mLastPlayerDirection)) {
mLastPlayerDirection = Direction.RIGHT;
GameActivity.this.mPlayer.animate(new long[]{200, 200, 200}, 3, 5, true);
}
break;
case LEFT:
if (!mPlayerDirection.equals(mLastPlayerDirection)) {
mLastPlayerDirection = Direction.LEFT;
GameActivity.this.mPlayer.animate(new long[]{200, 200, 200}, 9, 11, true);
}
break;
case NONE:
mLastPlayerDirection = Direction.NONE;
GameActivity.this.mPlayer.setCurrentTileIndex(6);
GameActivity.this.mPlayer.stopAnimation();
break;
default:
mLastPlayerDirection = Direction.NONE;
GameActivity.this.mPlayer.setCurrentTileIndex(6);
GameActivity.this.mPlayer.stopAnimation();
break;
}
}
};
this.mPlayer.setScaleCenterY(this.mPlayerTextureRegion.getHeight());
this.mPlayer.setScale(2);
this.mGameScene.attachChild(this.mPlayer);
this.mCamera.setChaseEntity(mPlayer);
this.mPlayerBody = createMovingBody(this.mPlayer, BodyType.DynamicBody, 1, 0, 0, BodyId.PLAYER);
this.mAnalogOnScreenControl = new AnalogOnScreenControl(32, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight() - 32, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 200, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
@Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
GameActivity.this.mPlayerBody.setLinearVelocity(new Vector2(pValueX*10, GameActivity.this.mPlayerBody.getLinearVelocity().y));
if (pValueX > 0)
GameActivity.this.mPlayerDirection = Direction.RIGHT;
else if (pValueX < 0)
GameActivity.this.mPlayerDirection = Direction.LEFT;
else
GameActivity.this.mPlayerDirection = Direction.NONE;
}
@Override
public void onControlClick(
AnalogOnScreenControl pAnalogOnScreenControl) {
GameActivity.this.mPlayerBody.setLinearVelocity(new Vector2(GameActivity.this.mPlayerBody.getLinearVelocity().x, -8.0f));
}
});
this.mAnalogOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
this.mAnalogOnScreenControl.getControlBase().setAlpha(0.2f);
this.mAnalogOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mAnalogOnScreenControl.getControlBase().setScale(1.25f);
this.mAnalogOnScreenControl.getControlKnob().setScale(1.25f);
this.mAnalogOnScreenControl.refreshControlKnobPosition();
this.mGameScene.setChildScene(this.mAnalogOnScreenControl);
pOnCreateSceneCallback.onCreateSceneFinished(this.mGameScene);
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
this.mEngine.registerUpdateHandler(new TimerHandler(3f, true, new ITimerCallback() {
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
// TODO Auto-generated method stub
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
private void initPhysics(Scene pScene)
{
mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
pScene.registerUpdateHandler(mPhysicsWorld);
}
private Body createBody(Sprite pSprite, BodyType pBodyType, float pDensity, float pElasticity, float pFriction, BodyId pBodyId)
{
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(pDensity, pElasticity, pFriction);
Body body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, pSprite, pBodyType, objectFixtureDef);
body.setUserData(pBodyId);
return body;
}
private Body createMovingBody(Sprite pSprite, BodyType pBodyType, float pDensity, float pElasticity, float pFriction, BodyId pBodyId) {
Body body = createBody(pSprite, pBodyType, pDensity, pElasticity, pFriction, pBodyId);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(pSprite, body, true, false));
return body;
}
I believe this could be caused by insufficient CPU speed. When the processor can’t keep up with the demands of Box2D, the simulation suffers. The bodies can fall a little bit into the space occupied by floor and are pushed upwards in the next simulation cycle. This also happens when some background application causes the CPU load to spike.
Try running the application on a faster phone or one without other applications running in background. I solved similar problem using the great MaxStepPhysicsWorld, which can be found here: http://www.andengine.org/forums/features/max-step-physics-to-avoid-choppyness-and-bad-simulation-t1271.html
The way it works is, when the engine is unable to perform enough simulation steps per second, MaxStepPhysicsWorld gives up on being real-time and rather performs enough simulation steps per “second” thus ensuring good simulation.