mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Fixes a rare out of bounds exception (#313)
* Fixes a rare crash Fixes a crash that occurs from the following >return AngleDirections[(int)Math.Floor(angle/45f)]; System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' That occurs most likely due to a float rounding error * Apparently this works fine
This commit is contained in:
committed by
Pieter-Jan Briers
parent
4607a106d0
commit
a43da12f9b
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
|
||||
|
||||
namespace SS14.Shared.Maths
|
||||
@@ -57,10 +57,19 @@ namespace SS14.Shared.Maths
|
||||
{
|
||||
return radians/Pi*180;
|
||||
}
|
||||
public static float ToRadians(float degrees)
|
||||
{
|
||||
return degrees/180*Pi;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static float ToRadians(float degrees)
|
||||
{
|
||||
return degrees / 180 * Pi;
|
||||
}
|
||||
|
||||
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
|
||||
{
|
||||
if (val.CompareTo(min) < 0) return min;
|
||||
else if (val.CompareTo(max) > 0) return max;
|
||||
else return val;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using SFML.Graphics;
|
||||
using SFML.Graphics;
|
||||
using SFML.System;
|
||||
using System;
|
||||
|
||||
@@ -109,7 +109,8 @@ namespace SS14.Shared.Maths
|
||||
// Add 22.5° to offset the angles since 0° is inside one.
|
||||
angle += 22.5f;
|
||||
|
||||
return AngleDirections[(int)Math.Floor(angle/45f)];
|
||||
int dirindex = FloatMath.Clamp((int)Math.Floor(angle / 45f), 0, 8);
|
||||
return AngleDirections[dirindex];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user