What is the use-case you have in mind? Depending on what you mean by collision count (# Collision Pairs, # Contact Points, # Collision Events) there are different answers.
This. I want to prevent too many push collisions by changing the speed of the physical objects. Just need to change a coefficient depending on the current number of collisions.
You can use an ICollisionEventsJob to count the collisions on specific shapes that are flagged to raise them An IContactsJob can count low level contacts (though this will be for all bodies). An IBodyPairsJob could count overlaps (though again this is for all bodies. The latter 2 options mean you'd be going over all contact or pairs and you'd need to filter the count if you are only interested in a specific body/bodies. On that note, if you are only interested on details of a single body, then a specific PhysicsWorld.CalculateDistance query might be the more optimal approach.
As I understood it is executed in one thread? I have 10k+ rigidbodies which interact with each other and have tried ICollisionEventsJob and it took a very long time in one thread. I thought there was a simple way like m_query.CalculateEntityCount().
The ICollisionEventsJob certainly needs parallelized, but is a common interface between the Unity Physics and the Havok Physics simulation backend. It's also pretty safe in terms of giving valid information at the appropriate time. If you are sure about your scheduling, only interested in the Unity Physics simulation, and are happy to make local modifications to the com.unity.physics package you could try adding a Count property to the CollisionEvents struct e.g. public int Count => m_EventDataStream.IsCreated ? m_EventDataStream.Count() >> 1 : 0; Then you can use the property like var numCollisionEvents = (World.GetOrCreateSystem<StepPhysicsWorld>().Simulation as Unity.Physics.Simulation).CollisionEvents.Count; You need to be careful when you read it though to ensure that the StepPhysicsWorld jobs are finished adding events to the underlying NativeStream.