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
0
Question by aaronfjerstad · Oct 29, 2017 at 12:47 AM · raycastcollidercollidersraycasting

Even-odd test with raycasting

An easy way to determine if a point is inside a finite, closed shape is to cast a ray and count how many times it intersects the surface. If it's odd, the point is inside. If it's even, the point was outside.

The proof is trivial. If a point in a finite closed shape moves along a ray, any time it passes through the surface it switches between being inside or outside the object.

A naive approach to solving this would be to run a ray from a point outward, store the point, and continue - toggling an odd/even Boolean each time. The problem is that rays in Unity can't intersect a Collider when coming from the inside. According to the Physics.Raycast() documentation.

Notes: Raycasts will not detect Colliders for which the Raycast origin is inside the Collider.

Well, that's fun. So what now?

EDIT

Based off Bunny83's helpful insights, I was able to solve the problem. Here is the code for anyone in the future who comes across this question:

 private bool HasOddParity(Collider collider, Vector3 point)
 {
     float magnitude = collider.bounds.size.magnitude * 2;
     bool odd = false;
 
     Vector3 outward = new Vector3 (1, 0, 0);
     Vector3 inward = new Vector3(-1, 0, 0);
     Ray currentRay = new Ray(point, outward);
     RaycastHit hit;
 
     //Near cast
     while (collider.Raycast(currentRay, out hit, magnitude))
     {
         odd = !odd;
         currentRay = new Ray(hit.point, outward);
     }
 
     Vector3 farPoint = Vector3.MoveTowards(currentRay.origin, currentRay.origin + (outward * magnitude), magnitude);
     currentRay = new Ray(farPoint, inward);
 
     //Far cast
     while (collider.Raycast(currentRay, out hit, magnitude))
     {
         if (hit.point.x > point.x)
         {
             odd = !odd;
             currentRay = new Ray(hit.point, inward);
         }
         else break;
     }
 
     return odd;
 }

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 Bunny83 · Oct 29, 2017 at 01:38 AM

You can simply use a linecast from outside to test the other sides of the collider.

Note that if you want to test a single collider, keep in mind that the Collider class has it's own Raycast method which will only raycast against this single collider. So nothing else can block the ray.

So start a line or raycast from your point inside and continue outwards until you don't hit anything. Then pick a far point outside and do a line cast from that far point to the point you want to test and go the route in reverse.

Comment
Add comment · Show 1 · 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 Bunny83 · Oct 29, 2017 at 01:40 AM 0
Share

Of course ins$$anonymous$$d of a line cast you can use a raycast as well, you just have to choose your ray length carefully to end at your point.

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

113 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 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 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 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 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 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

How can I let the ray to the edge of the capsule? 1 Answer

RaycastHit returns object without collider, in wrong layer 1 Answer

Raycast Collider Tag not returning correct result 1 Answer

Raycasting to make Buildings Clickable 1 Answer

raycast not colliding 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