0
votes

I have a game, where with the gyroscope, i control a couple of blocks. Now the problem is that i have inplemented box2d in my andengine code, but the bodies do not seem to react one at another, causing them to overlap, instead of actually doing the physics stuff.

In my onCreateScene i have this:

@Override
protected Scene onCreateScene() {
    this.mMainScene = new Scene();
    backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());

physicsWorld = new PhysicsWorld(new Vector2(0, 0), false);

final Player oPlayer = new Player(centerX, centerY, this.goodTiledTextureRegion, this.getVertexBufferObjectManager(), MainActivity.this, playerID, 0);
    player_fix = PhysicsFactory.createFixtureDef(10.0f, 0.2f, 1.0f);
    body = PhysicsFactory.createBoxBody(physicsWorld, oPlayer, BodyType.DynamicBody, player_fix);
    oPlayer.setBody(body);
    playerID++;
    players.add(oPlayer);
    for (Player player : players) {
        player.setPlayers(players);
        mMainScene.attachChild(player);
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(player, body, true, false));
        mMainScene.registerTouchArea(player);
    }

this.mMainScene.registerUpdateHandler(physicsWorld);
this.mMainScene.registerUpdateHandler(new TimerHandler(0.1f, true, new ITimerCallback() {
        @Override
        public void onTimePassed(final TimerHandler pTimerHandler) {
                       Player player = new Player(random, 5, goodTiledTextureRegion, getVertexBufferObjectManager(), MainActivity.this, playerID, 0);
                body = PhysicsFactory.createBoxBody(physicsWorld, player, BodyType.DynamicBody, player_fix);
                            player.setBody(body);
                            players.add(player);
mMainScene.detachChildren();
                    mMainScene.attachChild(backgroundSprite);
                    for (Player player : players) {
                        player.setPlayers(players);
                        mMainScene.attachChild(player);
                        player.body.setLinearVelocity(AccelerometerHelper.TILTX * 5, AccelerometerHelper.TILTY * 5);
                        physicsWorld.registerPhysicsConnector(new PhysicsConnector(player, body, true, false));
                        mMainScene.registerTouchArea(player);
                    }
 }

return this.mMainScene;

}

Now, I create the scene, I create the physics world. Then i create my first "player", (this is a sprite, that i move). I create a body for it, and also set that body in my Player.class (entity). to have access to every players body, from it's entity. Then on every a couple of updates (the code is a bit longer, but i took out the stuff that doesn't have anything to do with physics) I create a new player. and then for every player in my arraylist (players), i set the linear velocity after the accelerometer, to make it move. The problem is that if a player hits another one, they overlap, and do not react at the impact.

the player fixture is the same everywhere player_fix = PhysicsFactory.createFixtureDef(10.0f, 0.2f, 1.0f); And the BodyType is always DynamicBody. Can somebody tell me what I am missing?

1
Why do you check the collision? Is something special going to happen when they collide. The box2dphysichsenxtension should be able to let you create boxes which will stop on collision without the need for manual code to check this. - CodeTower
searching for an answer, i heard a lot about box2dphysichs, but how do I integrate it in my program? - rosu alin
You use the same Body for each player. That means all player gets connected to the same physical body. Your code is a bit confusing due. Do you create more than one player? In the updateHandler you add all the players in the players collection again, i'm not sure thats what you want. A entity, unless removed, should only be added once to the scene and to the physicsworld. - CodeTower
I added some modified code in my answer. Try see if it helps. - CodeTower
You might need to ask the MASKBIT that i use in my code. Though as i rememeber the default behaviour is that the bodys collide. - CodeTower

1 Answers

0
votes

You can download it from http://code.google.com/p/andenginephysicsbox2dextension/.

I use Eclipse, and added it as a project. I then included it into my project. I also recommend the project AndEngineExamples, it shows you all kinds of nice tools.

Here is how i add Walls:

final Rectangle wall = new Rectangle(pX, pY, pWidth, pHeight,  pVman); // pvman is the ObjectVertexBufferManager 
wall.setIgnoreUpdate(true);


Body body = PhysicsFactory.createBoxBody(
    pParent.mPhysicsWorld,
    wall, 
    BodyType.StaticBody,
    Environment.WALL_FIXTURE_DEF
  );
  pScene.getFirstChild().attachChild(wall);
  pParent.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(wall, body, true, true));
  // pParent is of type GameActivity.

The WALL_FIXTURE_DEF is:

     /* The categories. */
public static final short CATEGORYBIT_WALL = 1;
public static final short CATEGORYBIT_MONNING = 2;
public static final short CATEGORYBIT_WHEEL = 4;

/* And what should collide with what. */
public static final short MASKBITS_WALL = CATEGORYBIT_WALL + CATEGORYBIT_MONNING + CATEGORYBIT_WHEEL;
public static final short MASKBITS_MONNING = CATEGORYBIT_WALL; 
public static final short MASKBITS_WHEEL = CATEGORYBIT_WALL; 

public static final FixtureDef WALL_FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0f, 0.9f, false, CATEGORYBIT_WALL, MASKBITS_WALL, (short)0);
public static final FixtureDef MONNING_FIXTURE_DEF = PhysicsFactory.createFixtureDef(10, 0f, 0f, false, CATEGORYBIT_MONNING, MASKBITS_MONNING, (short)0);
public static final FixtureDef WHEEL_FIXTURE_DEF = PhysicsFactory.createFixtureDef(20, 0f, 10f, false, CATEGORYBIT_WHEEL, MASKBITS_WHEEL, (short)0);
public static final float GRAVITY_MONNINGS = 2*SensorManager.GRAVITY_EARTH;

This also handles collisions using the MASK_BITS. Monnings and Wheels should not collide with each other, but they should collide with Walls.

The Physics world is created as this in the createScene function:

this.mPhysicsWorld = new FixedStepPhysicsWorld(Environment.FPS, new Vector2(0, Environment.GRAVITY_MONNINGS), false);

Again the AndEngineExamples project (http://code.google.com/p/andengineexamples/) is a must to have installed.

This might be the code you need. Note that i have not run the code so its might contain error but i hope you can get the idea: (Sorry for indent)

@Override
protected Scene onCreateScene() {
   this.mMainScene = new Scene();
   backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion,   getVertexBufferObjectManager());

    physicsWorld = new PhysicsWorld(new Vector2(0, 0), false);

   final Player oPlayer = new Player(centerX, centerY, this.goodTiledTextureRegion, this.getVertexBufferObjectManager(), MainActivity.this, playerID, 0);
   player_fix = PhysicsFactory.createFixtureDef(10.0f, 0.2f, 1.0f);

   oPlayer.setBody(body);
   playerID++;
   players.add(oPlayer);
   for (Player player : players) {

      Body body = PhysicsFactory.createBoxBody(physicsWorld, oPlayer, BodyType.DynamicBody, player_fix);
      player.setPlayers(players);
      mMainScene.attachChild(player);
      physicsWorld.registerPhysicsConnector(new PhysicsConnector(player, body, true, false));
      mMainScene.registerTouchArea(player);
}

this.mMainScene.registerUpdateHandler(physicsWorld);
this.mMainScene.registerUpdateHandler(new TimerHandler(0.1f, true, new ITimerCallback()  {
    @Override
    public void onTimePassed(final TimerHandler pTimerHandler) {
             Player player = new Player(random, 5, goodTiledTextureRegion, getVertexBufferObjectManager(), MainActivity.this, playerID, 0);
             Body body = PhysicsFactory.createBoxBody(physicsWorld, player, BodyType.DynamicBody, player_fix);
             player.setBody(body);
             players.add(player);
             player.setPlayers(players);
             mMainScene.attachChild(player);

             /* This might have to be in the loop */
             player.body.setLinearVelocity(AccelerometerHelper.TILTX * 5, AccelerometerHelper.TILTY * 5);
            physicsWorld.registerPhysicsConnector(new PhysicsConnector(player, body, true, false));
            mMainScene.registerTouchArea(player);
       }
   }

 return this.mMainScene;

}