OMAR Posted April 5, 2016 Share Posted April 5, 2016 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 Quote Link to comment Share on other sites More sharing options...
Vousk-prod. Posted April 5, 2016 Share Posted April 5, 2016 Hey, thats actually a good question. Quote Link to comment Share on other sites More sharing options...
OMAR Posted April 5, 2016 Author Share Posted April 5, 2016 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 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted April 5, 2016 Share Posted April 5, 2016 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. Quote Link to comment Share on other sites More sharing options...
RaananW Posted April 5, 2016 Share Posted April 5, 2016 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 OMAR and Pryme8 2 Quote Link to comment Share on other sites More sharing options...
OMAR Posted April 6, 2016 Author Share Posted April 6, 2016 @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! Quote Link to comment Share on other sites More sharing options...
RaananW Posted April 6, 2016 Share Posted April 6, 2016 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. Quote Link to comment Share on other sites More sharing options...
OMAR Posted April 6, 2016 Author Share Posted April 6, 2016 @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! Quote Link to comment Share on other sites More sharing options...
RaananW Posted April 6, 2016 Share Posted April 6, 2016 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! OMAR 1 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.