我有一个游戏,我用陀螺仪控制几个街区。现在的问题是我在我的 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。有人可以告诉我我错过了什么吗?