物理身体通过每个

时间:2013-02-10 02:35:00

标签: android box2d andengine

我正在使用Andengine + Box2D开展一个小项目。我有一个玩家角色和一个地面.Ground是一个StaticBody,而ply​​aer是DynamicBody。我想要一个人在地上行走的效果。

问题是,当我移动播放器时,它正在地面下沉。我该如何解决?我必须用图片解释它。

This is how it should be

This is what is happenning

这是我的代码:

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
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.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

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.MassData;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;

public class BlokKirmaActivity extends SimpleBaseGameActivity {

private EngineOptions engineOptions;
private Camera camera;
private Scene oyunSahne;
private static final int CAMERA_HEIGHT = 480;
private static final int CAMERA_WIDTH = 800; 
private PhysicsWorld physicsWorld= new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH),false);
private FixtureDef fixDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 1.0f);
private BitmapTextureAtlas texYer,texKamil,texSol,texSag,texZipla;
private TextureRegion texRegYer,texRegKamil,texRegSol,texRegSag,texRegZipla;
private Sprite spriteYer,spriteKamil,spriteSol,spriteSag,spriteZipla;
private Body bodyYer,bodyKamil;
private boolean solBasilimi=false,sagBasilimi=false,ziplaBasilimi=false;

public EngineOptions onCreateEngineOptions() {
    camera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
    engineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),camera);

    engineOptions.getTouchOptions().setNeedsMultiTouch(true);
    engineOptions.getAudioOptions().setNeedsSound(true);

    return this.engineOptions;

}

@Override
protected void onCreateResources() {

    texYer = new BitmapTextureAtlas(this.getTextureManager(),1024,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texKamil = new BitmapTextureAtlas(this.getTextureManager(),64,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texSol = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texSag = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texZipla = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texYer.load();
    texKamil.load();
    texSol.load();
    texSag.load();
    texZipla.load();

    texRegYer = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texYer, this, "gfx/oyunyer.png",0,0);
    texRegKamil = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texKamil, this, "gfx/kamil.png",0,0);
    texRegSol = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texSol, this, "gfx/sol.png",0,0);
    texRegSag = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texSag, this, "gfx/sag.png",0,0);
    texRegZipla = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texZipla, this, "gfx/zipla.png",0,0);

}

@Override
protected Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());
    this.oyunSahne = new Scene();
    this.oyunSahne.registerUpdateHandler(physicsWorld);

    spriteYer = new Sprite(0,355,texRegYer,getVertexBufferObjectManager());
    spriteKamil = new Sprite(220,50,texRegKamil,getVertexBufferObjectManager()){

        protected void onManagedUpdate(float pSecondsElapsed){
            if(solBasilimi==true){
                bodyKamil.setLinearVelocity(-10, 0);
            }
            if(sagBasilimi==true){
                bodyKamil.setLinearVelocity(10, 0);
            }

        }

    };

    spriteSol = new Sprite(100,50,texRegSol,getVertexBufferObjectManager()){

        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {

            if(pSceneTouchEvent.isActionDown()){
                solBasilimi=true;
            }
            if(pSceneTouchEvent.isActionUp()){
                solBasilimi=false;
            }

            return true;
        }
    };

    spriteSag = new Sprite(300,50,texRegSag,getVertexBufferObjectManager()){

        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {

            if(pSceneTouchEvent.isActionDown()){
                sagBasilimi=true;
            }
            if(pSceneTouchEvent.isActionUp()){
                sagBasilimi=false;
            }
            return true;

        }
    };

    spriteZipla = new Sprite(600,50,texRegZipla,getVertexBufferObjectManager()){

        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {

            if(pSceneTouchEvent.isActionDown()){
                bodyKamil.setLinearVelocity(0,-20);

            }
            if(pSceneTouchEvent.isActionUp()){
                bodyKamil.setLinearVelocity(0,0);
            }
            return true;
        }
    };

    bodyYer = PhysicsFactory.createBoxBody(physicsWorld, spriteYer, BodyType.StaticBody, fixDef);
    this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spriteYer, bodyYer,true,false)); 
    bodyKamil = PhysicsFactory.createBoxBody(physicsWorld, spriteKamil, BodyType.DynamicBody, fixDef);
    this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spriteKamil, bodyKamil,true,false)); 



    MassData massData1 = bodyKamil.getMassData();
    massData1.mass= 75;
    bodyKamil.setMassData(massData1);

    this.oyunSahne.attachChild(spriteYer);
    this.oyunSahne.attachChild(spriteKamil);
    this.oyunSahne.attachChild(spriteSol);
    this.oyunSahne.attachChild(spriteSag);
    this.oyunSahne.attachChild(spriteZipla);

    this.oyunSahne.registerTouchArea(spriteSol);
    this.oyunSahne.registerTouchArea(spriteSag);
    this.oyunSahne.registerTouchArea(spriteZipla);

    return this.oyunSahne;
}


}

1 个答案:

答案 0 :(得分:0)

很可能这是因为你的物理身体的大小或形状与你的精灵不同。 我不知道任何简单的方法来修改它。可能你必须为精灵创建自己的形状。 但是这里有一个很好的工具box2d调试渲染器使用它你可以获得你的物理体的轮廓。 Here is the link

它的使用非常简单,只需将项目作为库包含,然后只需添加一行代码

即可
mScene.attachChild(new Box2dDebugRenderer(mPhysicsWorld));

现在你的所有物理体都会以轮廓显示, 请记住,这将减少你的FPS,但对调试非常有帮助。