你如何不断检查Box2d机体之间的碰撞?

时间:2021-06-29 07:31:44

I am using Libgdx box2d and I need to constantly check collision between bodies, unlike on Libgdx where you can do rectangle.overlaps(rect1) on box2d you need to implement ContactListener which only have beginContact() and endContact(). I want to detect collision all the time and not just when it starts or end.

我正在使用Libgdx box2d,我需要不断检查主体之间的冲突,不像在Libgdx上你可以在box2d上执行rectangle.overlaps(rect1),你需要实现只有beginContact()和endContact()的ContactListener。我想一直检测碰撞,而不仅仅是它开始或结束时。

1 个解决方案

#1


1  

There is no need to check this every frame. Just set a boolean when contact is made and contact ends.

没有必要每帧检查一次。只需在联系人和联系人结束时设置布尔值。

public class MyBody {
    private boolean colliding;

    //...

    public void update()
    {
        if (beginContact()) colliding = true;
        else if (endContact) colliding = false;

        if (colliding)
        {
            System.out.println("I am colliding...");
        }
        else
        {
            System.out.println("I am not colliding...");
        }
    }
}

#1


1  

There is no need to check this every frame. Just set a boolean when contact is made and contact ends.

没有必要每帧检查一次。只需在联系人和联系人结束时设置布尔值。

public class MyBody {
    private boolean colliding;

    //...

    public void update()
    {
        if (beginContact()) colliding = true;
        else if (endContact) colliding = false;

        if (colliding)
        {
            System.out.println("I am colliding...");
        }
        else
        {
            System.out.println("I am not colliding...");
        }
    }
}