Added a bunch of error checks to physicsbody-manipulating code
This commit is contained in:
@@ -360,6 +360,44 @@ namespace Barotrauma
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
private bool IsValidValue(float value, string valueName)
|
||||
{
|
||||
if (!MathUtils.IsValid(value))
|
||||
{
|
||||
string errorMsg =
|
||||
"Attempted to apply invalid " + valueName +
|
||||
" to a physics body (userdata: " + UserData == null ? "null" : UserData.ToString() +
|
||||
"), value: " + value + "\n" + Environment.StackTrace;
|
||||
|
||||
DebugConsole.ThrowError(errorMsg);
|
||||
GameAnalyticsManager.AddErrorEventOnce(
|
||||
"PhysicsBody.SetPosition:InvalidPosition",
|
||||
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
||||
errorMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool IsValidValue(Vector2 value, string valueName)
|
||||
{
|
||||
if (!MathUtils.IsValid(value))
|
||||
{
|
||||
string errorMsg =
|
||||
"Attempted to apply invalid " + valueName +
|
||||
" to a physics body (userdata: " + UserData == null ? "null" : UserData.ToString() +
|
||||
"), value: " + value + "\n" + Environment.StackTrace;
|
||||
|
||||
DebugConsole.ThrowError(errorMsg);
|
||||
GameAnalyticsManager.AddErrorEventOnce(
|
||||
"PhysicsBody.SetPosition:InvalidPosition",
|
||||
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
||||
errorMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ResetDynamics()
|
||||
{
|
||||
body.ResetDynamics();
|
||||
@@ -367,6 +405,7 @@ namespace Barotrauma
|
||||
|
||||
public void ApplyLinearImpulse(Vector2 impulse)
|
||||
{
|
||||
if (!IsValidValue(impulse, "impulse")) return;
|
||||
body.ApplyLinearImpulse(impulse);
|
||||
}
|
||||
|
||||
@@ -375,6 +414,9 @@ namespace Barotrauma
|
||||
/// </summary>
|
||||
public void ApplyLinearImpulse(Vector2 impulse, float maxVelocity)
|
||||
{
|
||||
if (!IsValidValue(impulse, "impulse")) return;
|
||||
if (!IsValidValue(maxVelocity, "max velocity")) return;
|
||||
|
||||
float currSpeed = body.LinearVelocity.Length();
|
||||
Vector2 velocityAddition = impulse / Mass;
|
||||
Vector2 newVelocity = body.LinearVelocity + velocityAddition;
|
||||
@@ -385,21 +427,26 @@ namespace Barotrauma
|
||||
|
||||
public void ApplyLinearImpulse(Vector2 impulse, Vector2 point)
|
||||
{
|
||||
if (!IsValidValue(impulse, "impulse")) return;
|
||||
body.ApplyLinearImpulse(impulse, point);
|
||||
}
|
||||
|
||||
public void ApplyForce(Vector2 force)
|
||||
{
|
||||
if (!IsValidValue(force, "force")) return;
|
||||
body.ApplyForce(force);
|
||||
}
|
||||
|
||||
public void ApplyForce(Vector2 force, Vector2 point)
|
||||
{
|
||||
if (!IsValidValue(force, "force")) return;
|
||||
if (!IsValidValue(point, "point")) return;
|
||||
body.ApplyForce(force, point);
|
||||
}
|
||||
|
||||
public void ApplyTorque(float torque)
|
||||
{
|
||||
if (!IsValidValue(torque, "torque")) return;
|
||||
body.ApplyTorque(torque);
|
||||
}
|
||||
|
||||
@@ -408,7 +455,10 @@ namespace Barotrauma
|
||||
System.Diagnostics.Debug.Assert(MathUtils.IsValid(simPosition));
|
||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.X) < 1000000.0f);
|
||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.Y) < 1000000.0f);
|
||||
|
||||
|
||||
if (!IsValidValue(simPosition, "position")) return;
|
||||
if (!IsValidValue(rotation, "rotation")) return;
|
||||
|
||||
body.SetTransform(simPosition, rotation);
|
||||
SetPrevTransform(simPosition, rotation);
|
||||
}
|
||||
@@ -418,14 +468,20 @@ namespace Barotrauma
|
||||
System.Diagnostics.Debug.Assert(MathUtils.IsValid(simPosition));
|
||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.X) < 1000000.0f);
|
||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.Y) < 1000000.0f);
|
||||
|
||||
|
||||
if (!IsValidValue(simPosition, "position")) return;
|
||||
if (!IsValidValue(rotation, "rotation")) return;
|
||||
|
||||
body.SetTransformIgnoreContacts(ref simPosition, rotation);
|
||||
SetPrevTransform(simPosition, rotation);
|
||||
}
|
||||
|
||||
public void SetPrevTransform(Vector2 position, float rotation)
|
||||
public void SetPrevTransform(Vector2 simPosition, float rotation)
|
||||
{
|
||||
prevPosition = position;
|
||||
if (!IsValidValue(simPosition, "position")) return;
|
||||
if (!IsValidValue(rotation, "rotation")) return;
|
||||
|
||||
prevPosition = simPosition;
|
||||
prevRotation = rotation;
|
||||
}
|
||||
|
||||
@@ -440,16 +496,19 @@ namespace Barotrauma
|
||||
prevPosition = (Vector2)targetPosition;
|
||||
}
|
||||
|
||||
body.SetTransform((Vector2)targetPosition, targetRotation == null ? body.Rotation : (float)targetRotation);
|
||||
SetTransform((Vector2)targetPosition, targetRotation == null ? body.Rotation : (float)targetRotation);
|
||||
targetPosition = null;
|
||||
}
|
||||
|
||||
public void MoveToPos(Vector2 pos, float force, Vector2? pullPos = null)
|
||||
public void MoveToPos(Vector2 simPosition, float force, Vector2? pullPos = null)
|
||||
{
|
||||
if (pullPos == null) pullPos = body.Position;
|
||||
|
||||
if (!IsValidValue(simPosition, "position")) return;
|
||||
if (!IsValidValue(force, "force")) return;
|
||||
|
||||
Vector2 vel = body.LinearVelocity;
|
||||
Vector2 deltaPos = pos - (Vector2)pullPos;
|
||||
Vector2 deltaPos = simPosition - (Vector2)pullPos;
|
||||
deltaPos *= force;
|
||||
body.ApplyLinearImpulse((deltaPos - vel * 0.5f) * body.Mass, (Vector2)pullPos);
|
||||
}
|
||||
@@ -474,8 +533,8 @@ namespace Barotrauma
|
||||
dragForce = Math.Min(drag, Mass * 500.0f) * -velDir;
|
||||
}
|
||||
|
||||
body.ApplyForce(dragForce + buoyancy);
|
||||
body.ApplyTorque(body.AngularVelocity * body.Mass * -0.08f);
|
||||
ApplyForce(dragForce + buoyancy);
|
||||
ApplyTorque(body.AngularVelocity * body.Mass * -0.08f);
|
||||
}
|
||||
|
||||
|
||||
@@ -559,18 +618,17 @@ namespace Barotrauma
|
||||
public void SmoothRotate(float targetRotation, float force = 10.0f)
|
||||
{
|
||||
float nextAngle = body.Rotation + body.AngularVelocity * (float)Timing.Step;
|
||||
|
||||
float angle = MathUtils.GetShortestAngle(nextAngle, targetRotation);
|
||||
|
||||
float torque = angle * 60.0f * (force/100.0f);
|
||||
float torque = angle * 60.0f * (force / 100.0f);
|
||||
|
||||
if (body.IsKinematic)
|
||||
{
|
||||
if (!IsValidValue(torque, "torque")) return;
|
||||
body.AngularVelocity = torque;
|
||||
}
|
||||
else
|
||||
{
|
||||
body.ApplyTorque(body.Mass * torque);
|
||||
ApplyTorque(body.Mass * torque);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user