|
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.
(comments are locked)
|
|
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:
GOOD implementation would be:
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: 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.
Jul 06 '10 at 01:15 PM
felix.
even faster - use sqrMagnitude, and check it's less than Mathf.Pow(a.radius + b.radius, 2)
Jul 06 '10 at 01:24 PM
Mike 3
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 sqrMagnitude as Mike wrote) is probably actually faster then a bounding volume test, because it has to do less branches.
Jul 06 '10 at 03:12 PM
jonas echterhoff ♦♦
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.
Jul 06 '10 at 06:29 PM
felix.
OK, then, can I disable full testing somehow? Just the first test would be enough for me.
Jul 08 '10 at 08:38 AM
vorg
(comments are locked)
|

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.