Jump to content

How to detect collisions with unknown meshes


OMAR
 Share

Recommended Posts

Hey guys,

is there a way of detecting a collision between a known mesh and an unknown one?

For example here: http://www.babylonjs-playground.com/#1M7CV7#1

Here we have box and let's say I want to check if this box collides with left or right box but without using box.intersectsMesh(mesh) considering that we actually don't know what we are colliding with and want to get the info about the object we are colliding with.

I searched the docs yet couldn't find it, is there way to do it? Thanks :) 

Link to comment
Share on other sites

I'm searching for something like a collision listener, or more precisely:

box.onCollisionEnter = function(other) {
   // other is the object we have collided with
   if (other.name === "box2"){
      // name of the collided object is box2
   } else if (other.tag === "box3") {
      // tag of the collided object is box3
   }
}

Perhaps something like this? This function could fire every time when our object collides with another object and store a reference to that object that we collided with, so that we can use its info later to implement some logic there

Link to comment
Share on other sites

there is a collision event listener, let me see if I can dig through Raans release notes, he posted an updated on that not to long ago! and I actually got to use it, im just not sure where that playground went.

ahhh I dont have time to dig right now actually I need to get back to work.

Requesting @RaananW; he will know where to point you right away.

Link to comment
Share on other sites

well! I was summoned :)

Babylon's internal collision system might be helpful here - http://www.babylonjs-playground.com/#1M7CV7#3

If you use moveWithCollision, the onCollide function of the mesh will be triggered when it collides against a different mesh with checkCollisions set to true. This way you can collide against "unknown" meshes. The reason it goes "into" the other mesh is the ellipsoid which I didn't set. And the reason it is triggered each frame is because moveWithCollision is set in a beforeRender loop. those things can be easily fixed :)

 

Link to comment
Share on other sites

@RaananW is there a way of doing this without using moveWithCollision? Or perhaps something more like a trigger that checks for collisions all the time and fires up when it detects one?

Thanks

Edit: Like for example in my previous PG keeping that lerping stuff yet still checking collisions, that would be hella useful!

Link to comment
Share on other sites

You could build one. Iterate thorough the entire list of meshes and check for intersection. 

If you wonder about performance, this is the reason it doesn't exist natively.

You could tag meshes to be checked against, or give them a specific mesh. This way your list of meshes will be smaller, not impacting the performance so much.

Link to comment
Share on other sites

@RaananW So I came up with this method: http://www.babylonjs-playground.com/#1M7CV7#4

Is it stupid? I mean... if it's stupid but it works, then it's not stupid I guess lol?

Edit: It also detects box2 when the scene starts, I dont know why

Another Edit: RaananW you are a genius! Doing something like this is actually really powerful:

var colliders = []; // all meshes that can collide go here

function checkCollisions(meshToCheckCollisions) {
   scene.registerBeforeRender(function() {
      for (var i = 0; i < colliders.length; i++) {
         if (meshToCheckCollisions.intersectsMesh(colliders[i]){
            // do something or maybe return something
         }
      }
   }
}

var player = BABYLON.Mesh.CreateBlahblahblah(blahblah);

var boxes = BABYLON.Mesh.CreateBoxBlahblah(blahblah);
var anotherbox = boxes.createInstance("another box");

colliders.push(boxes, anotherbox);

checkCollisions(player); // perfect!

 

Link to comment
Share on other sites

1 hour ago, OMAR said:

// do something or maybe return something

You will not be able to return anything from this function, as this is running in a different scope. But, you can do something like this:

var colliders = []; // all meshes that can collide go here

function checkCollisions(meshToCheckCollisions, onCollide) {
   scene.registerBeforeRender(function() {
      colliders.forEach(function(c) {
          if (meshToCheckCollisions.intersectsMesh(c) {
              onCollide && onCollide(c);
          }
      });
   }
}

var player = BABYLON.Mesh.CreateBlahblahblah(blahblah);

var boxes = BABYLON.Mesh.CreateBoxBlahblah(blahblah);
var anotherbox = boxes.createInstance("another box");

colliders.push(boxes, anotherbox);

checkCollisions(player, function(collidedAgainst) {
   doSomethingWith(collidedAgainst);
}); // perfect!

 

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