简体   繁体   中英

Box2D Physics with Andengine not working on real devices but works on emulator

I know this is silly question but I can not getting any reson why its happening.I am new to android gaming and I am trying to create a simple ball falling scene using Andengine SDK.For this I used Box2D Extensions Physics to create ball falling effect for my ball sprite.However Its working fine on emulator but its not working on real devie,Ball is not falling.Here is my code:

package com.example.mygame;


import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
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.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.hardware.SensorManager;
import android.view.Display;

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


public class GameActivity extends BaseGameActivity{

    Scene scene;
    protected static int CAMERA_HEIGHT;
    protected static int CAMERA_WIDTH;
    BitmapTextureAtlas  BallTexture;
    ITextureRegion BallTextureRegion;
    PhysicsWorld physicsworld;
    @SuppressWarnings("deprecation")
    @Override
    public EngineOptions onCreateEngineOptions() {
        final Display display=getWindowManager().getDefaultDisplay();
        CAMERA_HEIGHT=display.getHeight();
        CAMERA_WIDTH=display.getWidth();
        Camera gameCamera=new Camera(0, 0, CAMERA_WIDTH,CAMERA_HEIGHT);
        EngineOptions options=new    EngineOptions(true,ScreenOrientation.PORTRAIT_FIXED,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),gameCamera);
        return options;
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)throws Exception 
    {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        BallTexture=new BitmapTextureAtlas(this.getTextureManager(),256,256,TextureOptions.BILINEAR);
        BallTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(BallTexture, getAssets(),"ball.png",0,0 );
        BallTexture.load();
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception 
    {
        scene=new Scene();  
        physicsworld=new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_JUPITER), true);  
        this.scene.setBackground(new Background(255, 23, 23));
        this.scene.registerUpdateHandler(this.physicsworld);
        pOnCreateSceneCallback.onCreateSceneFinished(this.scene);

    }

    @Override
    public void onPopulateScene(Scene pScene,OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception
    {
        Sprite ball=new Sprite(CAMERA_WIDTH/2,10,BallTextureRegion,this.mEngine.getVertexBufferObjectManager());
        final FixtureDef BallFixture=PhysicsFactory.createFixtureDef(10.0f, 1.0f,0.0f);
        Body body=PhysicsFactory.createCircleBody(this.physicsworld,ball, BodyType.DynamicBody, BallFixture);
        this.scene.attachChild(ball);
        physicsworld.registerPhysicsConnector(new PhysicsConnector(ball, body, true, true));
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }
}

Can anyone help me?,so that I can learn my Android gaming concept in a right way.Thanx in advance.

I suggest making a subclass of Scene because then you can controll all the stuff in that. so in

  @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception 
    {
        scene=new PhysicsScene();  


    }

Then in the physics scene handle all the stuff . This will allows the phone to run the code from the scene class that you made rather than from the main game acitivy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM