GodotComponentTest/utils/Plane2D.cs

39 lines
989 B
C#

using Godot;
namespace GodotComponentTest.utils;
public class Plane2D {
public static readonly float DistancePrecision = 1.0e-5f;
private Vector2 _planePoint;
public Vector2 Normal;
public Plane2D(Vector2 planePoint, Vector2 normal) {
_planePoint = planePoint;
Normal = normal;
}
public float DistanceToPoint(Vector2 point) {
return (point - _planePoint).Dot(Normal);
}
public bool IsParallelToDir(Vector2 dir) {
float normalDotDir = Normal.Dot(dir);
return Mathf.Abs(normalDotDir) <= DistancePrecision;
}
public float DistanceToLineSegment(Vector2 point, Vector2 dir) {
float normalDotDir = Normal.Dot(dir);
if (Mathf.Abs(normalDotDir) > DistancePrecision) {
return (_planePoint.Dot(Normal) - point.Dot(Normal)) / normalDotDir;
}
if (normalDotDir < 0) {
return float.PositiveInfinity;
}
return float.NegativeInfinity;
}
}