Looking for someone to help implement cube collision resolution using Mesh-BVH

Im looking for someone to help me implement collision resoultion with cubes. Using the following library: GitHub - gkjohnson/three-mesh-bvh: A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.

There is a simple example that shows sphere collision which is quite simple of course:
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/physics.html

You can shapecast to the bvh and detect the collision quite easily. However i want to add resoultion to the collision as well where angular velocity and regular velocity is adjusted accordingly.

Happy to discuss pricing etc.

Are you handrolling all your physics? If you need real physics, you might prefer to just leverage an existing physics engine like, ammo, jolt, havok, physx, rapier… Most engines create some kind of bvh internally already.

2 Likes

Id like to avoid using those engines as they are limited for my usecase. The physics also have to be handled server side and all of those engines step through the entire world with one function. Its not possible to step per object and to throttle behaviours etc.

and all of those engines step through the entire world with one function

That is incorrect. Most/all of the good engines identify simulation islands of activity automatically and only simulate “islands” that contain active/moving objects. If you want to pause regions, you can just put the corresponding objects to sleep.

You can have a world of 1 million objects, and only the ones that are near each other and contain an actively moving dynamic/static pair will be touched in the the simulation pass.

I’m mentioning this, because it sounds like you will basically be re-implementing this scheme by hand… which is possible, but if you’re going to the level where you’re simulating full rigid body dynamics, then you’re basically re-implementing a physics engine, but presumably without the benefit of a math phd. If this is just a learning project, then I guess that’s not the end of the world… but you may learn more by attempting to resolve one of these collisions on paper.

Already the idea that you’re only resolving a collision after a box shapecast intersect happens, is a bit of a red flag… since then it’s likely that you are attempting to resolve an interpenetration condition after the fact, without the corresponding contact manifold datastructures that are commonly used to prevent interpenetration during the normal simulation phase. (unless you are expanding your shapecast collision box by the length of the per frame movement vector, in which case you do stand a chance of detecting the collision before it happens, and thus you can record it in a list of potential contact points which can be tracked over time.)
( this is what most physics engines do).

Then there is the fact that what you described Sounds easy… for box->box collision, but these boxes also still have to collide with the rest of the world in a meaningful way… like falling onto/orienting towards other geometry, not to mention stacking of objects… So… a box… falls on another box. Who gets out of the way? The box that is moving? Perhaps it moves half way back, the other box is moved halfway forward… then you’ve created a new interpenetration as the second box is now penetrating the ground. If that interpenetration puts that boxes centerpoint past the geometry of the ground, then it falls through the ground. (Which you’ve probably seen in many many games before) or, perhaps the second box was blocking a door, but the collision response has now moved it past the door, even though it shouldn’t have been able to fit through it. You can solve these problems by keeping a list of contact points per dynamic object, and not allowing them to violate these point constraints during resolution… but it’s non trivial.

The big 4 engines I mentioned have had entire teams of engineers optimizing these interactions, in some cases for decades.
They are also usually written in languages like C and wasmed, and thus can achieve better performance than handrolled js in most cases.
Using a physics engine also enables other technique like continuous collision detection, for fast moving objects, (along with a whole host of other functionality that you wont know you need until you need it… like friction, motors, constraints like hinges and springs, raycasting, broadphase collision checks.)

r.e. running server side… the main 4 engines I listed are also pretty platform agnostic.
They have wasm &| c | c++ | rust variants.

Check out the Jolt physics repo for more overview of the benefits: (especially the features section)

In summary:
If you’re trying to build something more complex than billiards or a sliding cubes puzzle… you probably want a real physics engine.

2 Likes

Thanks for the extensive response. Jolt or Ammo js for example use wasm so its not possible to run that on a Node.js server.

The main reason i dont want to have to use an entire engine for this is because all im looking for is for the cube to interact with the BVH itself. Having the BVH in memory in addition to the physics engine seems like a waste and I would like to stick with one. The islands as you describe would also add memory overhead.

“Already the idea that you’re only resolving a collision after a box shapecast intersect happens, is a bit of a red flag” how so? This is the most optimised way to do it. When a collision is detected i can go back to the previous state and make my calculations accordingly.

Im not entirely opposed to using an existing physics engine. But the ones listed arent supported server side to my knowledge.

Thanks for the extensive response. Jolt or Ammo js for example use wasm so its not possible to run that on a Node.js server.

I believe that is incorrect:

The main reason i dont want to have to use an entire engine for this is because all im looking for is for the cube to interact with the BVH itself. Having the BVH in memory in addition to the physics engine seems like a waste and I would like to stick with one. The islands as you describe would also add memory overhead.

Don’t use mesh-bvh. Use the physics engines own BVH. They all have shapecasting routines built in.
r.e. Islands adding overhead… an island is simply a list of objects that may be interacting with each other, and contain at least 1 “awake”/moving object.

“Already the idea that you’re only resolving a collision after a box shapecast intersect happens, is a bit of a red flag” how so? This is the most optimised way to do it.

Also incorrect. The most optimized way is to detect collisions BEFORE they happen, and then carefully advance them To the point of penetration and accurately resolve the collision at time of intersect, rather than whatever time you happen to catch the penetration (which is often too late).

Im not entirely opposed to using an existing physics engine. But the ones listed arent supported server side to my knowledge.

I think most engines will run serverside under node… whether via wasm, or emscripen/asmjs, or in the case of cannon.js , as pure vanilla js.

Whether you really want to run your physics simulation serverside is also a design choice. I believe many games opt to only run physics on the client, and on the server only deal with more coarse grained events like “Player doesn’t have the amulet yet, so they can’t access this region”, and then just serve as an event relay between clients / to sync player states across clients.
It really depends on what you’re trying to build.

I honestly don’t mean to discourage you from writing a physics engine… I’m just trying to relay what I’ve learned.

2 Likes

“Don’t use mesh-bvh. Use the physics engines own BVH. They all have shapecasting routines built in.”

I know they each have their own. But the point was to stick with BVH because its much more lightweight and it is faster than ammo or any of the other ones. And it allows me to fully controll the collision checks myself. I cant just step through the entire world in one call because this doesnt allow me to control or sync things the way i would like. Ill give this lib you posted another look thanks

But the point was to stick with BVH because its much more lightweight and it is faster than ammo or any of the other ones.

This is most likely incorrect. Also physics engines often use a combination of data structures, not purely bounding volume hierarchies, and can thus outperform a single data structure.

And it allows me to fully controll the collision checks myself. I cant just step through the entire world in one call because this doesnt allow me to control or sync things the way i would like.

You don’t have to do that when using a physics engine. You can just get the list of currently contacting objects for a given object directly from the engine.
Usually in the form of a linked list or array of “contact manifolds” containing the pairs of colliding objects and information about the collision relationship… like contact point, and contact normal. This CAN get tricky since this list may change repeatedly during a single simulation update, but there is usually a mechanism to get a call on each simulation substep interval so you can be sure to not miss contacts. (if you’re not using an existing engine specific mechanism)

Most engines also have a higher level “event” system that you can subscribe to “collision” events and get callbacks when they happen.

2 Likes