0

我有一个游戏,我用陀螺仪控制几个街区。现在的问题是我在我的 andengine 代码中实现了 box2d,但身体似乎没有相互反应,导致它们重叠,而不是实际做物理的东西。

在我的 onCreateScene 我有这个:

@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;

}

现在,我创建场景,创建物理世界。然后我创建了我的第一个“玩家”,(这是我移动的精灵)。我为它创建了一个主体,并将该主体设置在我的 Player.class(实体)中。从它的实体中访问每个玩家的身体。然后每隔几次更新(代码有点长,但我取出了与物理无关的东西)我创建了一个新玩家。然后对于我的数组列表中的每个玩家(玩家),我在加速度计之后设置线速度,以使其移动。问题是,如果一名球员击中另一名球员,他们会重叠,并且不会对撞击做出反应。

播放器夹具在任何地方都是相同player_fix = PhysicsFactory.createFixtureDef(10.0f, 0.2f, 1.0f);的并且 BodyType 始终是 DynamicBody。有人可以告诉我我错过了什么吗?

4

1 回答 1

0

您可以从http://code.google.com/p/andenginephysicsbox2dextension/下载它。

我使用 Eclipse,并将其添加为项目。然后我将它包含在我的项目中。我还推荐项目 AndEngineExamples,它向您展示了各种不错的工具。

这是我添加墙的方法:

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.

WALL_FIXTURE_DEF 是:

     /* 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;

这也使用 MASK_BITS 处理冲突。Monnings 和 Wheels 不应该相互碰撞,但它们应该与 Walls 碰撞。

物理世界在 createScene 函数中是这样创建的:

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

AndEngineExamples 项目 ( http://code.google.com/p/andengineexamples/ ) 也是必须安装的。

这可能是您需要的代码。请注意,我没有运行代码,所以它可能包含错误,但我希望你能明白:(抱歉缩进)

@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;

}
于 2013-03-25T08:43:07.293 回答