我在 AndEngine 中遇到了物理问题。我的精灵在移动时会颤抖,有时还会跳跃。我制作了 32x32 的盒子“地板”,每个人都是另一个身体,所以我认为这是问题所在(与他们的角落或其他东西发生碰撞)。我尝试使用一种形状而不是那些盒子,但问题仍然存在。
看起来我的地板表面不是直的,而是不规则的......
我的问题是,如何让我的角色在这个表面上顺利移动?
顺便说一句,如何让我的角色更重?
我的整个代码在这里:
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;
}