JBullet检测到冲突时如何执行回调?

时间:2019-07-27 00:58:55

标签: callback collision jbullet

因此,目前我有一个JBullet实例在其自己的线程上运行,我不知道如何获取有关JBullet 20101010-1上的冲突的信息。

我曾尝试如下所示致电ContactAddedCallback,但没有任何反应。

new ContactAddedCallback() {

            @Override
            public boolean contactAdded(ManifoldPoint cp, CollisionObject colObj0, int partId0, int index0,
                    CollisionObject colObj1, int partId1, int index1) {
                System.out.println("Collision");
                return false;
            }
        };

1 个答案:

答案 0 :(得分:0)

您必须将 ContactAddedCallback 添加到 BulletGlobals

在创建 ContactAddedCallback 后执行此操作,例如 ::

ContactAddedCallback myCollisionCallback = new ContactAddedCallback() {

            @Override
            public boolean contactAdded(

                ManifoldPoint cp, 
                CollisionObject colObj0, 
                int partId0, 
                int index0,
                CollisionObject colObj1, 
                int partId1, 
                int index1) {

                System.out.println("hi i am collision !!");// :|
                return false;
            }
        };

并像这样 ::

向刚体添加标志
rb = new RigidBody(constructionInfo);
rb.setCollisionFlags(CollisionFlags.CUSTOM_MATERIAL_CALLBACK);

现在添加到 BulletGlobals ,就像这样 ::

BulletGlobals.setContactAddedCallback(myCollisionCallback);

如果要获取哪个RigidBody发生冲突,请执行以下 ::

创建CollisionShape时,必须像这样 ::

添加userPointer。
BoxShape groundShape = new BoxShape(new Vector3f(1000,10,1000));

rb = new RigidBody(constructionInfo);
    ... 
groundShape.setUserPointer(rb);

最后在ContactaddedCallback中,您可以像这样 ::

ContactAddedCallback myCollisionCallback = new ContactAddedCallback() {

    @Override
    public boolean contactAdded(

        ManifoldPoint cp, 
        CollisionObject colObj0, 
        int partId0, 
        int index0,
        CollisionObject colObj1, 
        int partId1, 
        int index1) {

        if (colObj0.getUserPointer().equals(rb)) {

            //for sample you can push them to up
            rb.applyCentralImpulse(new Vector3f(0,1,0));

        }

        if (colObj1.getUserPointer().equals(rb)) {

            rb.applyCentralImpulse(new Vector3f(0,1,0));

        }
        System.out.println("hi i am collision !!");// :|
        return false;
    }
};