Interpolating item drawRotation in the "shortest direction" (prevents twitching if the rotation changes e.g. from -0.1f to PI*2), physicsbodies are lerped towards their targetposition at a constant speed

This commit is contained in:
Regalis
2017-02-09 00:01:01 +02:00
parent 4c5da19ab4
commit 1f16bef01c
2 changed files with 21 additions and 9 deletions

View File

@@ -98,6 +98,7 @@ namespace Barotrauma
float dir;
Vector2 offsetFromTargetPos;
float offsetLerp;
private float netInterpolationState;
@@ -396,7 +397,8 @@ namespace Barotrauma
if (lerp && Vector2.Distance((Vector2)targetPosition, body.Position) < 10.0f)
{
offsetFromTargetPos = (Vector2)targetPosition - (body.Position - offsetFromTargetPos);
offsetFromTargetPos = (Vector2)targetPosition - (body.Position - Vector2.Lerp(offsetFromTargetPos, Vector2.Zero, offsetLerp));
offsetLerp = 1.0f;
prevPosition = (Vector2)targetPosition;
}
@@ -441,21 +443,24 @@ namespace Barotrauma
public void UpdateDrawPosition()
{
drawPosition = Timing.Interpolate(prevPosition, body.Position) - offsetFromTargetPos;
drawPosition = Timing.Interpolate(prevPosition, body.Position);
drawPosition = ConvertUnits.ToDisplayUnits(drawPosition);
drawRotation = Timing.Interpolate(prevRotation, body.Rotation);
drawRotation = Timing.InterpolateRotation(prevRotation, body.Rotation);
if (offsetFromTargetPos == Vector2.Zero) return;
float diff = offsetFromTargetPos.Length();
if (diff < 0.05f)
if (offsetFromTargetPos == Vector2.Zero)
{
offsetFromTargetPos = Vector2.Zero;
return;
}
else
{
offsetFromTargetPos = Vector2.Lerp(offsetFromTargetPos, Vector2.Zero, 0.05f);
drawPosition -= ConvertUnits.ToDisplayUnits(Vector2.Lerp(Vector2.Zero, offsetFromTargetPos, offsetLerp));
}
offsetLerp -= 0.1f;
if (offsetLerp < 0.0f)
{
offsetFromTargetPos = Vector2.Zero;
}
}

View File

@@ -30,6 +30,13 @@ namespace Barotrauma
return current * (float)alpha + previous * (1.0f - (float)alpha);
}
public static float InterpolateRotation(float previous, float current)
{
float angleDiff = MathUtils.GetShortestAngle(previous, current);
return previous + angleDiff * (float)alpha;
}
public static Vector2 Interpolate(Vector2 previous, Vector2 current)
{
return new Vector2(