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 Karmate · May 16, 2015 at 06:07 PM · triggeroverlapareapercentage

How to check trigger overlap area? (with Image)

Helo, I'am wroking on a parking game. And I am stuck at what percentage of my car is overlapping to park area. Both use "Box Collider".alt text

1.png (439.4 kB)
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

Answer by AlwaysSunny · May 16, 2015 at 06:07 PM

If you can, just check whether four points (the four corners of your car) lie within the parking space's bounding volume. This is a much easier approach.

If you need a little more data, check the car's position and orientation against the parking spot's center and orientation. This allows testing a parking job's accuracy with a high degree of fidelity, and is computationally trivial. A distance of one quarter of a car width might be the worst possible "successful" park job, and the Dot Product of the car's forward and the parking spot's forward will give you a scalar value representing alignment. Definitely sounds like the way to go.


I mention the alternative below because it's useful in other situations, and can give you an intersection mesh, accurate overlap percentages, and other relevant data...

If you can simplify a problem to 2D, do so. Definitely looks like you can. This means discarding the Y axis information on your two boundary objects and conducting the overlap check in 2D.

Numerous discussions on this topic exist; it becomes a "simple" math problem. You might be able to find a geometry library that does such calculations for you with relative ease. Otherwise, you'll need to hunt down a helpful paper or article which discusses how to calculate the area of intersection between two rectangles. It should make for a long, boring, educational afternoon. ;)

Comment
Add comment · Show 5 · 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 Karmate · May 17, 2015 at 08:24 AM 0
Share

Thanks for answer, Can you give some codes for simplest solution? I don't get english very well.

avatar image AlwaysSunny · May 17, 2015 at 03:19 PM 1
Share

This is untested, but it should get you pretty close to a valid "accuracy checking" solution. Code goes on the "parking space" object. Code implies the "Z" axis of the parking space is the "long" axis.

 // Adjust these values as needed
 float $$anonymous$$Distance = 0.5f;
 float $$anonymous$$Dot = 0.85f;
 
 void OnTriggerStay(Collider other){
 
   if (other.tag != "Car") return; // must be a car
 
   float difference = car.position - transform.position;
   float distance = difference.magnitude;
   if (distance > $$anonymous$$Distance) return; // too far from center
 
   float dot = Vector3.Dot( car.forward, transform.forward );
   dot = $$anonymous$$athf.Abs(dot);
   if (dot < $$anonymous$$Dot) return; // too extreme angle
 
   float distanceAccuracy = $$anonymous$$athf.InverseLerp( $$anonymous$$Distance, 0, distance );
   float angleAccuracy = $$anonymous$$athf.InverseLerp( $$anonymous$$Dot, 1, dot );    
   // these two variables between 0..1 represent overall parking accuracy
 }
avatar image cjdev · May 17, 2015 at 06:00 PM 1
Share

That doesn't actually give the area of overlap that he asked for.

avatar image AlwaysSunny · May 17, 2015 at 06:30 PM 1
Share

No, but his comment asked for the "simplest solution" for measuring the accuracy of a parked car. This solution is valid for many placement-and-orientation checks. Boolean mesh operations are a much more detailed and complex topic than UA welcomes anyhow.

avatar image Karmate · May 17, 2015 at 09:22 PM 0
Share

AlwaysSunny, Thanks for code. I think this is enough. Thank you. I will update my answer after testing 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

Variable doesn't change when box hit trigger area 3 Answers

Manipulate gravity in an area? 1 Answer

Overlapping Triggers not working 0 Answers

How to use Physics2D.OverlapArea 2 Answers

Is OnTriggerEnter always fired when an object is already inside? 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