Jump to content

SolidParticleSystem and Precise Collisions


entropy
 Share

Recommended Posts

I have a project where I have a collection of solid particles that I need to test if they intersect. I have a simplified demo of the project here:

https://playground.babylonjs.com/#C7K3Q1

The particles themselves will have a box shape (as in the example) and I need to know if they physically overlap. According to the documentation for SPS, 

particle.intersectsMesh(otherParticle) 

uses less precise metrics to establish whether meshes intersect. Indeed, in the above playground, a collision is registered (red color) even if the actual meshes do not overlap.

So I found the following line in babylon.solidParticle.ts located at https://github.com/BabylonJS/Babylon.js/blob/master/src/Particles/babylon.solidParticle.ts#L173:

return this._boundingInfo.intersects(target._boundingInfo, false);

In my local version of the script, I tried changing the "false" to "true" to force the more precise collision detection, but that seems to have no effect (I can't demo that in the playground, obviously). Is there a way to force the more precise calculation (even if it's on my local copy of babylon.js)? Or, perhaps there's some better way of achieving this?

P.S. I realize this is an expensive CPU operation, but I only need to test for collisions once since the particles are stationary.

Link to comment
Share on other sites

You did exactly what should be expected when dealing with a mesh.

Solid particles actually aren't real meshes because they don't have each a world matrix for memory and performance reasons. When you want to check precise collisions between two meshes (so when you set the parameter precise to true), each mesh bounding box is updated according to its world matrix in order to compute the Bbox rotated axes in the world. This can't be achieved for solid particles not having a world matrix.

If you still want to do this, you'll have to :

- get the stored rotation matrix of each particle (note : it's a dedicated light implementation, not a real BJS matrix object, just a 9 float array) : https://github.com/BabylonJS/Babylon.js/blob/master/src/Particles/babylon.solidParticle.ts#L96

- rotate manually the Bbox directions according to this matrix, a bit like you would update the Bbox according to some mesh world matrix. Once this done, you may consider the particle Bbox is updated according to the particle rotation, so the direction values are now right : https://github.com/BabylonJS/Babylon.js/blob/master/src/Culling/babylon.boundingInfo.ts#L233

- then apply the precision intersection test

 

This should work. But it's quite a big work to do. Maybe could you consider another simpler approach :

- create some nodeTransform objects in order to just compute some maths on them, one per particle to be checked. Apply each one the same rotation than its matching particle. Update its world matrix and its Bbox.

https://doc.babylonjs.com/how_to/transformnode

- then check precise intersections directly on these nodeTransform Bboxes and apply your wanted behavior to the related particles. In this case, no need for particle intersections within the SPS, the collisions are computed apart in a dedicated logic on pure math objects, a bit like a physics engine works actually. 

 

An even simpler approach is to use a real physics engine and to associate each particle to an impostor. This works also quite well.

Link to comment
Share on other sites

I can't remember if the transfomNode holds a Bbox or not.

If not :

- create a transformNode

- create an independant BoundingInfo objects besides

- each time you rotate the transformNode, get its updated world martix, then update the BoundingInfo by passing it the transformNode world matrix. This will update the Bbox directions.

Link to comment
Share on other sites

@jerome Here's the solution that I employed. Instead of using transformNode, I just used BoundingInfo and directly set its world matrix. Fortunately, the Matrix class has all the functionality needed to generate the necessary information. Here's the PG:

https://playground.babylonjs.com/#C7K3Q1#1

Lines 60 through 62 contain the relevant new code. Everything works great...thanks for the help!

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