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 argentummanusbeta · May 18, 2020 at 06:06 AM · planegeometrylinesintersectioncalculations

Finding the intersection of vertical plane and scene mesh geometry

I need to find all the intersections of an upright plane with the main character's position and rotation. I currently have some code that works but is spewing out some strange values. In my code, I convert the 3-dimensional edges of all the meshes into two-dimensional equations in the form of Ax + By = C with bounds. I make an equation for the plane with the same form and calculate their intersection. If the intersection is within the edge's bounds, it calculates the percentage along the line from point 1 to point 2 and uses that the calculate a 3d position where the intersection takes place.

Here is my code: public class Edge { public Vector3 p1; public Vector3 p2; public LineEquation Xequation; public LineEquation Yequation; public LineEquation Zequation; public BoxRange Range; public float Length;

         public Edge(Vector3 p, Vector3 pp)
         {
             p1 = p;
             p2 = pp;
 
             float x = Mathf.Abs(p.x - pp.x);
             float y = Mathf.Abs(p.y - pp.y);
             float z = Mathf.Abs(p.z - pp.z);
 
             Xequation = new LineEquation(new Vector2(p.y, p.z), new Vector2(pp.y, pp.z));
             Yequation = new LineEquation(new Vector2(p.x, p.z), new Vector2(pp.x, pp.z));
             Zequation = new LineEquation(new Vector2(p.x, p.y), new Vector2(pp.x, pp.y));
 
             Range = new BoxRange(p, pp);
 
             Length = (p - pp).sqrMagnitude;
         }
     }
     public class LineEquation
     {
         public float A, B, C;
         public LineEquation(float a, float b, float c)
         {
             A = a;
             B = b;
             C = c;
         }
         public LineEquation(Vector2 p1, Vector2 p2)
         {
             A = p2.y - p1.y;
             B = p1.x - p2.x;
             C = A * p1.x + B * p1.y;
         }
         public LineEquation(Vector2 p, float angle)
         {
             angle *= Mathf.Deg2Rad;
             Vector2 p2 = new Vector2(p.x + Mathf.Sin(angle), p.y + Mathf.Cos(angle));
 
             A = p2.y - p.y;
             B = p.x - p2.x;
             C = A * p.x + B * p.y;
         }
     }
     public class BoxRange
     {
         public Vector3 lowerBound;
         public Vector3 higherBound;
 
         public BoxRange(Vector3 p1, Vector3 p2)
         {
             lowerBound = new Vector3(
                 Mathf.Min(p1.x, p2.x),
                 Mathf.Min(p1.y, p2.y),
                 Mathf.Min(p1.z, p2.z)
             );
             higherBound = new Vector3(
                 Mathf.Max(p1.x, p2.x),
                 Mathf.Max(p1.y, p2.y),
                 Mathf.Max(p1.z, p2.z)
             );
         }
     }
     public class PlaneCaster
     {
         public Edge[] edges;
         public string layer { get; private set; }
 
         public PlaneCaster(string tagLayer)
         {
             layer = tagLayer;
 
             GameObject[] list = GameObject.FindGameObjectsWithTag(tagLayer);
             List<Edge> buffer = new List<Edge>();
             for (int i = 0; i < list.Length; i++)
             {
                 Vector3 scale = list[i].transform.localScale;
                 Vector3 pos = list[i].transform.position;
                 Mesh mesh = list[i].GetComponent<MeshFilter>().sharedMesh;
                 Vector3[] vertices = mesh.vertices;
                 int[] triangles = mesh.triangles;
 
                 for (int j = 0; j < triangles.Length / 3; j++)
                 {
                     Edge edge1 = new Edge(Vector3.Scale(vertices[triangles[j * 3]], scale) + pos, Vector3.Scale(vertices[triangles[j * 3 + 1]], scale) + pos);
                     Edge edge2 = new Edge(Vector3.Scale(vertices[triangles[j * 3 + 1]], scale) + pos, Vector3.Scale(vertices[triangles[j * 3 + 2]], scale) + pos);
                     Edge edge3 = new Edge(Vector3.Scale(vertices[triangles[j * 3 + 2]], scale) + pos, Vector3.Scale(vertices[triangles[j * 3]], scale) + pos);
 
                     if (!CheckMatch(buffer, edge1)) buffer.Add(edge1);
                     if (!CheckMatch(buffer, edge2)) buffer.Add(edge2);
                     if (!CheckMatch(buffer, edge3)) buffer.Add(edge3);
                 }
             }
 
             edges = buffer.ToArray();
         }
 
         public void DrawPlaneRay(Vector3 pos, float rot)
         {
             rot *= Mathf.Deg2Rad;
             LineEquation plane = new LineEquation(new Vector2(pos.x, pos.z), rot);
             Debug.DrawRay(pos, new Vector3(Mathf.Sin(rot), 0f, Mathf.Cos(rot)), Color.green, 0.1f, false);
         }
 
         public void DrawDebug()
         {
             for (int i = 0; i < edges.Length; i++)
             {
                 Debug.DrawLine(edges[i].p1, edges[i].p2, Color.red, 2f, false);
             }
         }
 
         private bool CheckMatch(List<Edge> buff, Edge check)
         {
             for (int i = 0; i < buff.Count; i++)
             {
                 if (
                     (buff[i].p1 == check.p1 && buff[i].p2 == check.p2) ||
                     (buff[i].p1 == check.p2 && buff[i].p2 == check.p1)
                 )
                 {
                     return true;
                 }
             }
             return false;
         }
 
         public List<Vector3> Cast(Vector3 pos, string axis, float rot)
         {
             List<Vector3> result = new List<Vector3>();
 
             LineEquation plane;
             if (axis == "x") plane = new LineEquation(new Vector2(pos.y, pos.z), rot);
             else if (axis == "y") plane = new LineEquation(new Vector2(pos.x, pos.z), rot);
             else if (axis == "z") plane = new LineEquation(new Vector2(pos.x, pos.y), rot);
             else throw new Exception("Undefined Axis");
 
             for (int i = 0; i < edges.Length; i++)
             {
                 try
                 {
                     Vector2 reference;
                     if (axis == "x") reference = new Vector2(edges[i].p1.y, edges[i].p1.z);
                     else if (axis == "y") reference = new Vector2(edges[i].p1.x, edges[i].p1.z);
                     else if (axis == "z") reference = new Vector2(edges[i].p1.x, edges[i].p1.y);
                     else throw new Exception("Undefined Axis");
                     Vector2 intercept = GetIntercept(i, axis, plane);
 
                     if (
                         (axis == "x" && intercept.x > edges[i].Range.lowerBound.z && intercept.x < edges[i].Range.higherBound.z && intercept.y > edges[i].Range.lowerBound.y && intercept.y < edges[i].Range.higherBound.y) ||
                         (axis == "y" && intercept.x > edges[i].Range.lowerBound.x && intercept.x < edges[i].Range.higherBound.x && intercept.y > edges[i].Range.lowerBound.z && intercept.y < edges[i].Range.higherBound.z) ||
                         (axis == "z" && intercept.x > edges[i].Range.lowerBound.x && intercept.x < edges[i].Range.higherBound.x && intercept.y > edges[i].Range.lowerBound.y && intercept.y < edges[i].Range.higherBound.y)
                         )
                     {
                         float dist = (reference - intercept).sqrMagnitude;
                         float perc = dist / edges[i].Length;
 
                         result.Add((edges[i].p1 * (1f - perc)) + (edges[i].p2 * perc));
                     }
                 }
                 catch(Exception e)
                 {
 
                 }
             }
 
             return result;
         }
 
         public Vector2 GetIntercept(int index, string axis, LineEquation eq)
         {
             LineEquation line;
             if (axis == "x") line = edges[index].Xequation;
             else if (axis == "y") line = edges[index].Yequation;
             else if (axis == "z") line = edges[index].Zequation;
             else
             {
                 throw new Exception("Axis is Incorrect");
             }
 
             float delta = line.A * eq.B - eq.A * line.B;
 
             if (delta == 0)
                 throw new Exception("Lines are Paralell");
 
             float x = (eq.B * line.C - line.B * eq.C) / delta;
             float y = (line.A * eq.C - eq.A * line.C) / delta;
 
             return new Vector2(x, y);
         }
     }

alt text

alt text

In the images above, the green line represents the plane's rotation, the red lines are all the calculated edges in the scene and the blue lines are lines going from the main player to the points of intersection. In theory, all of the blue lines should be parallel with the green line. Any help in finding the problem with my code is appreciated.

capture.png (52.4 kB)
capture1.png (96.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

0 Replies

· Add your reply
  • Sort: 

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

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

Point on a Plane between two points 1 Answer

Check if mesh.bounds intersect a plane 1 Answer

Detecting object inside an area. 1 Answer

How to find symmetry plane of an object 1 Answer

Segment, plane intersection 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