Submarine position is synced using the same logic as the characters, AICharacter position syncing

This commit is contained in:
Regalis
2016-10-25 19:01:19 +03:00
parent ac8edb5b2a
commit 07f8c966ab
12 changed files with 234 additions and 256 deletions
+55 -33
View File
@@ -11,9 +11,35 @@ using System;
namespace Barotrauma
{
struct PosInfo
{
public readonly Vector2 Position;
public readonly Direction Direction;
public readonly float Timestamp;
public readonly UInt32 ID;
public PosInfo(Vector2 pos, Direction dir, float time)
{
Position = pos;
Direction = dir;
Timestamp = time;
ID = 0;
}
public PosInfo(Vector2 pos, Direction dir, UInt32 ID)
{
Position = pos;
Direction = dir;
this.ID = ID;
Timestamp = 0.0f;
}
}
class PhysicsBody
{
public enum Shape
{
Circle, Rectangle, Capsule
@@ -70,18 +96,7 @@ namespace Barotrauma
targetPosition.Y = MathHelper.Clamp(value.Y, -10000.0f, 10000.0f);
}
}
//public Vector2 TargetVelocity
//{
// get { return targetVelocity; }
// set
// {
// if (!MathUtils.IsValid(value)) return;
// targetVelocity.X = MathHelper.Clamp(value.X, -100.0f, 100.0f);
// targetVelocity.Y = MathHelper.Clamp(value.Y, -100.0f, 100.0f);
// }
//}
public float TargetRotation
{
get { return targetRotation; }
@@ -92,16 +107,6 @@ namespace Barotrauma
}
}
//public float TargetAngularVelocity
//{
// get { return targetAngularVelocity; }
// set
// {
// if (!MathUtils.IsValid(value)) return;
// targetAngularVelocity = value;
// }
//}
public Vector2 DrawPosition
{
get { return Submarine == null ? drawPosition : drawPosition + Submarine.DrawPosition; }
@@ -223,6 +228,16 @@ namespace Barotrauma
list.Add(this);
}
public PhysicsBody(Body farseerBody)
{
body = farseerBody;
body.UserData = this;
LastSentPosition = body.Position;
list.Add(this);
}
public PhysicsBody(XElement element, Vector2 position, float scale=1.0f)
{
float radius = ConvertUnits.ToSimUnits(ToolBox.GetAttributeFloat(element, "radius", 0.0f)) * scale;
@@ -464,35 +479,42 @@ namespace Barotrauma
public void CorrectPosition(List<PosInfo> positionBuffer, float deltaTime, out Vector2 newVelocity)
{
newVelocity = Vector2.Zero;
if (positionBuffer.Count < 2) return;
Vector2 newPosition = SimPosition;
CorrectPosition(positionBuffer, deltaTime, out newVelocity, out newPosition);
SetTransform(newPosition, Rotation);
}
public void CorrectPosition(List<PosInfo> positionBuffer, float deltaTime, out Vector2 newVelocity, out Vector2 newPosition)
{
newVelocity = Vector2.Zero;
newPosition = SimPosition;
if (positionBuffer.Count < 2) return;
PosInfo prev = positionBuffer[0];
PosInfo next = positionBuffer[1];
Vector2 currPos = SimPosition;
//interpolate the position of the collider from the first position in the buffer towards the second
if (prev.Timestamp < next.Timestamp)
{
//if there are more than 2 positions in the buffer,
//increase the interpolation speed to catch up with the server
float speedMultiplier = 1.0f + (float)Math.Pow((positionBuffer.Count - 2) / 2.0f, 2.0f);
float speedMultiplier = 0.9f + (float)Math.Pow((positionBuffer.Count - 2) / 5.0f, 2.0f);
netInterpolationState += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp);
currPos = Vector2.Lerp(prev.Position, next.Position, netInterpolationState);
newPosition = Vector2.Lerp(prev.Position, next.Position, netInterpolationState);
//override the targetMovement to make the character play the walking/running animation
newVelocity = (next.Position - prev.Position) / (next.Timestamp - prev.Timestamp);
}
else
{
currPos = next.Position;
newPosition = next.Position;
netInterpolationState = 1.0f;
}
SetTransform(currPos, Rotation);
if (netInterpolationState >= 1.0f)
{
netInterpolationState = 0.0f;