【问题标题】:Why AndEngine's CollisionHandler produces multiple events in one collision?为什么 AndEngine 的 CollisionHandler 在一次碰撞中产生多个事件?
【发布时间】:2012-08-23 08:20:46
【问题描述】:

我正在编写一个 摆动钟摆 的 GameActivity(在 AndEngine 中)。

在应用CollisionHandler 时,这将产生一系列碰撞事件 而不是一个??实际上,我希望它在精灵碰撞时执行一次

我的情况:当movingBallmLeftWall碰撞时,它调用5 times > onCollision();不正常,我预计只有一次。

我应该改用ContactListener 并使用PhysicsWorld 注册吗?这是我的代码:

MyGameActivity.java:

/** List of objects to check collision of any animating objects. E.g: walls  */
private ArrayList<IShape> mCollidingTargetList = new ArrayList<IShape>();

/** user-defined collision handler */
PendulumCollisionCallback collideActionCallback = new PendulumCollisionCallback();

@Override
public Scene onCreateScene() 
{
    final Scene scene = super.onCreateScene();

    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();

    //Create walls
    mWallLeft = new Rectangle(0, 0, 2, mCameraHeight, vertexBufferObjectManager);
    mWallRight = new Rectangle(mCameraWidth - 2, 0, 2, mCameraHeight, vertexBufferObjectManager);
    PhysicsFactory.createBoxBody(mPhyscisWorld, mWallLeft, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(mPhyscisWorld, mWallRight, BodyType.StaticBody, mWallFixtureDef);
    scene.attachChild(mWallLeft);
    scene.attachChild(mWallRight);

    //Create movingBall - the pendulum ball
    final AnimatedSprite movingBall = new AnimatedSprite(mCenterX, mCenterY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
    final Body ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, movingBall, BodyType.DynamicBody, mObjectFixtureDef);
    movingBall.setUserData(ballBody); // for getting the body attached with UI

    //..... Stuff to create anchorBody and RevoluteJoint with the movingBall

    //list of bodies where sprites collide
    this.mCollidingTargetList.add(mWallLeft);
    this.mCollidingTargetList.add(mWallRight);

    //Manage user-defined collision handler: movingBall collides with walls
    CollisionHandler pendulumCollideHandler = new CollisionHandler(collideActionCallback, movingBall, mCollidingTargetList);
    scene.registerUpdateHandler(pendulumCollideHandler);

    return scene;
}

PendulumCollisionCallback.java:

public class PendulumCollisionCallback implements ICollisionCallback
{
    public PendulumCollisionCallback()
    { }

    /**
     * @param animatedShape
     *      Entity to check collision with other targets
     * @param pTargetShape
     *      Target to check the collision
     * @return <code>true</code> to proceed, <code>false</code> to stop further collosion-checks.
     */
    @Override
    public boolean onCollision(IShape animatedShape, IShape pTargetShape) 
    {
        String pendulPos = String.format("Pendul:x=%f, y=%f", animatedShape.getX(), animatedShape.getY());      
        Log.d("COLLIDE", pendulPos);
        return false;
    }
}

【问题讨论】:

    标签: java android collision-detection andengine


    【解决方案1】:

    我发现了问题。这是因为 Physics Handler 和 Scene 的 UpdateHandler 工作序列是分开的。

    虽然PhysicsWorld 仍在控制身体碰撞,但身体仍在向前移动几帧 => 以便 UI 精灵(与此身体相连)的位置与碰撞对象相交。在这些帧中,updateHandler 通过CallbackHandler 产生碰撞事件。

    解决方案是添加 布尔标志 的一种棘手方法,该标志在第一次碰撞时 设置 并且在结束碰撞时 取消设置(尽管这不是不错)。

    P/S:我从 FPSLogger 观察到的帧速率是 ~88,9 FPS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多