Added a bunch of checks to make sure a normalized zero vector (= NaN, NaN) is not used in any position/velocity/movement calculations. There were at least three places where it was causing problems according to error reports: when a character that can't enter a sub spawns at the center of a hull and when using an underwater scooter or throwing something while the cursor is at the position of the character, but there were tons of other places as well where it may have potentially caused physics errors.

This commit is contained in:
Joonas Rikkonen
2018-07-31 12:28:04 +03:00
parent d881a7060b
commit d81ee1a27e
13 changed files with 56 additions and 29 deletions
@@ -74,6 +74,8 @@ namespace Barotrauma.Items.Components
}
Vector2 dir = Vector2.Normalize(character.CursorPosition - character.Position);
//move upwards if the cursor is at the position of the character
if (!MathUtils.IsValid(dir)) dir = Vector2.UnitY;
Vector2 propulsion = dir * force;
@@ -94,8 +94,9 @@ namespace Barotrauma.Items.Components
if (throwPos < -0.0)
{
Vector2 throwVector = picker.CursorWorldPosition - picker.WorldPosition;
throwVector = Vector2.Normalize(throwVector);
Vector2 throwVector = Vector2.Normalize(picker.CursorWorldPosition - picker.WorldPosition);
//throw upwards if cursor is at the position of the character
if (!MathUtils.IsValid(throwVector)) throwVector = Vector2.UnitY;
GameServer.Log(picker.LogName + " threw " + item.Name, ServerLog.MessageType.ItemInteraction);
@@ -116,7 +116,7 @@ namespace Barotrauma.Items.Components
else
{
diff.Y = 0.0f;
if (diff != Vector2.Zero && diff.Length() > 10.0f)
if (diff != Vector2.Zero && diff.LengthSquared() > 10.0f * 10.0f)
{
character.AnimController.TargetMovement = Vector2.Normalize(diff);
character.AnimController.TargetDir = diff.X > 0.0f ? Direction.Right : Direction.Left;
@@ -211,8 +211,9 @@ namespace Barotrauma.Items.Components
{
Vector2 diff = item.Submarine.WorldPosition - (Vector2)intersection;
//far enough -> ignore
if (diff.Length() > avoidRadius) continue;
float dist = diff.Length();
//far enough or too close to normalize the diff -> ignore
if (dist > avoidRadius || dist < 0.00001f) continue;
float dot = item.Submarine.Velocity == Vector2.Zero ?
0.0f : Vector2.Dot(item.Submarine.Velocity, -Vector2.Normalize(diff));
@@ -241,16 +242,15 @@ namespace Barotrauma.Items.Components
float otherSize = Math.Max(sub.Borders.Width, sub.Borders.Height);
Vector2 diff = item.Submarine.WorldPosition - sub.WorldPosition;
float dist = diff == Vector2.Zero ? 0.0f : diff.Length();
//far enough -> ignore
if (dist > thisSize + otherSize) continue;
diff = Vector2.Normalize(diff);
Vector2 dir = dist <= 0.0001f ? Vector2.UnitY : diff / dist;
float dot = item.Submarine.Velocity == Vector2.Zero ?
0.0f : Vector2.Dot(Vector2.Normalize(item.Submarine.Velocity), -Vector2.Normalize(diff));
0.0f : Vector2.Dot(Vector2.Normalize(item.Submarine.Velocity), -dir);
//heading away -> ignore
if (dot < 0.0f) continue;