📜 ⬆️ ⬇️

Try AndEngine and physicsbox2d

In this post I want to talk about the existence of such an extension for AndEngine as physics2box.
AndEngine is a free Andtoid game engine, one of the most popular. For him, there are a number of extensions, one of them - physicsbox2d. This library allows you to create physics in the game world.

About AndEngine on Habré already perfectly wrote stepango . I enjoyed his work with pleasure, and, at the same time, screwed physicsbox2d. It turned out to be quite simple.

I do not undertake to tell about all, or even about some part of the capabilities of these two tools, but I will just tell interested people where to start. And start with this:

Create a standard project for Android, and add the library andengine.jar, andenginephysicsbox2dextension.jar. You will also need the armeabi folder with the files libandenginephysicsbox2dextension.so and armeabilibxmp.so. In Nebeans, it’s enough to put all this in the libs folder in the project. How to get all this - I will not describe, which will prevent idle curious, but will not stop really interested. Now you can proceed.
')
1. Inherit our Activity from AndEngine-based BaseGameActivity.
2. We implement the methods we need onLoadEngine and onLoadScene

Here is the code:
public class MainActivity extends BaseGameActivity { //      private static final int CAMERA_WIDTH = 800; //     private static final int CAMERA_HEIGHT = 480; //       private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); // ,       private Camera mCamera; // ,       private Scene mScene; //  ,    . private PhysicsWorld mPhysicsWorld; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } //   public Engine onLoadEngine() { DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); //      ,  //      . RatioResolutionPolicy    // .      ,    return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); } public void onLoadResources() { } public Scene onLoadScene() { //   .    ,     , . mScene = new Scene(1); //      ,  . this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); //      mScene.registerUpdateHandler(mPhysicsWorld); return mScene; } public void onLoadComplete() { } } 


Run - and see a friendly vacuum. In our world, nothing yet.

2. Load the textures. Slightly improving the stepango example, we use static variables. Rumor has it that this is better. To do this, create a separate class:

 public class Textures { private Texture mTexture; private static TextureRegion mBallTextureRegion; private static TextureRegion mBackgroundTextureRegion; public Textures(final BaseGameActivity activity, final Engine engine) { //      -   ""   //  . mTexture = new Texture(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA); //   - 5121024. mBackgroundTextureRegion = TextureRegionFactory.createFromAsset( mTexture, activity, "gfx/bkg.png", 0, 0); //   -       // 512,0 -     . mBallTextureRegion = TextureRegionFactory.createFromAsset( mTexture, activity, "gfx/ball.png", 512, 0); engine.getTextureManager().loadTexture(mTexture); } public static TextureRegion getBackground() { return mBackgroundTextureRegion; } public static TextureRegion getBallTextureRegion() { return mBallTextureRegion; } } 


You need to initialize this object in activity in the appropriate method:

 public void onLoadResources() { //   mTextures = new Textures(this, getEngine()); } 


The fact that we are not working correctly with static objects is not the essence of the example. On good, here any singleton would approach.

3. Now we have textures. It's about time to create objects in the onLoadScene method - side walls, floor, and a ball.
The method is transformed as follows:

 public Scene onLoadScene() { //   .    ,     , . mScene = new Scene(1); //      ,  . this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); //      mScene.setBackground(new SpriteBackground(new Sprite(0, 0, Textures.getBackground()))); //   -   Line line_top = new Line(0, 0, CAMERA_WIDTH, 0, 5.0f * metrics.density); Line line_left = new Line(0, 0, 0, CAMERA_HEIGHT, 5.0f * metrics.density); Line line_right = new Line(CAMERA_WIDTH, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 5.0f * metrics.density); Line line_bottom = new Line(0, CAMERA_HEIGHT, CAMERA_WIDTH, CAMERA_HEIGHT, 5.0f * metrics.density); //      Body wall_top = PhysicsFactory.createLineBody(mPhysicsWorld, line_top, FIXTURE_DEF); Body wall_left = PhysicsFactory.createLineBody(mPhysicsWorld, line_left, FIXTURE_DEF); Body wall_right = PhysicsFactory.createLineBody(mPhysicsWorld, line_right, FIXTURE_DEF); Body wall_bottom = PhysicsFactory.createLineBody(mPhysicsWorld, line_bottom, FIXTURE_DEF); //    Sprite mSprite = new Sprite(0.0f, 0.0f, 100, 100, Textures.getBallTextureRegion()); //      mSprite.setPosition(100, 100); //     Body mBody = PhysicsFactory.createCircleBody(mPhysicsWorld, 100, 100, 50, 0, BodyType.DynamicBody, FIXTURE_DEF); //       this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(line_top, wall_top, true, true)); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(line_left, wall_left, true, true)); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(line_right, wall_right, true, true)); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(line_bottom, wall_bottom, true, true)); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mSprite, mBody, true, true)); //     this.mScene.attachChild(mSprite); this.mScene.attachChild(line_top); this.mScene.attachChild(line_left); this.mScene.attachChild(line_right); this.mScene.attachChild(line_bottom); mScene.registerUpdateHandler(mPhysicsWorld); return mScene; } 


That's all. When we start, we will have the background, and the ball at the top left. He will immediately begin to fall, fly to the end of the screen, bounce off ... and then everything will be as in life, without the intervention of a programmer.

After some time of acquaintance, I can say: physics2box is a powerful and interesting thing. There are problems with documentation and examples, but the more curious is the process of creating an application.

Here is a link to the stepango article that helped me in my research:
habrahabr.ru/blogs/android_development/120716

Source: https://habr.com/ru/post/132810/


All Articles