Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Dynamically changin size of colliders

Discussion in 'Physics' started by Hellothere_1, Nov 9, 2018 at 5:05 PM.

  1. Hellothere_1

    Hellothere_1

    Joined:
    Sep 18, 2018
    Posts:
    8
    I'm currently working on a 2D collision avoidance algorithm.

    One of the ideas I had is to attach a trigger-collider to all moving objects and have it shrink or grow proportional to object speed (since faster objects need longer to brake and thus need to check for obstacles in a larger area.)

    However since I don't know what kind of optimization algorithms unity uses for it's physics calculations I'm a bit worried that constantly changing the size of the collider might be a larger stain upon performance that just sticking to the larger size or maybe switching between two preset sizes for fast or slow objects.

    Is changing the size of colliders something that affects performance, or do the colliders get recalculated from scratch every tick anyway?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,500
    You should not be changing the size of colliders if you can avoid it but doing to a handful is hardly going to cause any problems but the typical and scalable solution is to simply use physics queries to determine colliders around you. Queries such as OverlapCircle, OverlapBox, OverlapCapsule etc are perfect.
     
  3. Hellothere_1

    Hellothere_1

    Joined:
    Sep 18, 2018
    Posts:
    8
    Could you explain to me why physics queries are better that colliders for this?

    My (admittedly uninformed) guess was that colliders would be cheaper since each intersection would only have to be checked once, whereas with physics queries object A would first cast a query to detect B and then B would cast another query to detect A, thus doubleling the number of effective calculations.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,500
    Firstly I didn't answer this specifically. Colliders don't get resized each simulation tick because why would they if they are not resizing. So no, there's no overhead if there's nothing done.

    If you resize a collider, its geometry has to be recalculated (don't do this for a PolygonCollider2D as that can be expensive), considered if it affects the mass but ultimately ignored if it's a trigger then all contacts are calculated each step and reported via callbacks. Assuming then you also have scriptings in these callbacks which have to process the contacts to determine if these are of interested too can add up. You might find not doing that and performing queries which are very fast give you better performance and are simpler to handle but ultimately it's up to you.

    Without being able to run your game or do profiling I cannot state which will actually be better for performance for you but modifying colliders can become much more costly than running simple queries. Only you can determine which is better for you however using the profiler. In the end, you might have so few that the difference is negligible.
     
    Hellothere_1 likes this.
  5. Hellothere_1

    Hellothere_1

    Joined:
    Sep 18, 2018
    Posts:
    8
    Thanks, that already helped me out a lot.