GodotComponentTest/utils/Plane2D.cs

39 lines
989 B
C#
Raw Permalink Normal View History

2023-08-13 21:18:46 +02:00
using Godot;
namespace GodotComponentTest.utils;
2023-11-18 22:32:57 +01:00
public class Plane2D {
2023-08-13 21:18:46 +02:00
public static readonly float DistancePrecision = 1.0e-5f;
private Vector2 _planePoint;
public Vector2 Normal;
2023-11-18 22:32:57 +01:00
public Plane2D(Vector2 planePoint, Vector2 normal) {
2023-08-13 21:18:46 +02:00
_planePoint = planePoint;
Normal = normal;
}
2023-11-18 22:32:57 +01:00
public float DistanceToPoint(Vector2 point) {
2023-08-13 21:18:46 +02:00
return (point - _planePoint).Dot(Normal);
}
2023-11-18 22:32:57 +01:00
public bool IsParallelToDir(Vector2 dir) {
2023-08-13 21:18:46 +02:00
float normalDotDir = Normal.Dot(dir);
2023-11-18 22:32:57 +01:00
return Mathf.Abs(normalDotDir) <= DistancePrecision;
2023-08-13 21:18:46 +02:00
}
2023-11-18 22:32:57 +01:00
public float DistanceToLineSegment(Vector2 point, Vector2 dir) {
2023-08-13 21:18:46 +02:00
float normalDotDir = Normal.Dot(dir);
2023-11-18 22:32:57 +01:00
if (Mathf.Abs(normalDotDir) > DistancePrecision) {
2023-08-13 21:18:46 +02:00
return (_planePoint.Dot(Normal) - point.Dot(Normal)) / normalDotDir;
}
2023-11-18 22:32:57 +01:00
if (normalDotDir < 0) {
return float.PositiveInfinity;
2023-08-13 21:18:46 +02:00
}
2023-11-18 22:32:57 +01:00
return float.NegativeInfinity;
2023-08-13 21:18:46 +02:00
}
}