#Architecture for 20k+ entities with physics

1 messages · Page 1 of 1 (latest)

hasty girder
#

Hello there.
What i'm currently envisioning is, having 2k-20k entities that spawn in the initialization of the level. Those entities will be spawned at pre-determermined zones, where they will have different quantities at different zones. To give a concrete example, the entities in question are going to be leaves. When they spawn, I want them to have physics, so they can pile up, scatter around and "curce" around terrain height.
I also want them to react to wind, to the player moving through them or using a leaf blower, etc.
How would you generally approach this as to not completely obliterate the fps?

fluid void
#

Physics for 20k entities can work, if the entities are mostly sparse and not piled up. If you pile up 1k-3k entities, even using the most basic sphere shape: it will already start killing physics performance, there is simply no way around that.

If you can only have less than 1k sphere collider geometry be piled at a time: I think it is possible. Thought definetelly profile it: it is pretty easy to check with a single script.

But what you are desribing is a more particle-like simulation, and there is a much more performant way: to use a VFX Graph. The problem is the collisions, as you will have to bake 1 sdf texture to describe collisions, and another sdf to describe currents (like wind). Piling up leaves is not easy and you will have to use some way to fake it, as VFX graph does not support particle-particle collision.

hasty girder
# fluid void Physics for 20k entities can work, if the entities are mostly sparse and not pil...

The majority of my entities are going to be overlapping quite a lot, thought I don't plan on all of them being literally on top of each other. This reference is pretty much exactly my use case. I want to simulate this effect without having to manually place each leaf in the level, but have a spawner zones instead. That being said, I will want the entities that the player interact with have the possibility of forming actual clumps

#

Also, I don't necessary need all of the entities simulated/spawned at once, as I can do it in zones/chunks close to the player while having the other be spawned in the background

fluid void
#

I would really suggest you write a dirty script which tries to replicate this behavior on top by spawning entity prefabs of a leaf in an area and check the profiler

#

The simplest way to asnwer if it is possible

#

If you can make this visible sector being simulated without much of a performance penalty, your idea of chunking leaves and simulating only active leaves is viable: you just write a proximity system which stores all leaves in a spatial map and disables their

PhysicsCollider, by setting BlobAssetReference<Collider> to BlobAssetReference.Null and storing the collider reference in another component

hasty girder
#

Yeah, I need to figure out a way to fake it and add physics on-demand or something. a few piles of 1k leaves and the fps poops itself

true yew
#

If you were making actual leaves physics objects, you'd usually just make them not collide with each other, but only the ground

hasty girder
#

My use case is a bit weird, since it's not just a passive folliage, but it's pretty much my main mechanic of the game to interact with them

fluid void
#

You can scan the leaves and merge them into bigger colliders which define the pile "blobs". This way, you combine 100 colliders into one which has a rough approximation of the merged colliders. You allow collision between the pile and the leaves: this way performance will always stay normal

#

You just have to also have some logic to "detach" the leaves from the blob. Probably not rely on the built in PhysicsVelocity but have a proxy LeafVelocity component which copies the velocity to PhysicsVelocity if the object is still a leaf collider, or move the leaf trabsform manually otherwise + check if the breaking distance is sufficient to detach the merged leaf and make it standalone again

#

It is doable and should be a good approximation of collision between leaves in a pile

#

So:

  1. no collision between leaves
  2. When leaves are close they are merged into a mesh collider which defines the rough volume of the leaves using some triangulator algorithm which tries to minimize triangle count.
  3. The combined shape collides with ground, but doesnt with leaves as instead those leaves are merged into the pile
  4. While in the pile, leaves still can be moved by external forces (using a simple linear velocity addition) which allows them to break from the pile at some breaking distance
  5. Probably when moving the leaves in a pile, raycasts have to be fired against the ground to not clip them through it. Easy reflection angle math and you know how to approximate these collisions
#

In the end, it is like a very simple physics approximation inside a pile which does not operate using unity physisc. Should be good with some tinkering and probably not noticable

hasty girder
#

I'll try something like that. I think my main issue and why I was striving away from merging individual leaves into one big blob is the scare that it won't feel good/work with my use case.
Since I want to be able to have for example a rake/leaf blower, moving through them, I never really thought that having them in big blobs is gonna do the trick, since I thought i'll be seing one big blob just moving around, which won't feel good.
On top of that, I'm really not currently sure how to make a good feeling/looking natural spread/lump of the leaves without collision between them/physics.

fluid void
#

Either way you will have to have a semi custom solution to apply forces or at least change center of mass of the leaf to have realistic leaf behavior. It is indeed not an easy task now, considering you want the realism of thousands of leafs while having realistoc physics. Some compromises have to be made

fathom pagoda
#

you could potentially ditch "physics" entirely and build some kind of boid-style algorithm

#

it's not like tree leaves are exactly rigid bodies anyway in real life

#

and maybe make it a hybrid with a fluid simulation where leaves on the ground are part of a "fluid" and leaves in the air switch to a boid-like system