2019-02-01 17:19:20 +00:00
|
|
|
|
namespace RobloxFiles.DataTypes
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
2019-02-01 18:40:39 +00:00
|
|
|
|
public class Ray
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
|
|
|
|
public readonly Vector3 Origin;
|
|
|
|
|
public readonly Vector3 Direction;
|
|
|
|
|
|
2019-11-01 02:40:31 +00:00
|
|
|
|
public override string ToString() => $"{{{Origin}}}, {{{Direction}}}";
|
|
|
|
|
|
2019-01-26 00:39:37 +00:00
|
|
|
|
public Ray Unit
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
Ray unit;
|
|
|
|
|
|
|
|
|
|
if (Direction.Magnitude == 1.0f)
|
|
|
|
|
unit = this;
|
|
|
|
|
else
|
|
|
|
|
unit = new Ray(Origin, Direction.Unit);
|
|
|
|
|
|
|
|
|
|
return unit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 22:01:19 +00:00
|
|
|
|
public Ray(Vector3 origin = null, Vector3 direction = null)
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
2019-06-30 22:01:19 +00:00
|
|
|
|
Origin = origin ?? new Vector3();
|
|
|
|
|
Direction = direction ?? new Vector3();
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector3 ClosestPoint(Vector3 point)
|
|
|
|
|
{
|
2019-02-01 18:40:39 +00:00
|
|
|
|
Vector3 result = Origin;
|
2019-02-04 19:30:33 +00:00
|
|
|
|
float dist = Direction.Dot(point - result);
|
2019-02-01 18:40:39 +00:00
|
|
|
|
|
2019-02-04 19:30:33 +00:00
|
|
|
|
if (dist >= 0)
|
|
|
|
|
result += (Direction * dist);
|
2019-02-01 18:40:39 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Distance(Vector3 point)
|
|
|
|
|
{
|
2019-02-01 18:40:39 +00:00
|
|
|
|
Vector3 closestPoint = ClosestPoint(point);
|
2019-02-04 19:30:33 +00:00
|
|
|
|
return (point - closestPoint).Magnitude;
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
2020-09-13 01:16:19 +00:00
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
2021-02-18 19:15:08 +00:00
|
|
|
|
if (!(obj is Ray other))
|
2020-09-13 01:16:19 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!Origin.Equals(other.Origin))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!Direction.Equals(other.Direction))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
int hash = Origin.GetHashCode()
|
|
|
|
|
^ Direction.GetHashCode();
|
|
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|