More physics error checks, ragdoll clamps the velocity returned by PhysicsBody.CorrectPosition to prevent applying excessively high velocities to the collider if the position has changed significantly between frames

This commit is contained in:
Joonas Rikkonen
2018-07-23 17:58:02 +03:00
parent a373c589d1
commit 25a62b3a51
3 changed files with 58 additions and 18 deletions
@@ -453,7 +453,7 @@ namespace Barotrauma
float newRotation = msg.ReadRangedSingle(0.0f, MathHelper.TwoPi, 7);
bool awake = msg.ReadBoolean();
Vector2 newVelocity = Vector2.Zero;
if (awake)
{
newVelocity = new Vector2(
@@ -461,6 +461,19 @@ namespace Barotrauma
msg.ReadRangedSingle(-MaxVel, MaxVel, 12));
}
if (!MathUtils.IsValid(newPosition) || !MathUtils.IsValid(newRotation) || !MathUtils.IsValid(newVelocity))
{
string errorMsg = "Received invalid position data for the item \"" + Name
+ "\" (position: " + newPosition + ", rotation: " + newRotation + ", velocity: " + newVelocity + ")";
#if DEBUG
DebugConsole.ThrowError(errorMsg);
#endif
GameAnalyticsManager.AddErrorEventOnce("Item.ClientReadPosition:InvalidData" + ID,
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
errorMsg);
return;
}
if (body == null)
{
DebugConsole.ThrowError("Received a position update for an item with no physics body (" + Name + ")");
@@ -470,7 +483,7 @@ namespace Barotrauma
body.FarseerBody.Awake = awake;
if (body.FarseerBody.Awake)
{
if ((newVelocity - body.LinearVelocity).Length() > 8.0f) body.LinearVelocity = newVelocity;
if ((newVelocity - body.LinearVelocity).LengthSquared() > 8.0f * 8.0f) body.LinearVelocity = newVelocity;
}
else
{
@@ -490,11 +503,12 @@ namespace Barotrauma
if ((newPosition - SimPosition).Length() > body.LinearVelocity.Length() * 2.0f)
{
body.SetTransform(newPosition, newRotation);
Vector2 displayPos = ConvertUnits.ToDisplayUnits(body.SimPosition);
rect.X = (int)(displayPos.X - rect.Width / 2.0f);
rect.Y = (int)(displayPos.Y + rect.Height / 2.0f);
if (body.SetTransform(newPosition, newRotation))
{
Vector2 displayPos = ConvertUnits.ToDisplayUnits(body.SimPosition);
rect.X = (int)(displayPos.X - rect.Width / 2.0f);
rect.Y = (int)(displayPos.Y + rect.Height / 2.0f);
}
}
}