#How to model a large number of 2d objects and collisions?

1 messages · Page 1 of 1 (latest)

wet shell
#

Hello Community 🙂
I try to develop a game in unity and it works great, but i am not happy with the performance.
The challenging part about my project is that i want to track many objects which collide (game is 2d).

First I just normal Circle Collider 2D and give every object a Monobehaviour to control movement. This worked great, but at around 2000 objects, the game started to lag.

So I thought I try ECS since it should be faster. However, I dont seem to get a performance increase out of it. Also there it starts to lag around 2000 objects, which was a very frustrating moment...

In ECS i used Unity.Physics.SphereCollider (since there is no 2d option) and lock angular and z-axis.

To give some context: I instantiate the creeps in code
Here the shared collider is made in the start:

        var filter = new CollisionFilter
        {
            BelongsTo    = 1u << 0,                  // Creep layer
            CollidesWith = (1u << 0) | (1u << 1),        // Creep | World   (world is just 4 bounding boxes around the map)
            GroupIndex   = 0
        };
        var material = new Unity.Physics.Material { Friction = 0f, Restitution = 0f };
        _creepSphere = Unity.Physics.SphereCollider.Create(
            new SphereGeometry { Center = float3.zero, Radius = math.max(0.001f, colliderRadius) },
            filter, material);

and i use this to spawn the object in the subscene:

#
Entity creepEntity = entityManager.CreateEntity(creepArchetype);  //  (only transform)
            
// Put the creep in the default physics world (index 0)
            entityManager.AddSharedComponentManaged(
                creepEntity,
                new PhysicsWorldIndex { Value = 0 }
            );
            
            entityManager.SetComponentData(creepEntity, new LocalTransform
            {
                Position = new Unity.Mathematics.float3(spawnPos.x, spawnPos.y, 0),
                Rotation = Unity.Mathematics.quaternion.identity,
                Scale = 1f
            });
            
            // Collider (shared blob)
            entityManager.AddComponentData(creepEntity, new PhysicsCollider { Value = _creepSphere });

            var massProps = _creepSphere.Value.MassProperties;
            var pm = PhysicsMass.CreateDynamic(massProps, creepMass);
            pm.InverseInertia = float3.zero; // locks rotation about X/Y/Z
            entityManager.AddComponentData(creepEntity, pm);

            entityManager.AddComponentData(creepEntity, new PhysicsVelocity { Linear = new float3(3,0,0), Angular = float3.zero });
            entityManager.AddComponentData(creepEntity, new PhysicsDamping { Linear = math.max(0, linearDamping), Angular = 0f });
            entityManager.AddComponentData(creepEntity, new PhysicsGravityFactor { Value = 0f });

For now the game is just objects going from left to right and they collide with the 4 world boundaries and each other (2d).
The Objects have no animation for now, so it is just physics.

So I really cant believe that this is already the Engine limit, so I must do something wrong...
I also dont understand why I cant improve performance with my ECS setup.

I would be really grateful for some ideas and feedbacks 🙂

My computer specs are Ryzen7900x, 128gb ram, rtx 3900.

#

I also already did some performance tweaks to the physics:

                step.Gravity              = float3.zero;
                step.SolverIterationCount = 1;
slate ferry
gaunt moonBOT
wet shell
slate ferry
#

I don't think messages can be moved

void stratus
#

Also, anytime you think performance, the first thing you should do is profile the game with the profiler. And share the profiling data(screenshots would do) if you still have a question after that.

wet shell
#

Thanks for the suggestion here is the profiler output. However, I dont really know how to interpret it.