Submarine position syncing improvements

This commit is contained in:
Regalis
2016-12-10 19:53:35 +02:00
parent 1f454d593e
commit 3411448ae8
4 changed files with 36 additions and 22 deletions
+7
View File
@@ -1194,6 +1194,13 @@ namespace Barotrauma
index++; index++;
} }
//position with the same timestamp already in the buffer (duplicate packet?)
// -> no need to add again
if (index < subBody.MemPos.Count && sendingTime == subBody.MemPos[index].Timestamp)
{
return;
}
subBody.MemPos.Insert(index, new PosInfo(newTargetPosition, Direction.Right, sendingTime)); subBody.MemPos.Insert(index, new PosInfo(newTargetPosition, Direction.Right, sendingTime));
} }
} }
+10 -18
View File
@@ -35,7 +35,7 @@ namespace Barotrauma
public readonly PhysicsBody Body; public readonly PhysicsBody Body;
List<PosInfo> memPos = new List<PosInfo>(); private List<PosInfo> memPos = new List<PosInfo>();
public Rectangle Borders public Rectangle Borders
{ {
@@ -186,22 +186,7 @@ namespace Barotrauma
{ {
if (GameMain.Client != null) if (GameMain.Client != null)
{ {
//if (memPos.Count > 1 && Vector2.Distance(memPos[1].Position, Body.SimPosition) > 5.0f) if (memPos.Count == 0) return;
//{
// Vector2 moveAmount = ConvertUnits.ToDisplayUnits(memPos[1].Position - Body.SimPosition);
// ForceTranslate(moveAmount);
// DisplaceCharacters(moveAmount);
// foreach (Submarine sub in submarine.DockedTo)
// {
// sub.SubBody.ForceTranslate(moveAmount);
// sub.SubBody.DisplaceCharacters(moveAmount);
// }
// memPos.RemoveRange(0, 2);
//}
//else
Vector2 newVelocity = Body.LinearVelocity; Vector2 newVelocity = Body.LinearVelocity;
Vector2 newPosition = Body.SimPosition; Vector2 newPosition = Body.SimPosition;
@@ -212,10 +197,17 @@ namespace Barotrauma
List<Submarine> subsToMove = new List<Submarine>() { this.submarine }; List<Submarine> subsToMove = new List<Submarine>() { this.submarine };
subsToMove.AddRange(submarine.DockedTo); subsToMove.AddRange(submarine.DockedTo);
foreach (Submarine dockedSub in submarine.DockedTo)
{
//clear the position buffer of the docked sub to prevent unnecessary position corrections
dockedSub.SubBody.memPos.Clear();
subsToMove.Add(dockedSub);
}
Submarine closestSub = null; Submarine closestSub = null;
if (Character.Controlled == null) if (Character.Controlled == null)
{ {
closestSub= Submarine.GetClosest(GameMain.GameScreen.Cam.WorldViewCenter); closestSub = Submarine.GetClosest(GameMain.GameScreen.Cam.WorldViewCenter);
} }
else else
{ {
@@ -68,7 +68,14 @@ namespace Barotrauma.Networking
//skip the event if we've already received it or if the entity isn't found //skip the event if we've already received it or if the entity isn't found
if (thisEventID != lastReceivedID + 1 || entity == null) if (thisEventID != lastReceivedID + 1 || entity == null)
{ {
DebugConsole.NewMessage("received msg "+thisEventID, Microsoft.Xna.Framework.Color.Red); if (thisEventID != lastReceivedID + 1)
{
DebugConsole.NewMessage("received msg "+thisEventID, Microsoft.Xna.Framework.Color.Red);
}
else if (entity == null)
{
DebugConsole.NewMessage("received msg " + thisEventID+", entity "+entityID+" not found", Microsoft.Xna.Framework.Color.Red);
}
msg.Position += msgLength * 8; msg.Position += msgLength * 8;
} }
else else
+11 -3
View File
@@ -511,10 +511,18 @@ namespace Barotrauma
float speedMultiplier = 0.9f + (float)Math.Pow((positionBuffer.Count - 2) / 5.0f, 2.0f); float speedMultiplier = 0.9f + (float)Math.Pow((positionBuffer.Count - 2) / 5.0f, 2.0f);
netInterpolationState += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp); netInterpolationState += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp);
newPosition = Vector2.Lerp(prev.Position, next.Position, netInterpolationState);
//override the targetMovement to make the character play the walking/running animation newPosition = Vector2.Lerp(prev.Position, next.Position, Math.Min(netInterpolationState, 1.0f));
newVelocity = (next.Position - prev.Position) / (next.Timestamp - prev.Timestamp);
if (next.Timestamp == prev.Timestamp)
{
newVelocity = Vector2.Zero;
}
else
{
//override the targetMovement to make the character play the walking/running animation
newVelocity = (next.Position - prev.Position) / (next.Timestamp - prev.Timestamp);
}
} }
else else
{ {