Handling sand

Hello everybody,

I am rookie at Unity3d and I have a project to develop for university. The objective of my project is to simulate the behaviour of the sand. I need to be able to manage the physics of the sand when I try to load it with a kind of excavator.

I see that Unity does not have a script or “prefab” component to make that and I know i will have to write a script for controlling it, but… could you provide some keys or preliminary help? Any key you provide would be usefull for me.

Thank you very much for your help and sorry for my low level of english. I hope you could understand… :S

Thank you all

I dont think it is possible to simulate sand with game engine. Not for another few years at least.

You might have some success if you treat it like a fluid.

Sand is quite doable if faked - you’ll be wanting physx spheres for sand. These are about tennis ball sized though. Going smaller isn’t feasible in realtime at the moment. Take a look on these forums for fluid physics - its basically the same granularity but different parameters for the feel of it.

I do have a number of ideas for it, but all are fairly complicated (generating a mesh based on a quadtree, similar to voxels).

Thank you for your quickly responses.
I know it isn´t feasible in realtime at the moment, but i know there should be several tricks.
In this video you can see a simulator working with sand. They are exacavating in real time. I was playing with the simulator and it was pretty good.

I know they used OGRE and their own phiycs engine.

90% faked without physics in that video. Notice that they use cluster detection, basically the more tennis ball sized particles in one place, the bigger the particle covering them. This way, it looks good. Also the ground: it’s a mesh that deforms, rather than being all separate particles of sand or dirt.

Using minimal amounts of physics objects to control location and determine heaps of dirt, but drawing a mesh to cover.

I’ve seen it used and done but only in 2D in real time, fun to do and play with, every pixel becomes a grain of sand.

Falling Sand, Sandbox game in action…

Yeah thats fine, but when you go 3D it instead of:

32 * 128 grains = 4096

it becomes (if a trough ish shape)

32 * 128 * 1024 = 4,194,394

4 million is a bit more than 4 thousand :stuck_out_tongue:

Even if it was perfectly square it’d be 128128128 = 2,097,152

The trick to this is some form of quadtree culling so you can only affect this area of sand and the maximum being the amount you can load. I’d say it’d still be a million grains worth.

Since your particles are likely to be very small you can forget about having to rotate them. All you have to do really is collide them. You could pretend they are spheres and do your own simple sphere-sphere collision detection. Or even treat them as rectangles and just do axis-aligned bounding box collisions. On a very small scale you’re not likely to notice that boxes always remain upright or that they don’t have spherical collisions, although I guess the sphere would give more realistic reactions.

You could look into some of the water based cellular automotons used to simulate water in dwarf fortress and minecraft styles games and look into adapting that to a cube based representation of sand, throw in a particle system and you could get a good looking low cost simulation of sand and even adapt it for snow.

Or what about using spherical physics colliders but make them chunky think big ball pool then overlay a mesh on them, and you might be able to get a flowing sand effect, or water/snow effect but would be best for small areas e.g. depending on target platform.

There is also something called ‘verlet’ physics that allows groups of linked springy objects to be simulated, could be an option?

Friends, thank you so much for your help.
I am still investigating.
I promise i will came back when i have some clear.
Regards!

Ps. Also if you have new ideas, of course are welcome :wink:

Well, its all about size. You obviously don’t calculate every grain for the whole thing, just the bit you’re scooping. You could try collisions and rendering entirely on the GPU - I imagine this would be the way forwards.