- Home /
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);
}
}
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.
Your answer
Follow this Question
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