AndEngine Body奇怪的移动加速度计

时间:2013-07-22 20:54:16

标签: android box2d andengine

首先抱歉我的英语不好,我是法国人。

我想分享一下我的问题。几个星期后我使用AndEngine,这是我第一次看到它。

我使用加速度计将我的播放器从左向右移动。问题是他采用了一种奇怪的物理定律。 (使用AndEngine物理法)

有一种迟到的beetwin身体动作和虚拟玩家移动。还有一种发音的惯性使得这些动作很难在手中拿走。我只想要经典的加速度计移动系统

这是我的代码

public class Jeu extends SimpleBaseGameActivity implements IAccelerationListener {

// Paramètres
// ==================================
    private int CAMERA_WIDTH = 480;
    private int CAMERA_HEIGHT = 800;
    private Background FOND_BACKGROUND = new Background(255,255,255);
// ==================================

    private BitmapTextureAtlas mBitmapTextureAtlas;
    private BitmapTextureAtlas mBitmapTextureAtlas2;
    private ITextureRegion joueurRegion;
    private ITextureRegion plateformeRegion;
    private Scene mScene;
    private Camera camera;
    private PhysicsWorld mPhysicsWorld;
    private Personnage joueur;
    private Plateforme plateforme[] = new Plateforme[20];
    private Sprite joueurSprite;
    private Font font;
    private Text distance;
    private Float distanceM = 0f;
    private Entity e;
    private HUD hud;
    private float OldEtat = 0;
    private float CurrentEtat;

    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub

        final CroppedResolutionPolicy canvasSurface = new CroppedResolutionPolicy( CAMERA_WIDTH, CAMERA_HEIGHT );
        this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, canvasSurface, camera);
    }

    @Override
    protected void onCreateResources() {
        // TODO Auto-generated method stub


        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        final BitmapTextureAtlas fontTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
        font = new Font(this.getFontManager(), fontTexture, Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), 24, true, Color.BLACK);
        mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mBitmapTextureAtlas2 = new BitmapTextureAtlas(this.getTextureManager(), 128, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        joueurRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, getAssets(), "personnage.png", 0, 0);
        plateformeRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas2, getAssets(), "plateforme.png", 0, 0);
        mBitmapTextureAtlas.load();
        mBitmapTextureAtlas2.load();
        fontTexture.load();
        font.load();
        enableAccelerationSensor(this);
        Log.i("Jump", "RESSOURCE LOADED");

    }

    @Override
    protected Scene onCreateScene() {
        // TODO Auto-generated method stub
        this.mScene = new Scene();
        this.mScene.setBackground(FOND_BACKGROUND);
        this.mEngine.registerUpdateHandler(new FPSLogger());
        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
        this.e = new Entity();
        this.e.setPosition(CAMERA_WIDTH/2, 0);
        this.camera.setChaseEntity(e);

        for (int i=0;i<5;i++)
        {
        plateforme[i] = new Plateforme(200, 500-i*120, plateformeRegion, this.getVertexBufferObjectManager(), 1.0f, "BASE");
        this.mScene.attachChild(plateforme[i]);
        }
        joueurSprite = new Sprite(200, -20, joueurRegion, this.getVertexBufferObjectManager());

        joueur = new Personnage(this.mPhysicsWorld, joueurSprite, "DESCENTE", 1.0f, 1.0f, 0f); // three last numbers are the PhysicsFactory.createFixtureDef(1.0f, 1.0f, 0f)
        distance = new Text(30, 10, font, distanceM+"m", 30, this.getVertexBufferObjectManager()); // there is also a setLinearDamping(1.2f);

        this.mScene.attachChild(joueur.getSprite());
        this.hud = new HUD();
        this.hud.attachChild(distance);
        this.camera.setHUD(hud);

        this.mEngine.registerUpdateHandler(new IUpdateHandler() {

            @Override
            public void onUpdate(float pSecondsElapsed) {
                // TODO Auto-generated method stub
                distanceM = -joueur.getSprite().getY()/100;
                if (distanceM <0)
                    distanceM = 0f;
                e.setY(joueur.getSprite().getY());
                CurrentEtat = joueur.getSprite().getY();
                if (OldEtat == 0)
                    OldEtat = joueur.getSprite().getY();
                else
                    CurrentEtat = joueur.getSprite().getY();




                Log.i("Jump Old", String.valueOf(OldEtat));
                Log.i("Jump Curr", String.valueOf(CurrentEtat));
                if (OldEtat < CurrentEtat)
                    joueur.setEtat("DESCENTE");
                else if (OldEtat > CurrentEtat)
                    joueur.setEtat("MONTEE");
                if (OldEtat != CurrentEtat)
                    OldEtat = CurrentEtat;
                //Log.i("Jump", String.valueOf(e.getY()));

            }

            @Override
            public void reset() {
                // TODO Auto-generated method stub

            }
        });

        this.mEngine.registerUpdateHandler(new IUpdateHandler() {

            @Override
            public void onUpdate(float pSecondsElapsed) {
                // TODO Auto-generated method stub
                for (int i=0;i<5;i++)

                    if (joueur.collidWith(plateforme[i]) && joueur.getEtat() == "DESCENTE")
                    {
                        joueur.Jump(plateforme[i]);
                        Log.i("Jump", "COLLISION");
                    }
            }

            @Override
            public void reset() {
                // TODO Auto-generated method stub

            }

        });

        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(joueur.getSprite(), joueur.getBody(), true, false));
        this.mScene.registerUpdateHandler(this.mPhysicsWorld);
        return mScene;
    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub
        final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX()*8, SensorManager.GRAVITY_EARTH);
        this.mPhysicsWorld.setGravity(gravity);
        Vector2Pool.recycle(gravity);

        //Log.i("Jump", String.valueOf(pAccelerationData.getX()));
        //Log.i("Jump", "["+String.valueOf(joueur.getBody().getPosition().x*PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT)+":"+String.valueOf(joueur.getBody().getPosition().y*PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT)+"]");
        //Log.i("Jump", "["+String.valueOf(plateforme.getX())+":"+String.valueOf(plateforme.getY())+"]");

        String s = String.format("%.3f", distanceM);
        distance.setText(s+"m");

    }



}

0 个答案:

没有答案