Precise collision detection and others

Hello I am working on my small game project (I am fulltime java developer and playing with Babylon as a HOBBY). I need one very simple thing - I have a Vector3 (bullet moving through space) and I need to detect when it collides with asteroid.

The problem is that Babylon´s intersectsMesh() function return way too much false positives :frowning: I understand that it uses bounding box (or OBB) inside and THAT is the reason why it returns false positives. But what I dont know is - how to do it otherwise ?

First of all I have to mention, that I am happily playing with meshes, materials, lights and so far I am happy - I DO NOT WANT to dive deep into physics and geometry - thats why I am using library in the first place :slight_smile:

Isnt there some oneliner like var collision = myMesh.preciselyCollidesWith(myVector3) somewhere ? I have already found that there are whole physics libraries that shall provide such functionality but it is “whole new world that I dont have capacity to explore” I have looked at some playground examples but they are always like 50 lines of code just to do that. I know that it feels lazy, but thats why libraries were invented in the first place right ?

I know that it could be CPU heavy, I know that I shall only use this precise collision calculation on pre-filtered list of meshes (prefiltered by simpler methods like OBB). Is there something ready to use just as it is ? Or do I really have to implement whole physics system into my project that I will not use for anything else besides just “is this vector inside that mesh ?” thing.


And I have one additional question - I am using particle systems for my explosions and I would be ok with all explosions looking alike. Is there some possibility to save result of particle system in some file / object and then using it repetitively ? I believe that it could save some valuable CPU time when it would not have to calculate partiles again and again.

Thanx sinerely in advance !!! Your library is pretty cool and I know that it is all my fault but as a person that have like 1 - 2 hours / day of time to my “hobby project” I am trying to focus on the game itself by using tools that are available out of the box, rather than dive into every aspect of 3D programming and trying to embrace everything.

2 Likes

cc @Cedric in case he has an idea :slight_smile:

How much precision or accuracy do you need?
You could use the BoundingSphere of the mesh to check against

If you are testing against a concave triangle mesh, you won’t be able to prevent false-positives

Thanx for your reply - I dont know exactly what precision do I need :smiley: Just as preisely as possible while it steill being playable (FPS wise) I am open to trying several variants.

You have mentioned BoundingSphere, which I would like to try, but function Mesh.intersectsMesh does not have parameter to do that. I can only set TRUE / FALSE (Orientet Box / Axis Oriented Box) but documentation do not mention that it is possible to use BoundingSphere in that function.

I can imagine to use that BSphere as a target for intersection check, instead of the mesh itself.

**shotMesh.intersects(asteroidMesh.getBoundingInfo().boundingSphere) ** you meant something like that ? I could definitely give it a try.

I can also scale that bounding sphere down for like 5% if it creates better real-world results ? False negatives are more acceptable than false positives in my case.

If it’s a bullet, then it moves. if it moves, then I would use a raycast and not mesh intersection.
There is a good one in Physics plugin (with ammojs) and there is the one used by picking (IIRC).

1 Like

Hello, I am not exactly sure what do you mean by “raycast” - I mean I know about that technique, but I am not sure about its correct implementation.

What I am doing at this moment is:

  1. I have one mesh that is very long and thin tube

  2. When I shot, I rotate this tube to the direction of that shot

  3. I cycle through all my asteroids and check, if that tube intersectsWith them and save intersected ones with distance information into an array. I am using bounding spheres as they work better than boxes in my case.

  4. When shot passes appropriate distance, I double-check intersection with mesh that I have found in step 3 and if everything goes well, considering as being hit.

What exactly do you mean by Raycasting ? I think that my rotated tube actually doing the same, isnt it ? With actual results I am ok, bounding spheres are precise enough for my use case. But offcoures I would try something even better if possible, but as I said, I would be happier with just Babylon.js as I am afraid that implement whole new library would be quite complicated ?

Thanx !!!

Have you read this page? Mesh Picking | Babylon.js Documentation
I think it can help in your use case.

This process sounds just very heavy and complicated. I imagine the first mesh that is hit by the bullet will take the hit? Unless your bullet can go through some materials, changing its range, velocity and the damage taken (which would make it a bit more complex).
I believe what @Cedric means with using rays is that when you aim and shoot your bullet from a distance, the ray cast from the source in the direction of aiming catches the first mesh that will be hit by the bullet. When the bullet reaches this point (hit), it is detected and makes for the impact (animation) and damage taken (game state). That’s it, that’s all. Else, You could also have a look at yuka
Here’s a link for an FPS demo:
https://yuka.babylonjs.xyz/js/playground/hideAndSeek/

2 Likes

In my case, there are often situations when another ship shoots - not just mine. Picking only works in first person situation, isnt it ?

You are right - first mesh gets the hit, but the problem is that not only my ship can shoot but also others (and more often actually). Picking only works when source position is “my camera” ?

One more reason to use the ray. You can cast the ray from any source.
Here a couple of PGs from the doc that might be of interest to you:

Edit: did you say ships? Like in artillery? these would have a curvature so the simple ray won’t work here.
May be start by taking a look at this pg:

Thanx a lot ! These rays are probably what am I looking for. Meanwhile I was able to get okayish rresults with mesh intersections but this feels more CPU effective, so I will definitely give it a try.

And as I said ship, I meant space ships so straight line is perfectly fine for me. Thanx once again !

1 Like