Suppose you’re making a game where the ground (not necessarily a Unity terrain) can be deformed at runtime, using some kind of procedural mesh deformation. And you also have physics objects that need to correct properly with this ground mesh.
Trouble is, because it can be deformed at runtime, it’s neither static nor (in general) convex.
What are my options for getting proper physics collisions on such a thing? All I can think of is to carve it up into convex pieces, but that sounds pretty expensive for runtime. Is there any other approach?
I think you’d be surprised how well the naive “drop some stuff on a terrain” works in Unity… Unity really is a beast.
I just wrote this script to sprinkle non-sleeping physics cubes into a level:
Download that MakeGeo project and fire up the grenade / bomb terrain damager scene (TerrainDamager) and drop an instance of that sprinkle script in, press play… then blast holes underneath the blocks and see.
Then just invert the terrain-depressor code to instead make it grow upwards from a grenade blast.
Invert it by changing the plus sign to a minus sign on line 154 of this script:
It clamps out the deform to the MaxDeformation value.
Assuming the cubes are big enough that the change in ground collider doesn’t “step through” them, it kinda … just works!
I guess with a bunch of little objects you could gather all the ones near the deformed ground and lift each one up enough to ensure ground clearance, or even add your own upwards velocity to start them flying… odds are the user will think it’s “about right.”
Unity is nutty-powerful. I love it. Five minutes to try out something that would have taken forever in the past.
So — just to be sure I understand — your suggestion is to actually use Unity terrains, which (1) can be deformed at runtime, and (2) play nice with the physics engine even though they are non-convex?
I merely did a test and observed that it worked “just fine” by my standards.
To let you experience it firsthand, I added some tips above to replicate the test at your end by a couple of clicks cloning the project and adding that script.
Non-convexity of the physics volume only applies to Rigidbody… you can make the environment colliders be whatever shape you like.
As far as performance or suitability, I make no warranty whatsoever.
I don’t think this is true. It’s only true for static objects, and our deformable terrain can’t be static. (However I think Unity Terrain has its own special collider type and plays by its own rules.)
Terrains with their Terrain Collider or meshes with mesh collider do not have to worry about convex or not.
Checking convex optimizes the mesh colliders allowing them to contact with each other. Otherwise they are just triangle collisions.
Terrain collider should have some optimizations for terrains since they are a special type of mesh.
My issue having looked @Kurt-Dekker code for his damager stuff. I have sense modified it to make use of
TerrainPaintUtility instead of manually modifying the maps. Is that when the terrain deforms, it does not wake up sleeping physics objects.
So like he mentioned may need to find all objects in the range and wake them or apply some force to them.
If you make a big change in the terrain may need to even raycast them to place them on top if the terrain is raised.
If its lowered waking them is probably enough to let gravity take over.