Some extra error checking in GUIComponent and PhysicsBody syncing logic

This commit is contained in:
Regalis
2017-03-03 20:29:07 +02:00
parent ee16f0708b
commit 4c863cfdd7
2 changed files with 41 additions and 4 deletions
+17
View File
@@ -461,6 +461,23 @@ namespace Barotrauma
public virtual void AddChild(GUIComponent child) public virtual void AddChild(GUIComponent child)
{ {
if (child == null) return;
if (child.IsParentOf(this))
{
DebugConsole.ThrowError("Tried to add the parent of a GUIComponent as a child.\n" + Environment.StackTrace);
return;
}
if (child == this)
{
DebugConsole.ThrowError("Tried to add a GUIComponent as its own child\n" + Environment.StackTrace);
return;
}
if (children.Contains(child))
{
DebugConsole.ThrowError("Tried to add a the same child twice to a GUIComponent" + Environment.StackTrace);
return;
}
child.parent = this; child.parent = this;
child.UpdateDimensions(this); child.UpdateDimensions(this);
+24 -4
View File
@@ -366,7 +366,15 @@ namespace Barotrauma
public void MoveToPos(Vector2 pos, float force, Vector2? pullPos = null) public void MoveToPos(Vector2 pos, float force, Vector2? pullPos = null)
{ {
if (pullPos==null) pullPos = body.Position; if (pullPos == null) pullPos = body.Position;
if (!MathUtils.IsValid(pos))
{
#if DEBUG
DebugConsole.ThrowError("Tried to move a physics body to an invalid position.\n" + Environment.StackTrace);
#endif
return;
}
Vector2 vel = body.LinearVelocity; Vector2 vel = body.LinearVelocity;
Vector2 deltaPos = pos - (Vector2)pullPos; Vector2 deltaPos = pos - (Vector2)pullPos;
@@ -502,11 +510,13 @@ namespace Barotrauma
//increase the interpolation speed to catch up with the server //increase the interpolation speed to catch up with the server
float speedMultiplier = (float)Math.Pow(1.0f + (positionBuffer.Count - 2) / 10.0f, 2.0f); float speedMultiplier = (float)Math.Pow(1.0f + (positionBuffer.Count - 2) / 10.0f, 2.0f);
netInterpolationState += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp); float dT = next.Timestamp - prev.Timestamp;
newPosition = Vector2.Lerp(prev.Position, next.Position, netInterpolationState);
netInterpolationState += (deltaTime * speedMultiplier) / dT;
newPosition = Vector2.Lerp(prev.Position, next.Position, MathHelper.Clamp(netInterpolationState, 0.0f, 1.0f));
//override the targetMovement to make the character play the walking/running animation //override the targetMovement to make the character play the walking/running animation
newVelocity = (next.Position - prev.Position) / (next.Timestamp - prev.Timestamp); newVelocity = (next.Position - prev.Position) / dT;
} }
else else
{ {
@@ -514,6 +524,16 @@ namespace Barotrauma
netInterpolationState = 1.0f; netInterpolationState = 1.0f;
} }
if (!MathUtils.IsValid(newPosition))
{
#if DEBUG
DebugConsole.ThrowError("Invalid physicsbody sync position "+newPosition);
#endif
netInterpolationState = 0.0f;
positionBuffer.RemoveAt(0);
return;
}
if (netInterpolationState >= 1.0f) if (netInterpolationState >= 1.0f)
{ {
netInterpolationState = 0.0f; netInterpolationState = 0.0f;