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:
clusterfack
2017-08-08 04:51:38 -05:00
committed by Pieter-Jan Briers
parent 4607a106d0
commit a43da12f9b
2 changed files with 19 additions and 9 deletions

View File

@@ -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;
}
}
}

View File

@@ -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>