From e34a7d931e053da480350ade47d701fe9359898f Mon Sep 17 00:00:00 2001 From: JeremyGfr Date: Sat, 20 Jun 2015 19:44:31 +0200 Subject: [PATCH 1/2] Adding EndContact Listener management ;-) I just add a few end contact management, hope this will help a lot of people :) --- src/device/cocoon_box2d.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/device/cocoon_box2d.js b/src/device/cocoon_box2d.js index 7488221..d7122d4 100644 --- a/src/device/cocoon_box2d.js +++ b/src/device/cocoon_box2d.js @@ -742,7 +742,7 @@ if ( !window.ext || typeof window.ext.IDTK_SRV_BOX2D === 'undefined' ){ window.Box2D.Dynamics.b2ContactListener = B2ContactListener ; B2ContactListener.prototype.BeginContact = function (/*contact*/) {} ;// NOTE: Only this one is called at the moment - B2ContactListener.prototype.EndContact = function (/*contact*/) {} ; + B2ContactListener.prototype.EndContact = function (/*contact*/) {} ;// NOTE: This is called for now ( Contribute by Jeremy Guilbault ) B2ContactListener.prototype.PreSolve = function (/*contact, oldManifold*/) {} ; B2ContactListener.prototype.PostSolve = function (/*contact, impulse*/) {} ; @@ -765,6 +765,7 @@ if ( !window.ext || typeof window.ext.IDTK_SRV_BOX2D === 'undefined' ){ this.m_jointList = []; this.m_fixturesList = []; this.m_contactListener = null ; + this.m_current_contacts = []; this.m_jointsList = [] ; this.m_worldID = window.ext.IDTK_SRV_BOX2D.makeCall( "createWorld" , gravity.x , gravity.y , doSleep ); @@ -864,6 +865,7 @@ if ( !window.ext || typeof window.ext.IDTK_SRV_BOX2D === 'undefined' ){ body.m_xf.position.Set(transforms[i+1] ,transforms[i+2] ) ; body.m_xf.R.Set(transforms[i+3]); } + // Handle object contacts if( this.m_contactListener !== null ){ @@ -880,9 +882,40 @@ if ( !window.ext || typeof window.ext.IDTK_SRV_BOX2D === 'undefined' ){ console.log("One of the fixtures in a contact DOESN'T EXIST!!"); continue ; } - this.m_contactListener.BeginContact( new B2Contact(fix1,fix2,touching) ) ; + // add to the currents contacts + this.m_current_contacts.push( new B2Contact(fix1,fix2,touching) ); + } + // check all the current contacts + var j; + for(j in this.m_current_contacts ) + { + var contact = this.m_current_contacts[j]; + var a = contact.GetFixtureA().GetBody(); + var b = contact.GetFixtureB().GetBody(); + if(a && b) + { + var a_contacts = a.GetContactList(); + var a_touching = false; + for(i in a_contacts) + { + if(a_contacts[i]==b) a_touching = true; + } + if(!a_touching) + { + this.m_current_contacts.splice(j,1); + this.m_contactListener.EndContact( new B2Contact( contact.GetFixtureA() , contact.GetFixtureB() ,false) ) ; + } + } + else + { + // body has been destroyed n + this.m_current_contacts.splice(j,1); + } + } + + } }; From 4d2be7f118976d8d5ea934b0d5c77438edbec8b2 Mon Sep 17 00:00:00 2001 From: JeremyGfr Date: Tue, 14 Jul 2015 15:53:52 +0200 Subject: [PATCH 2/2] Update cocoon_box2d.js --- src/device/cocoon_box2d.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/device/cocoon_box2d.js b/src/device/cocoon_box2d.js index d7122d4..e53cd86 100644 --- a/src/device/cocoon_box2d.js +++ b/src/device/cocoon_box2d.js @@ -892,25 +892,21 @@ if ( !window.ext || typeof window.ext.IDTK_SRV_BOX2D === 'undefined' ){ for(j in this.m_current_contacts ) { var contact = this.m_current_contacts[j]; - var a = contact.GetFixtureA().GetBody(); - var b = contact.GetFixtureB().GetBody(); - if(a && b) - { - var a_contacts = a.GetContactList(); - var a_touching = false; - for(i in a_contacts) - { - if(a_contacts[i]==b) a_touching = true; - } - if(!a_touching) - { - this.m_current_contacts.splice(j,1); - this.m_contactListener.EndContact( new B2Contact( contact.GetFixtureA() , contact.GetFixtureB() ,false) ) ; - } + if(contact.GetFixtureA() && contact.GetFixtureB()) + { + var a = contact.GetFixtureA().GetBody(); + var b = contact.GetFixtureB().GetBody(); + var a_contacts = window.ext.IDTK_SRV_BOX2D.makeCall( "getObjectContacts" , this.m_worldID , a.m_bodyID ) ; + if(a_contacts.indexOf(b.m_bodyID)==-1) + { + // End contact ! + this.m_current_contacts.splice(j,1); + this.m_contactListener.EndContact( new B2Contact( contact.GetFixtureA() , contact.GetFixtureB() ,false) ) ; + } } else { - // body has been destroyed n + // body has been destroyed this.m_current_contacts.splice(j,1); } }