Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by DasSchwarz · Aug 13, 2011 at 07:14 PM · physicsoptimizationcolliders

How to optimize hundreds of sphere colliders

Hi,

I realize that this is quite ambitious, but I should explain my reasoning why. The game I am creating spawns hundreds of friendly fighters ships, each of which have an enemy detector. This detector is basically an invisible sphere collider that "sees" if any enemy is too close; if so, it chases it.

Originally, friendly ships hit each others' detector, causing performance issues. So I decided to make them ignore each others' detectors by using layers and performance improved. I can make about 400 or so ships now before things really slow down. Without the enemy detector colliders performance is even better -- but those detectors are necessary.

Any thoughts? I suppose I should probably use some kind of grid, kdtree, or data structure to somehow help performance, although I'm not sure where to start. I'm also not sure if unity's internal engine helps in this regard.

Thank you

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 Eric5h5 · Aug 13, 2011 at 09:09 PM 0
Share

PhysX already uses performance optimizations, so at some point you just need to limit the number of objects you're using. CPU power is finite and can only do so much.

avatar image DasSchwarz · Aug 13, 2011 at 09:47 PM 0
Share

I feel like there is a way around this. I.e. if there are no enemies your part of the map, disable the detector. The problem is that lots of Unity stuff is abstracted away, so I would have to make intelligent use of its resources.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Aug 13, 2011 at 07:50 PM

Do you have any rigid body on them? If you use any collider without a rigid body, and move them, physx will bake the static collision data causing performance hits. If you haven't got rigid bodies on them, just slap them on and set isKinematic to true and you should see some improvement.

Another thing you could try is to perform a Physics.OverlapSphere every 0.1 seconds for detection purposes, but that would also require a collider on them.

Another bit of speculation is the scale of your objects. I don't know in depth how this work but generally, if physx uses some octree and your objects are too small (all inside one bucket) then collision detection should be slow since it can't efficiently cull away the majority of objects. This is just some thought I had, but I don't know the scale of your game, neither do I know any optimal scale for the physics engine, if any applies.

Comment
Add comment · Show 3 · 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 DasSchwarz · Aug 13, 2011 at 08:31 PM 0
Share

Thanks. I do have rigidbodies attached to the enemy detector. Here's the hierarchy for each fighter:

-Fighter prefab: script, sphere collider [radius = 0.5] to detect direct collisions with enemies + kinematic rigidbody. Scale: (1.0, 1.0, 0.65). Children of prefab:

a)EnemyDetector (script, trigger-enabled sphere collider of radius 4.0 to "see objects" some distance away, kinematic rigidbody). Scale: (1.0, 1.0, 1.0)

b)Geometry (sphere mesh plus material). Scale: (1.0, 1.0, 1.0)

I have a feeling that Physics.OverlapSphere may still cause issues. But keep in $$anonymous$$d that this slowdown occurs even when no collisions occur (I have breakpoints set in $$anonymous$$onoDevelop), and whether or not the enemy detector is a trigger -- so basically it can't hit anything. The enemy detector's sphere collider size and the fact that it's enabled may cause slowdowns.

I've listed the scale above. I believe its physics engine might get bogged down with scale values besides 1.0.

avatar image Statement · Aug 13, 2011 at 08:57 PM 0
Share

What does your profiler (if you have access to it) say?

avatar image DasSchwarz · Aug 13, 2011 at 09:49 PM 0
Share

The profiler is off-limits to me as I am using free Unity, unfortunately.

avatar image
0

Answer by Julien-Lynge · Aug 13, 2011 at 09:12 PM

Could you do away with the colliders altogether and just use Vector3.Distance.sqrMagnitude calls? In your Start() function, create a list of all the fighters using GameObject.FindGameObjectsWithTag and then iterate through this list? Possibly iterate through on a coroutine that batches the checks so that some subset gets checked every frame? I honestly don't know if 400^2 simple float operations would be faster than 400 collider checks by the physics engine, but it would be relatively quick to try.

Comment
Add comment · Show 2 · 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 Eric5h5 · Aug 13, 2011 at 09:37 PM 0
Share

You'd be comparing every fighter to every other fighter, so this is definitely going to be slower than octree optimizations (or whatever it uses exactly) that the physics engine has.

avatar image DasSchwarz · Aug 13, 2011 at 10:13 PM 0
Share

I would normally agree with Eric5h5, but strangely enough, this type of test is a bit faster...here's what I did:

--I kept the enemy detector's sphere collider but made it a non-trigger so may detection code does nothing. I made 450 fighters, resulting in 5-6 fps when fighters are NOT being rendered on screen (camera is not facing them).

--I then disabled the sphere collider for the enemy detector entirely, and in the enemy detector's Update() function I wrote similar to the one found here: http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html (FindClosestEnemy). Except I found stuff with tag "FighterShip" (shared by friend AND enemy fighters). About 15 fps, again no fighters not being rendered on screen.

I'm not sure why the distance method would work better. $$anonymous$$eep in $$anonymous$$d that it doesn't really scale well though (there are only 450 friendly fighters here, no enemies), so a data structure solution works best....or some kind of batching thing. I wonder how Unity does it...

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help! how to convert input.GetAxis to Accelerometer 0 Answers

Model tips over for no reason 1 Answer

Problem with Colliders 1 Answer

2D 360 degress platformer example needed 0 Answers

How to check is object directly next to another object? 1 Answer


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