简体   繁体   English

使用Box2d在AndEngine中发生碰撞时该怎么办

[英]What to do on collision in AndEngine with Box2d

In Android with AndEngine and Box2d extension, I use the following code to create sprites and set their attributes (physics etc.): 在具有AndEngine和Box2d扩展名的Android中,我使用以下代码创建子画面并设置其属性(物理等):

mPlayer1_Sprite = new Sprite(0, BASELINE_Y, mResourcesManager.mPlayer1_TextureRegion, mVertexManager) {
    @Override
    protected void preDraw(GLState pGLState, Camera pCamera) {
        super.preDraw(pGLState, pCamera);
        pGLState.enableDither();
    }
};
Body body = PhysicsFactory.createBoxBody(mPhysicsWorld, mPlayer1_Sprite, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(10.0f, 0.0f, 0.5f));
body.setUserData(BODY_TYPE_PLAYER_1);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mPlayer1_Sprite, body, true, false));
mPlayer1_Sprite.setUserData(body);

(same code for other players and game objects) (其他玩家和游戏对象的代码相同)

Now, for collisions, I use this piece of code: 现在,对于冲突,我使用以下代码:

private boolean isCollision(final Fixture x1, final Fixture x2, final String type1, final String type2) {
    return (x1.getBody().getUserData() != null && x2.getBody().getUserData() != null) && ((x1.getBody().getUserData().equals(type1) && x2.getBody().getUserData().equals(type2)) || (x1.getBody().getUserData().equals(type2) && x2.getBody().getUserData().equals(type1)));
}

@Override
public void beginContact(Contact contact) {
    final Fixture x1 = contact.getFixtureA();
    final Fixture x2 = contact.getFixtureB();
    if (isCollision(x1, x2, BODY_TYPE_PLAYER_1, BODY_TYPE_PLAYER_2)) {
        ((Body) mPlayer1_Sprite.getUserData()).applyForce(3.0f, 1.0f, 0, 0);
    }
    if (isCollision(x1, x2, BODY_TYPE_PLAYER_1, BODY_TYPE_WALL)) {
        ((Body) mPlayer1_Sprite.getUserData()).applyForce(3.0f, 1.0f, 0, 0);
    }
    if (isCollision(x1, x2, BODY_TYPE_PLAYER_2, BODY_TYPE_WALL)) {
        ((Body) mPlayer1_Sprite.getUserData()).applyForce(3.0f, 1.0f, 0, 0);
    }
}

@Override
public void endContact(Contact contact) { }

@Override
public void preSolve(Contact contact, Manifold oldManifold) { }

@Override
public void postSolve(Contact contact, ContactImpulse impulse) { }

That should be correct so far, right? 到目前为止应该是正确的,对吗? But what should I do if I detected the collision here? 但是,如果我在这里检测到碰撞,该怎么办? Is applying a force to the body correct? 对身体施加力正确吗? And what values do I need to use if I want that: 如果需要,我需要使用哪些值:

  • Players both bounce back if they collide with each other. 如果玩家彼此碰撞,它们都会反弹。
  • Players that run against a wall bounce back as well. 撞墙的玩家也会反弹。
  • When colliding with the ground, the player stops, but only one time, as the player is then standing on the ground you should not apply forces continually to the player, should you? 当与地面碰撞时,玩家会停下来,但是只有一次,因为玩家随后会站在地面上,因此您不应该持续地向玩家施加力量,对吗?

Box2d is applying physical updates (collisions, friction, gravity etc.) anyway, right? Box2d仍在应用物理更新(碰撞,摩擦,重力等),对吗? So what do you do if you want to add custom physical calculations? 那么,如果要添加自定义物理计算,该怎么办? Do you have to switch off Box2d's calculations and use it for collision detection only? 您是否必须关闭Box2d的计算并将其仅用于碰撞检测?

since you don't want box2d to calculate the collision physically (at least for some combinations) you should filter these collisions and calculate them (as you already do). 由于您不希望box2d物理计算碰撞(至少对于某些组合),因此应过滤这些碰撞并进行计算(就像您已经做的那样)。 for collision filtering see this tutorial . 有关碰撞过滤的信息,请参阅本教程

physical calculated collisions would work the way, Physic works ;-) -> two objects collide and get an impulse (linear+angular) depending on their weight/density, the collision point, their speed and surface friction - meaning your bounce-back effect could result in rotations etc. 物理计算的碰撞会起作用,物理起作用;-)->两个物体碰撞并根据其重量/密度,碰撞点,它们的速度和表面摩擦而获得冲量(线性+角)-这意味着您的反弹效果可能导致旋转等。

since i think (and read from your other post), your world boundaries are static and your player(s) can move, i recommend you to use collision filtering (as mentioned above). 由于我认为(并从您的其他文章中阅读),您的世界边界是静态的,并且您的玩家可以移动,因此我建议您使用碰撞过滤(如上所述)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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