JBullet 检测到碰撞时如何执行回调?

How do I execute a callback when JBullet detects a collision?

所以目前我在自己的线程上有一个 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;
            }
        };

you must add ContactAddedCallback to 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;
            }
        };

and add flag to rigidbody like this ::

rb = new RigidBody(constructionInfo);
rb.setCollisionFlags(CollisionFlags.CUSTOM_MATERIAL_CALLBACK);

now add to BulletGlobals Like this ::

BulletGlobals.setContactAddedCallback( myCollisionCallback );

if you want get which RigidBody are Collided do this ::

当您创建 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;
    }
};