-
Notifications
You must be signed in to change notification settings - Fork 14
Collision & Physics
On this page, an overview of our approach to implementing the physics and most importantly, the collision of objects.
We are using the Cannon.js library, which is a physics engine that provides a way to describe objects' rigid shapes, their behavior when colliding, mass, and so on.
We have 2 types of rigid shapes (bodies) in our game world:
- Static bodies - these have a mass of 0 and they never move, however they affect non-static objects.
- Non-static bodies - these have a mass other than 0 and move around.
The billiard balls are non-static, while the components of the table: holes, walls and floor are static bodies.
Creating the holes turned out to be non-trivial, as the holes are concave shapes, but a convex representation of them was necessary.
We did some experimentation, there are software tools for decomposing a concave mesh into convex subshapes, such as V-HACD. As the tool seemed to require a bit of parameter tweaking to get a nice result, we decided to try other approaches first.
The Cannon.js library provides built-in implementation of heightfields. This means you can provide an array of height data and you get a body with corresponding height values. This is often used for the landscapes of games (hills, valleys, etc.). However, because of how easy it is to define a heightfield, we decided to try whether we could use heightfields for our billiard table holes.
The result was sort of acceptable, however not very elegant nor flexible, which is why we abandoned this idea.
The approach we ended up using was to create arches out of CANNON.js primitive shapes - namely boxes. We developed code for generating an arch out of an arbitrary number of boxes. Because the number of boxes used is arbitrary, we can easily tweak this number to find a balance between performance and realism (Or add a setting for users adjust this setting according to their hardware). After all, this approach of using boxes is sub-optimal: it causes overhead, as several faces of the boxes will never collide with the ball, but the physics engine still runs calculations for all of the box faces.
A possible improvement to this approach would be to create the arches out of custom trimesh shapes. For a single hole, we used two arches, one of them aligned with the floor and made out of thicker overlapping boxes, giving the impression of an even surface near the hole edge.