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;
|
|
|
|
|
|
|
|
|
|
public Ray Unit
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
Ray unit;
|
|
|
|
|
|
|
|
|
|
if (Direction.Magnitude == 1.0f)
|
|
|
|
|
unit = this;
|
|
|
|
|
else
|
|
|
|
|
unit = new Ray(Origin, Direction.Unit);
|
|
|
|
|
|
|
|
|
|
return unit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Ray(Vector3 origin, Vector3 direction)
|
|
|
|
|
{
|
|
|
|
|
Origin = origin;
|
|
|
|
|
Direction = direction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2019-01-30 06:36:56 +00:00
|
|
|
|
return '{' + Origin.ToString() + "}, {" + Direction.ToString() + '}';
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector3 ClosestPoint(Vector3 point)
|
|
|
|
|
{
|
2019-02-01 18:40:39 +00:00
|
|
|
|
Vector3 result = Origin;
|
|
|
|
|
float t = Direction.Dot(point - result);
|
|
|
|
|
|
|
|
|
|
if (t >= 0)
|
|
|
|
|
result += (Direction * t);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return (closestPoint - point).Magnitude;
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|