Skip to content

Instantly share code, notes, and snippets.

@StagPoint
Created September 25, 2018 17:57
Show Gist options
  • Save StagPoint/2eaa878f151555f9f96ae7190f80352e to your computer and use it in GitHub Desktop.
Save StagPoint/2eaa878f151555f9f96ae7190f80352e to your computer and use it in GitHub Desktop.
Find intersection point of three planes in C# for Unity
private bool planesIntersectAtSinglePoint( Plane p0, Plane p1, Plane p2, out Vector3 intersectionPoint )
{
const float EPSILON = 1e-4f;
var det = Vector3.Dot( Vector3.Cross( p0.normal, p1.normal ), p2.normal );
if( det < EPSILON )
{
intersectionPoint = Vector3.zero;
return false;
}
intersectionPoint =
( -( p0.distance * Vector3.Cross( p1.normal, p2.normal ) ) -
( p1.distance * Vector3.Cross( p2.normal, p0.normal ) ) -
( p2.distance * Vector3.Cross( p0.normal, p1.normal ) ) ) / det;
return true;
}
@StagPoint
Copy link
Author

StagPoint commented Oct 26, 2021

Hmmm... That sounds interesting, I think I'll give it a try.

It seems likely that to me that if that's correct, I missed it because the specific project I'm currently using this in is pretty narrowly scoped (always a convex hull, with plane normals always pointing in towards the center of a symmetric enclosed volume so det is always positive for a valid solution, strict minimum size, etc).

Thanks for the heads-up 👍

@StagPoint
Copy link
Author

StagPoint commented Oct 26, 2021

Hmm... Or it might be that the code above really only works for such situations, and will fall in cases where there's a greater than 90° angle between the plane normals, even if they do indeed all meet at a single point.

Checking the absolute value of det would really only be checking if the planes were nearly coplanar, right?

@cjzswang5
Copy link

In my project, it works very well if I use Mathf.Abs(det). I haven't figured out the intersectionPoint formula, but it seems to be applicable to all situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment