GodotComponentTest/utils/Plane2D.cs

47 lines
1.0 KiB
C#
Raw Normal View History

2023-08-13 21:18:46 +02:00
using System;
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) <= Plane2D.DistancePrecision);
}
public float DistanceToLineSegment(Vector2 point, Vector2 dir)
{
float normalDotDir = Normal.Dot(dir);
if (Mathf.Abs(normalDotDir) > Plane2D.DistancePrecision)
{
return (_planePoint.Dot(Normal) - point.Dot(Normal)) / normalDotDir;
}
if (normalDotDir < 0)
{
return Single.PositiveInfinity;
}
return Single.NegativeInfinity;
}
}