MackeyK24 Posted January 1, 2017 Share Posted January 1, 2017 Hey guys... Anybody got on insights on how the mesh.onPhysicsColiide property is supposed to work ??? I have this code to detect when two meshes with physics state enabled collide with each other. Now in the scene... both mesh actually do collide and bounce off each other like it should... but the the mesh.onPhysicsCollide property i set is NOT firing off. Here is code to log the collision event to the console (BUT I GET NOTHING ) // Physcis collision this.mesh.onPhysicsCollide = (collider:BABYLON.AbstractMesh, contact:any) => { console.log("*** HOLY SHIT - I GOT COLLISION ***"); console.log(collider); }; Any thoughts ??? Quote Link to comment Share on other sites More sharing options...
jpdev Posted January 1, 2017 Share Posted January 1, 2017 You can register a function for collisions this way: http://www.babylonjs-playground.com/#1NASOD (From https://doc.babylonjs.com/overviews/Using_The_Physics_Engine#collision-callbacks) adam 1 Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted January 2, 2017 Author Share Posted January 2, 2017 @jpdev Yeah i saw that but i need the actual 'mesh.onPhysicsCollide' to work. It will be apart of the built-in Collision System i am creating for the U3D-BabylonJS Toolkit. I don't want to have to specify EACH and EVERY item i want to collide with physics (I do that kind of collision detection using mesh.intersectsMesh). I want to be able to use the default physics collision detection system... Which basically just fires when TWO rigid bodies COLLIDE with each other... I want to be able to "DETECT" when that happens and act accordingly ... I THINK... that is what the 'mesh.onPhysicsCollide' is supposed to do. Maybe ill have to go COMPLETELY native and some kind of mesh.physicsImposter.physicsBody.addEventListent('collide') or something like that... But then i would to find and implement for each physics engine (CANNON - OIMO - ENERGY when comes out)... Plus be able to get a reference to the Babylon.Mesh that the imposter is representing. I prefer NOT to have go that route. Maybe @Deltakosh or @Sebavan can weigh in here Quote Link to comment Share on other sites More sharing options...
adam Posted January 2, 2017 Share Posted January 2, 2017 You can also pass it an array of physics imposters.. https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/babylon.physicsImpostor.ts#L278 Quote Link to comment Share on other sites More sharing options...
BitOfGold Posted January 2, 2017 Share Posted January 2, 2017 @MackeyK24: A small script from my engine-library (adds a cannonjs collision event listener) (self is my game object, self.physics_engine is the same as scene.getPhysicsEngine(); ) This calls self._ontouch on every collision (with any to any physics body) without registerOnPhysicsCollide. var pb = mesh.physicsImpostor.physicsBody; pb.addEventListener( "collide", function ( e ) { var otherImpostor = self.physics_engine.getImpostorWithPhysicsBody( e.body ); if ( otherImpostor ) { if ( otherImpostor.meshParent ) { self._ontouch( mesh, otherImpostor.meshParent ); } } } ); Quote Link to comment Share on other sites More sharing options...
RaananW Posted January 2, 2017 Share Posted January 2, 2017 What I understand is - you want to execute a function every time this object collides with any other object. Is that right? I commented on your PR, w can take it from there. Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted January 2, 2017 Author Share Posted January 2, 2017 The Unregister give error (Function to remove was not found): this._mesh.physicsImpostor.unregisterOnPhysicsCollide([], this.processPhysicsCollisions) I have properly registered handler with : this._mesh.physicsImpostor.registerOnPhysicsCollide([], this.processPhysicsCollisions) and the handler is being called... So why would the unregister give missing error Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted January 2, 2017 Author Share Posted January 2, 2017 3 hours ago, BitOfGold said: @MackeyK24: A small script from my engine-library (adds a cannonjs collision event listener) (self is my game object, self.physics_engine is the same as scene.getPhysicsEngine(); ) This calls self._ontouch on every collision (with any to any physics body) without registerOnPhysicsCollide. var pb = mesh.physicsImpostor.physicsBody; pb.addEventListener( "collide", function ( e ) { var otherImpostor = self.physics_engine.getImpostorWithPhysicsBody( e.body ); if ( otherImpostor ) { if ( otherImpostor.meshParent ) { self._ontouch( mesh, otherImpostor.meshParent ); } } } ); I was doing this... But it would require me to handle the differences in the engines (cannon vs oimo) while this modified onCollide would work great and use the default internals for collision callbacks no matter if cannon or oimo (which does not use events for collision... it explicitly calls imposter.onCollide from executeStep in oimo plugin) So i PR this: public onCollide = (e: { body: any }) => { if (!this._onPhysicsCollideCallbacks.length) return; var otherImpostor = this._physicsEngine.getImpostorWithPhysicsBody(e.body); if (otherImpostor) { this._onPhysicsCollideCallbacks.filter((obj) => { return (obj.otherImpostors.length === 0 || obj.otherImpostors.indexOf(otherImpostor) !== -1) }).forEach((obj) => { obj.callback(this, otherImpostor); }) } } Just added OR obj.otherImposters.length === 0 for ALL/ANY imposter collision detection ALLTHOUGH... If i keep having problems with the unregisterphysicscollide (Function to remove was not found) error... I will probably have to go native for each engine and go back to addEventListener for cannon and MAYBE NOT SUPPORT OIMO AT ALL... Dunno yet. Quote Link to comment Share on other sites More sharing options...
Wingnut Posted January 5, 2017 Share Posted January 5, 2017 Hi guys. Here's a playground that helps illustrate these issues. Watch console. A 10-second timer calls un-register... but it fails to do the unregister (with CannonJS). When switching to OimoJS (adjust lines 11/12), function not found (Oimo has no collide events, to date). This topic is loosely related-to another topic... which I have been poorly-servicing. So, I was in a position to create a PG test case for this topic, and did. Exciting, huh? Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 5, 2017 Share Posted January 5, 2017 function clear(e){mesh.body.removeEventListner("collide", response); //Or like e.collisiomesh/target.body.removeEventListner("collide", response); or what ever the var that holds the target mesh held in the event } function response(e){ mesh.body.collision= true; clear(e); } mesh.colliderListen = mesh.body.addEventListener("collide", response); ? I think what is happening (almost know) is your trying it unregister a unnamed function... I could go into why this happens but Im pretty sure the above code should get you on the route to success. Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted January 5, 2017 Author Share Posted January 5, 2017 I actually have a working solution... waiting for @RaananW to approve it. This is the modified 'onCollide' from the babylon.physicsImposter.ts: /** * Legacy collision detection event support */ public onCollideEvent: (collider:BABYLON.PhysicsImpostor, collidedWith:BABYLON.PhysicsImpostor) => void = null; //event and body object due to cannon's event-based architecture. public onCollide = (e: { body: any }) => { var otherImpostor = this._physicsEngine.getImpostorWithPhysicsBody(e.body); if (otherImpostor) { // Legacy collision detection event support if (this.onCollideEvent) { this.onCollideEvent(this, otherImpostor); } if (!this._onPhysicsCollideCallbacks.length) return; this._onPhysicsCollideCallbacks.filter((obj) => { return obj.otherImpostors.indexOf(otherImpostor) !== -1 }).forEach((obj) => { obj.callback(this, otherImpostor); }) } } Works perfect... and NO UNREGISTER LEAKS Pryme8 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 6, 2017 Share Posted January 6, 2017 I never had unregistered leaks so I'm wondering if it was isolated to 2.5 Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted January 6, 2017 Author Share Posted January 6, 2017 I can't get to UNREGISTER... Does not ever Find the previously registered handler... But the #1641 PR fixes the issue anyways Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.