kripken / box2d.js

Port of Box2D to JavaScript using Emscripten
1.32k stars 196 forks source link

Using collision events not working #91

Open fdev31 opened 6 years ago

fdev31 commented 6 years ago

Taking a working scene and adding the README code to it (while renaming JSContactListener to b2ContaxtListener):

        var listener = new b2ContactListener();
        listener.BeginContact = function (contactPtr) {
            var contact = Box2D.wrapPointer( contactPtr, b2Contact );
            var fixtureA = contact.GetFixtureA();
            var fixtureB = contact.GetFixtureB();
            // now do what you wish with the fixtures
            console.log('plop')     
        }
        world.SetContactListener( listener );

The console never prints anything...

orgicus commented 6 years ago

@kripken I'm running into the same issue as @fdev31 Any hints on how to resolve this ?

kripken commented 6 years ago

@orgicus Sorry, I don't have much time for this project anymore, and I don't remember enough about how stuff like the callbacks work, to be honest.

finscn commented 6 years ago

There are 2 js version box2d : https://github.com/flyover/box2d.js https://github.com/flyover/box2d.ts

they are active, I think you could try them.

kripken commented 6 years ago

It would be great though if someone did have the time to be a maintainer of this project, as it compiles to asm.js/wasm which is great for speed.

ShoutTree commented 5 years ago

I added prefix "Box2D." to both JSContactListener and b2Contact, which results Box2D.JSContactListener and Box2D.b2Contact, and then it works.

orgicus commented 5 years ago

@ShoutTree do you mind posting a short snippet ? I've tried something super basic like:

var cl = new Box2D.b2ContactListener()
cl.BeginContact = function(e){
    console.log(e);
}
cl.EndContact = function(e){
    console.log(e);
}
world.SetContactListener(cl);

but I got no console message. I might missing a trick here.

Can you please elaborate on your approach that worked ?

Thank you, George

ShoutTree commented 5 years ago

@ShoutTree do you mind posting a short snippet ? I've tried something super basic like:

var cl = new Box2D.b2ContactListener()
cl.BeginContact = function(e){
    console.log(e);
}
cl.EndContact = function(e){
    console.log(e);
}
world.SetContactListener(cl);

but I got no console message. I might missing a trick here.

Can you please elaborate on your approach that worked ?

Thank you, George

var world = new Box2D.b2World(new Box2D.b2Vec2(0.0, -9.8));
var listener = new Box2D.JSContactListener();
listener.EndContact = function(contactPtr) {
    var contact = Box2D.wrapPointer( contactPtr, Box2D.b2Contact );
    var fixtureA = contact.GetFixtureA();
    ....
}
listener.BeginContact = function(contactPtr){}
listener.PreSolve = function(contactPtr){}
listener.PostSolve = function(contactPtr){}
world.SetContactListener( listener );