Vehicles can fall through terrain when deformed

I have implemented terrain deformation so you can dig a hole or build a hill or level a road. I have vehicles that drive around on the terrain using physics (simulating real vehicles without spinning tires pushed with whatever force it takes to get them to move). My problem is that when I deform the terrain, which triggers a rebuild of that terrain tile, the vehicles can fall through the terrain even when far from the spot editted! Or they might just get jostled by the terrain change, or stuck halfway in the terrain. Do I have to turn off physics for every object to change the terrain data? Perhaps there is a toggle to halt physics globally?

The problem is caused by the fact that underneath (from the physics engine point of view) the old terrain collider (with all the “contacts” it was tracking) is just destroyed. The new one gets created, and the physics engine determines from the start what should happen with stuff that was touching this collider. It is unfortunately indeed possible that things that have been “touching” the old terrain mesh will fall through the new one.

There are ways to temporarily disable physics (simplest: togle Viewport.Items.EnablePhysics to false). But they will not really help – whenever you enable the physics again, the physics engine will determine how does new collider interact with cars.

I do not yet have an easy answer, admittedly. The general advise is “do not do that”, i.e. do not change a collider of something at runtime, when that collider may be touching some things. How this can transfer to your game – I’m not sure. Are your terrain edits somehow limited, so that you can have 1 terrain collider with terrain used by cars for riding that is constant? Or by deforming you only change some additional colliders?

Otherwise, maybe simply bump the cars positions in Y to be safer. I.e. when changing terrain collider, just set CarTransform.Y := CarTransform.Y + 0.1 or something like this for all cars. Maybe you can find a value for “0.1” in this that is 1. large enough to prevent cars from falling down, 2. small enough to not look funny for the player?

So perhaps I need to create a whole new tile, add it to the scene and then remove the old terrain? Then there is always something under the cars? Terrain deforms to make roads work, so will happen a lot.

So that for e.g. 1 frame both old and new collider exists? I don’t think this would work. The physics engine will not realize it has to “copy” the contacts from old to new one. The new terrain may as well cause the cars to jump up. And you’d have 2 colliders at the same place for a bit.

That said, I am not 100% sure – results of experiments welcome :slight_smile:

I don’t think there’s a solution that doesn’t involve moving the cars a bit (bumping their Y when collider is replaced), to avoid issues. (Or that would require reworking your colliders.)

The cars have a physics mode for movement and a non physics mode. I will turn them all to non-physics mode and then do the deformation. Then turn back on their physics. So for a split second they will be immune to tipping over on steep terrain.

1 Like