#Is Unity Physics Rebuilding Static Bodies Every Frame A Bug?

1 messages · Page 1 of 1 (latest)

safe token
#

If you spawn a bunch of Static Physics Body entities at runtime, Unity Physics will spend a lot of time rebuilding the tree for those static objects every frame.

Is this intended behavior, a known bug, or am I not fully understanding the situation as to why this happens?

glass cloak
#

Are you affecting the transforms of those static entities at runtime? An issue we had was some objects that should've been kinematic, were actually static, and as you say - it then spent every frame rebuilding the static physics BVH because their transforms had been written to.

safe token
#

When the entities are spawned, their transform position, scale, rotation are changed, but that is all. They are not moved at all after their placement.

glass cloak
#

Do you have any systems that have write access to the transforms of those static things? Just accidentally marking those chunks as "possibly written to" it would trigger the change filter, I think?

safe token
#

I do have a system that spawns the static collider entities, but no system that touches them after the fact.

#
{
    Entity spawned = ECB.Instantiate(spawnerComponent.Prefab);

    LocalTransform spawnedTransform = new LocalTransform
    {
        Position = Random.NextFloat3(-halfDimensions, halfDimensions),
        Rotation = quaternion.RotateY(Random.NextFloat(0, 2 * math.PI)),
        Scale = Random.NextFloat(spawnerComponent.Scale.x, spawnerComponent.Scale.y)
    };

    ECB.SetComponent(spawned, spawnedTransform);
}```
glass cloak
#

Interesting, that looks mostly fine to me (as long as the prefab in your SpawnerComponent idoes not itself contain SpawnerComponents! Since that would cause it to potentially generate new things every frame).

How are you ensuring your prefabs are static from the physics systems PoV? Also what version of entities, physics, and the engine are you using?

safe token
#

This is the prefab

wide python
#

If you create a project with no systems, and put a bunch of static physics objects in the world

#

Does it rebuild every frame?

safe token
#

I am using Entities 1.0-pre.15, Physics 1.0-pre.15

safe token
wide python
#

you can spawn them just don't have any systems opening transforms

safe token
#

or is there some other way of spawning at runtime I am unaware of?

wide python
#

for a quick test, just throw 1000 objects in a subscene with no spawning

#

it shouldn't be updating the static bodies every frame

#

but you need to confirm this

safe token
#

ok 😅

safe token
#

Ok, here are my highlevel, in-editor profiler results for 204,800 entities spawned in Editor with no systems written by me in the project at all

#

(may need to open in browser to read, as I have an ultrawide)

#

and this is the frame immediately after

wide python
#

only seems to build once

safe token
safe token
#

but the next frame, it appears to do it again

wide python
#

yeah something is (probably incorrectly) triggering change filters

#

i wonder what

#

in theory only opening transform or physicscollider for write

#

or creating/destroying a static collider

#

should trigger it

safe token
#

I guess I can zip up this project and submit it as a bug report

wide python
#

i was curious about this

#

and the reason it updates every frame is one of the change filters is on LocalToWorld
chunk.DidChange(ref LocalToWorldType, m_LastSystemVersion) ||

#

which is open every frame for writing in the transform system stuff

#

i suspect this is just some teething issues with the V2 transform update

#

and when we can properly remove components again it should be fine

safe token
#

Do you know if you comment out that specific check, if it stops doing the update every frame?

wide python
#

as long as you aren't opening localtransform

#

however it will break if you have a localtoworld entity with no localtransform

#

i think the proper fix is only doing a LocalToWorld DidChange if it doesn't have a LocalTransform

#

(!chunk.Has(ref LocalTransformType) && chunk.DidChange(ref LocalToWorldType, m_LastSystemVersion)) ||

#

something like that

lavish echo
#

Hello! @wide python is correct, this was a bug in the 1.0-pre15 release, where the transform system itself was prematurely taking Read/Write access on entities whose transforms hadn't changed, which caused a bunch of ChangeFilter checks to fail and a bunch of unnecessary work to happen on otherwise-static entities. It was promptly reported and fixed in the forums; the fix is included in this week's new 1.0 preview release.

wicked cliff