mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Add simple raycast method (#2937)
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
@@ -615,6 +616,72 @@ namespace Robust.Shared.Maths
|
||||
|
||||
#endregion InterpolateCubic
|
||||
|
||||
#region Intersections
|
||||
|
||||
// MIT License
|
||||
|
||||
// Copyright (c) 2019 Erin Catto
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
/// <summary>
|
||||
/// Gets the intersection between a line and a circle.
|
||||
/// Essentially a reduced raycast.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool TryGetIntersecting(Vector2 start, Vector2 end, float radius, [NotNullWhen(true)] out Vector2? point)
|
||||
{
|
||||
var maxFraction = (end - start).Length;
|
||||
float b = Vector2.Dot(start, start) - radius * radius;
|
||||
|
||||
// Solve quadratic equation.
|
||||
var r = end - start;
|
||||
float c = Vector2.Dot(start, r);
|
||||
float rr = Vector2.Dot(r, r);
|
||||
float sigma = c * c - rr * b;
|
||||
|
||||
// Check for negative discriminant and short segment.
|
||||
if (sigma < 0.0f || rr < float.Epsilon)
|
||||
{
|
||||
point = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find the point of intersection of the line with the circle.
|
||||
float a = -(c + MathF.Sqrt(sigma));
|
||||
|
||||
// Is the intersection point on the segment?
|
||||
if (0.0f <= a && a <= maxFraction * rr)
|
||||
{
|
||||
a /= rr;
|
||||
var lineToEnd = end - start;
|
||||
// a is a fraction so need to work out the distance along the line we need to be.
|
||||
point = start + lineToEnd * a;
|
||||
return true;
|
||||
}
|
||||
|
||||
point = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion Public Members
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user