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;
}
@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