- Home /
Colliders slow for crowds?
I have 800 boids with SphereColliders on them. I use them to determine neighbor boids for each bird. At the beginning when the boids are close to each other I get lots of collisions and my frame rate drops to 5fps. But when they spread my frame rate jumps to 20fps.
Why the speed difference is so high? Is there some internal Unity/PhysX messaging that is so expensive? I don't do anything on OnTriggerEnter (yet) but obviously physics engine has to check for collisions between all the colliders every frame because I make them kinematic and animate them by myself.
You don't really need colliders to do the boids algorithm, the only thing I can suggest is to lower the radius of the spheres or make them solid so that there's no overlap of several colliders
Performance can depend on a host of things, including how you're detecting proximity. It would probably behoove you to first ensure that the physics are actually what's slowing you down.
Answer by fherbst · Jul 06, 2010 at 01:04 PM
The physics engine can accelerate the check whether objects do collide by checking whether the bounding boxes collide. This is very fast. Example of what the engine is probably doing:
BAD implementation would be:
// Checking for sphere collisions
if((a.position - b.position).magnitude < (a.radius + b.radius))
{
// the spheres collide
}
GOOD implementation would be:
var dist = a.radius + b.radius;
// first checking for bounding box collisions
if
(
(Mathf.Abs(a.position.x - b.position.x) < dist) &&
(Mathf.Abs(a.position.y - b.position.y) < dist) &&
(Mathf.Abs(a.position.z - b.position.z) < dist) &&
((a.position - b.position).magnitude < (a.radius + b.radius))
)
{
// there spheres collide
}
The second thing is incredibly faster, because it will stop checking when the first condition evaluates to "false" and only do the (slower) full check if all the other conditions meet. For example: A
is far left from B
, so there is no need to calculate the distance of them - you already know they don't collide.
But if all objects are close to each other, the full test has to be made, and this will really slow down - you notice it by the frame rate drop.
I also remembered the term for it: short-circuit evaluation, http://en.wikipedia.org/wiki/Short-circuit_evaluation.
even faster - use sqr$$anonymous$$agnitude, and check it's less than $$anonymous$$athf.Pow(a.radius + b.radius, 2)
While it is generically true that bounding box evaluations are used to speed up collision detection, in the case of two spheres, the test itself (using sqr$$anonymous$$agnitude as $$anonymous$$ike wrote) is probably actually faster then a bounding volume test, because it has to do less branches.
No, its not the actual sphere collision equation (which can be surely made faster) thats faster, its the short-circuit evaluation. "Nearly all" (as a mathematician would probably say) collision tests are finished after |a.x - b.x| < dist.
O$$anonymous$$, then, can I disable full testing somehow? Just the first test would be enough for me.