nicolasgramlich/AndEnginePhysicsBox2DExtension — Issue #3: Body maximal speed = framerate*2
Author: JFixbi
Created: 2012-02-17 16:04:15+00:00
State: open

I'm using FixedStepPhysics with the number of steps defined in the
int SUPER_NUMBER.

Than I take a simple body and drop it in the gravity field. Starting from 0 speed it accelerating until reaches maximal speed
= SUPER_NUMBER*2;

SUPER_NUMBER = 30 gives the maximal velocity of the body = 60
SUPER_NUMBER = 40 gives the maximal velocity of the body = 80
and so on.

Below there is the full code.

Moreover, using PhysicsWorld + FixedStepEngine this bug(?) gives the maximal body speed equal to 2*(frame rate).

package physics_bug.physics_bug;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.sensor.accelerometer.AccelerometerData;
import org.andengine.sensor.accelerometer.IAccelerometerListener;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.Toast;

public class Physics_bugActivity extends SimpleBaseGameActivity implements
IUpdateHandler {

private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;

private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mBoxFaceTextureRegion;
private Scene mScene;
private PhysicsWorld mPhysicsWorld;
Camera camera;

@Override
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}

@Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("");

this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 64,
TextureOptions.BILINEAR);
this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(this.mBitmapTextureAtlas, this,
"face_box_tiled.png", 0, 0, 2, 1); // 64x32
this.mBitmapTextureAtlas.load(this.getTextureManager());
}

@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());

this.mScene = new Scene();
this.mScene.setBackground(new Background(0, 0, 0));

this.mPhysicsWorld = new FixedStepPhysicsWorld(SUPER_NUMBER, new Vector2(0,
SensorManager.GRAVITY_EARTH), false, 3, 2);

this.mScene.registerUpdateHandler(this.mPhysicsWorld);

this.mScene.registerUpdateHandler(this);

final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1,
0.5f, 0.5f);

face = new AnimatedSprite(100, 100, this.mBoxFaceTextureRegion,
this.getVertexBufferObjectManager());
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face,
BodyType.DynamicBody, objectFixtureDef);

face.animate(200);

this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,
body, true, true));
camera.setChaseEntity(face);

return this.mScene;
}

AnimatedSprite face;
Body body;
private Vector2 v;
long t;

@Override
public void onUpdate(float pSecondsElapsed) {
// TODO Auto-generated method stubbody
if (System.currentTimeMillis() - t > 2000) {
t = System.currentTimeMillis();

v = body.getLinearVelocity();
Debug.e("linear speed Vx=" + v.x + " Vy=" + v.y);
}

}

@Override
public void reset() {
}

}