• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by vorg · Jul 06, 2010 at 09:46 AM · physicsoptimization

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spinaljack · Jul 06, 2010 at 09:50 AM 1
Share

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

avatar image Ricardo · Jul 06, 2010 at 11:17 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fherbst · Jul 06, 2010 at 01:15 PM 0
Share

I also remembered the term for it: short-circuit evaluation, http://en.wikipedia.org/wiki/Short-circuit_evaluation.

avatar image Mike 3 · Jul 06, 2010 at 01:24 PM 2
Share

even faster - use sqr$$anonymous$$agnitude, and check it's less than $$anonymous$$athf.Pow(a.radius + b.radius, 2)

avatar image jonas-echterhoff ♦♦ · Jul 06, 2010 at 03:12 PM 0
Share

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.

avatar image fherbst · Jul 06, 2010 at 06:29 PM 0
Share

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.

avatar image vorg · Jul 08, 2010 at 08:38 AM 0
Share

O$$anonymous$$, then, can I disable full testing somehow? Just the first test would be enough for me.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Can I stop a lerp completely when it gets to 20% of its original value? 3 Answers

Ok I really Dont Know how to use unity But how do i make like a game with a start screen were u pick like (single player, Multiplayer, Training) and how would i go around doing that But my big question is how to make a start screen and log in 0 Answers

Is a collider that enables/disables considered stationary? 2 Answers

Physics Optimization. 0 Answers

Unity Profiler - Physics optimization (FPS drop) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges