Jump to content

[SOLVED] - Mesh.OnPhysicsCollide NOT Firing


MackeyK24
 Share

Recommended Posts

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 ???

Link to comment
Share on other sites

@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 :)

 

Link to comment
Share on other sites

@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 );
      }
   }
} );
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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? :)

Link to comment
Share on other sites


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.

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...