Search code examples
game-physicsphysics-enginecannon.js

Large-scale spherical world in a physics engine


(This question is targeted at Cannon.js (by @schteppe), but hopefully answer(s) will be applicable to other engines for the benefit of others not using Cannon.)

It seems like one strategy for building a huge spherical world in a physics engine is to start with 6 heightmaps forming a cube, and then to warp that cube into a sphere (the "cubic sphere" approach, see this explanation).

So that approach looks fine, and I was about to implement it just because it's what everyone else seems to be doing, but I've got a nagging (and very likely naive) question in the back of my head:

Why not just generate a (non-rigidbody) spherical surface and use it in the pre-step of the simulation to provide a reaction force against vertices that fall below that surface? Are there any problems with this approach that make people avoid it and choose the cube sphere approach instead?

Gravity on all objects is allso applied during the pre-step, so it seems like it would just be a matter of checking the object's vertices while I'm at it?

In my particular case the world doesn't need to be high-resolution, and could even be a blocky, minecraft-type world. It also doesn't need any textures or anything like that, if that has some impact on many people choosing the cube sphere approach. Thanks!


Solution

  • If you go the sphere/cube path, you get many things "for free" from the physics engine. It includes stable contacts, friction, collision events etc..

    If you implement a "force sphere" that applies force in the normal direction on each penetrating vertex, there are a few obvious things that you will miss:

    • No friction: your objects will glide on the planet surface, unless you implement friction yourself (this is difficult to get right without a friction solver).

    • "Spongy" contacts: since you're using explicit force calculations and not a contact solver, the contacts will allow larger penetration. (unless you use a large spring coefficient and very small time steps - which is gives bad performance)

    • Penetration at ridges: if you want to have some sort of ridge or a sharp mountain peak on your planet, you cannot drop an object straight onto the peak. The peak will penetrate the object where it has no vertices. A physics engine solves this for you with the help of mesh clipping.