Improved character movement lag compensation

This commit is contained in:
Regalis
2015-11-03 18:15:12 +02:00
parent 2a8939f6dc
commit 963f46e7d1
8 changed files with 46 additions and 26 deletions
@@ -120,8 +120,6 @@ namespace Barotrauma
message.WriteRangedSingle(AnimController.RefLimb.SimPosition.X, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16); message.WriteRangedSingle(AnimController.RefLimb.SimPosition.X, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
message.WriteRangedSingle(AnimController.RefLimb.SimPosition.Y, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16); message.WriteRangedSingle(AnimController.RefLimb.SimPosition.Y, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
//message.Write(AnimController.RefLimb.LinearVelocity.X);
//message.Write(AnimController.RefLimb.LinearVelocity.Y);
return true; return true;
} }
+18 -11
View File
@@ -501,16 +501,14 @@ namespace Barotrauma
float findClosestTimer; float findClosestTimer;
public void Control(float deltaTime, Camera cam) public Vector2 GetTargetMovement()
{ {
if (isDead || AnimController.StunTimer>0.0f) return;
Vector2 targetMovement = Vector2.Zero; Vector2 targetMovement = Vector2.Zero;
if (IsKeyDown(InputType.Left)) targetMovement.X -= 1.0f; if (IsKeyDown(InputType.Left)) targetMovement.X -= 1.0f;
if (IsKeyDown(InputType.Right)) targetMovement.X += 1.0f; if (IsKeyDown(InputType.Right)) targetMovement.X += 1.0f;
if (IsKeyDown(InputType.Up)) targetMovement.Y += 1.0f; if (IsKeyDown(InputType.Up)) targetMovement.Y += 1.0f;
if (IsKeyDown(InputType.Down)) targetMovement.Y -= 1.0f; if (IsKeyDown(InputType.Down)) targetMovement.Y -= 1.0f;
//the vertical component is only used for falling through platforms and climbing ladders when not in water, //the vertical component is only used for falling through platforms and climbing ladders when not in water,
//so the movement can't be normalized or the character would walk slower when pressing down/up //so the movement can't be normalized or the character would walk slower when pressing down/up
if (AnimController.InWater) if (AnimController.InWater)
@@ -525,6 +523,15 @@ namespace Barotrauma
targetMovement *= SpeedMultiplier; targetMovement *= SpeedMultiplier;
SpeedMultiplier = 1.0f; SpeedMultiplier = 1.0f;
return targetMovement;
}
public void Control(float deltaTime, Camera cam)
{
if (isDead || AnimController.StunTimer>0.0f) return;
Vector2 targetMovement = GetTargetMovement();
AnimController.TargetMovement = targetMovement; AnimController.TargetMovement = targetMovement;
AnimController.IsStanding = true; AnimController.IsStanding = true;
@@ -1439,15 +1446,15 @@ namespace Barotrauma
AnimController.IsStanding = true; AnimController.IsStanding = true;
keys[(int)InputType.Use].Held = actionKeyState; keys[(int)InputType.Use].Held = actionKeyState;
keys[(int)InputType.Aim].Held = secondaryKeyState; keys[(int)InputType.Aim].Held = secondaryKeyState;
if (sendingTime <= LastNetworkUpdate) return; if (sendingTime <= LastNetworkUpdate) return;
keys[(int)InputType.Left].Held = leftKeyState; keys[(int)InputType.Left].Held = leftKeyState;
keys[(int)InputType.Right].Held = rightKeyState; keys[(int)InputType.Right].Held = rightKeyState;
keys[(int)InputType.Up].Held = upKeyState; keys[(int)InputType.Up].Held = upKeyState;
keys[(int)InputType.Down].Held = downKeyState; keys[(int)InputType.Down].Held = downKeyState;
keys[(int)InputType.Run].Held = runState; keys[(int)InputType.Run].Held = runState;
@@ -1496,7 +1503,7 @@ namespace Barotrauma
cursorPosition = Position + new Vector2(1000.0f, 0.0f) * dir; cursorPosition = Position + new Vector2(1000.0f, 0.0f) * dir;
} }
AnimController.RefLimb.body.TargetPosition = pos; AnimController.RefLimb.body.TargetPosition = AnimController.EstimateCurrPosition(pos, (float)(NetTime.Now + message.SenderConnection.RemoteTimeOffset) - sendingTime);
LastNetworkUpdate = sendingTime; LastNetworkUpdate = sendingTime;
@@ -131,7 +131,7 @@ namespace Barotrauma
void UpdateSineAnim(float deltaTime) void UpdateSineAnim(float deltaTime)
{ {
movement = MathUtils.SmoothStep(movement, TargetMovement*swimSpeed, 1.0f); movement = TargetMovement*swimSpeed;
if (movement.LengthSquared() < 0.00001f) return; if (movement.LengthSquared() < 0.00001f) return;
if (!inWater) movement.Y = Math.Min(0.0f, movement.Y); if (!inWater) movement.Y = Math.Min(0.0f, movement.Y);
@@ -183,7 +183,7 @@ namespace Barotrauma
//current * (float)alpha + previous * (1.0f - (float)alpha); //current * (float)alpha + previous * (1.0f - (float)alpha);
steerForce = (movement * 50.0f - head.LinearVelocity * 30.0f); steerForce = (movement+correctionMovement) * 50.0f - head.LinearVelocity * 30.0f;
// force += (headMovement - movement) * Math.Min(head.LinearVelocity.Length()/movement.Length(), 1.0f); // force += (headMovement - movement) * Math.Min(head.LinearVelocity.Length()/movement.Length(), 1.0f);
if (!inWater) steerForce.Y = 0.0f; if (!inWater) steerForce.Y = 0.0f;
@@ -910,6 +910,17 @@ namespace Barotrauma
hand.body.SmoothRotate((ang2 + handAngle * Dir), 100.0f * force); hand.body.SmoothRotate((ang2 + handAngle * Dir), 100.0f * force);
} }
public override Vector2 EstimateCurrPosition(Vector2 prevPosition, float timePassed)
{
timePassed = MathHelper.Clamp(timePassed, 0.0f, 1.0f);
Vector2 targetMovement = character.GetTargetMovement();
Vector2 currPosition = prevPosition + targetMovement * timePassed/1000.0f;
return currPosition;
}
public override void Flip() public override void Flip()
{ {
base.Flip(); base.Flip();
+12 -7
View File
@@ -42,7 +42,7 @@ namespace Barotrauma
//a movement vector that overrides targetmovement if trying to steer //a movement vector that overrides targetmovement if trying to steer
//a character to the position sent by server in multiplayer mode //a character to the position sent by server in multiplayer mode
private Vector2 correctionMovement; protected Vector2 correctionMovement;
protected float floorY; protected float floorY;
protected float surfaceY; protected float surfaceY;
@@ -692,15 +692,15 @@ namespace Barotrauma
{ {
if (inWater) if (inWater)
{ {
foreach (Limb limb in Limbs) //foreach (Limb limb in Limbs)
{ //{
//if (limb.body.TargetPosition == Vector2.Zero) continue; // //if (limb.body.TargetPosition == Vector2.Zero) continue;
//limb.body.SetTransform(limb.SimPosition + newMovement * 0.1f, limb.Rotation); // limb.body.SetTransform(limb.SimPosition + Vector2.Normalize(diff) * 0.1f, limb.Rotation);
} //}
correctionMovement = correctionMovement =
Vector2.Lerp(targetMovement, Vector2.Normalize(diff) * MathHelper.Clamp(dist * 5.0f, 0.1f, 5.0f), 0.2f); Vector2.Lerp(targetMovement, Vector2.Normalize(diff) * MathHelper.Clamp(dist * 8.0f, 0.1f, 8.0f), 0.2f);
} }
else else
{ {
@@ -732,6 +732,11 @@ namespace Barotrauma
} }
} }
public virtual Vector2 EstimateCurrPosition(Vector2 prevPosition, float timePassed)
{
return prevPosition;
}
private Vector2 GetFlowForce() private Vector2 GetFlowForce()
{ {
@@ -105,10 +105,7 @@ namespace Barotrauma.Items.Components
private void DrawRadar(SpriteBatch spriteBatch, Rectangle rect) private void DrawRadar(SpriteBatch spriteBatch, Rectangle rect)
{ {
Vector2 center = new Vector2(rect.Center.X, rect.Center.Y); Vector2 center = new Vector2(rect.Center.X, rect.Center.Y);
//lineEnd += new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * Math.Min(width, height) / 2.0f;
//GUI.DrawLine(spriteBatch, GuiFrame.Center, lineEnd, Color.Green);
if (!IsActive) return; if (!IsActive) return;
+3 -1
View File
@@ -170,7 +170,9 @@ namespace Barotrauma.Networking
Entity e = Entity.FindEntityByID(id); Entity e = Entity.FindEntityByID(id);
if (e == null) if (e == null)
{ {
//DebugConsole.ThrowError("Couldn't find an entity matching the ID ''" + id + "''"); #if DEBUG
DebugConsole.ThrowError("Couldn't find an entity matching the ID ''" + id + "''");
#endif
return false; return false;
} }
Binary file not shown.