armory3d / haxebullet

Bullet 3D Physics for Haxe
http://bulletphysics.org
zlib License
94 stars 21 forks source link

Collision callback #4

Closed Ohmnivore closed 8 years ago

Ohmnivore commented 8 years ago

Hi Lubos,

I've been sifting through Bullet's docs and forums to find how to process collisions. My first use case is to detect collisions of certain objects with the player, for which the gContactProcessedCallback method from http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Collision_Callbacks_and_Triggers seems well suited. However I'm not sure how to use the bindings to achieve this or what to add to the bindings. I'm wondering if you have any advice on how to process collisions in general.

Thank you for your time

Ohmnivore commented 8 years ago

Actually I'm making progress with the int numManifolds = world->getDispatcher()->getNumManifolds(); method, will update and close issue soon.

Ohmnivore commented 8 years ago

Here's an example of what worked for me. setUserIndex wasn't working for html5, turns out i had an outdated ammo.js build.

// Collision
for (i in 0...dispatcher.value.getNumManifolds()) {
    var m:BtPersistentManifold = dispatcher.getManifoldByIndexInternal(i);
    var b0:BtCollisionObject = m.getBody0();
    var b1:BtCollisionObject = m.getBody1();

    if (b0.getUserIndex() == player.phys.body.value.getUserIndex() ||
        b1.getUserIndex() == player.phys.body.value.getUserIndex()) {
        var numContacts:Int = m.getNumContacts();
        if (numContacts > 0) {
            var sum:Float = 0;
            for (j in 0...numContacts) {
                var p:BtManifoldPoint = m.getContactPoint(j);
                sum += p.getAppliedImpulse();
            }

            if (sum > 0) {
                player.cam.shake(1, 0.1);
                hudCam.shake(1, 0.4);
            }
        }
    }
}