Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by S_Darkwell · Apr 08, 2015 at 12:47 AM · collidertriggermathboundsarea

Percentage of Collider within a Trigger Area (C#)

Hello fellow Unity Developers,

I am attempting to create a C# function that linearly interpolates a Rigidbody’s drag based upon the percentage of the Rigidbody’s collider that is within a trigger collider "Water Region".

Considerations:

  • Water Regions will likely be convex and with relatively simple geometry, but they may sometimes be spherical and may not always be regular or quadrilateral.

  • The Rigidbody may enter the Water Region from any point and may be facing any direction while doing so.

While I am certain such is possible via a series of Bounds.Contains() checks, this would not be performant and would have a resolution limited by the number points being checked.

Is there an efficient or mathematical means of determining what percentage of a Collider is within a trigger zone?

Even partial solutions are absolutely welcome (eg: only works with rectangular / spherical Water Regions).

My absolute gratitude in advance!

- S. Darkwell

Comment
Add comment
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

1 Reply

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

Answer by S_Darkwell · Apr 16, 2015 at 12:46 PM

Below is a simplistic solution. It treats both the object and the region that the object is being tested against as quadrangles, but for the time being, it works for my purposes:

 /// <summary>
 /// Returns the percentage of obj contained by region. Both obj and region are calculated as quadralaterals for performance purposes.
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="region"></param>
 /// <returns></returns>
 private float BoundsContainedPercentage( Bounds obj, Bounds region )
 {
     var total = 1f;
 
     for ( var i = 0; i < 3; i++ )
     {
         var dist = obj.min[i] > region.center[i] ?
             obj.max[i] - region.max[i] :
             region.min[i] - obj.min[i];
 
         total *= Mathf.Clamp01(1f - dist / obj.size[i]);
     }
 
     return total;
 }

I hope that others may find the above helpful!

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 KasperCEGO · Aug 21, 2017 at 08:24 AM 0
Share

I don't want to be a killjoy, but it seems you'll need some sort of zero division check. I only noticed this trying it on a 2D object, since these still have their size defined in a Vector3 though z-size is 0.

This check will also be relevant if you animate or in other ways manipulate your objects to possibly reach a zero thickness in any one direction. Since my need for this only applied to 2D objects, I just went with i [0;1], i.e.

for (int i = 0; i < 2; i++)

but just add a check and you'll be fine to use it more generically ;)

avatar image Serellyn KasperCEGO · May 28, 2018 at 10:28 AM 0
Share

$$anonymous$$asperCEGO could you please explain some more and maybe give an example of what should be done to make this work for 2D objects? Thanks

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving object next to another object. 0 Answers

Can't click gameobject when over another trigger? 1 Answer

Instantiate GameObjects inside a collider 1 Answer

Layers, parenting and collisions. 0 Answers

why is OnTriggerEnter Destroy working once then stoping? 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