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:
@@ -98,6 +98,7 @@ namespace Barotrauma
|
|||||||
float dir;
|
float dir;
|
||||||
|
|
||||||
Vector2 offsetFromTargetPos;
|
Vector2 offsetFromTargetPos;
|
||||||
|
float offsetLerp;
|
||||||
|
|
||||||
private float netInterpolationState;
|
private float netInterpolationState;
|
||||||
|
|
||||||
@@ -396,7 +397,8 @@ namespace Barotrauma
|
|||||||
|
|
||||||
if (lerp && Vector2.Distance((Vector2)targetPosition, body.Position) < 10.0f)
|
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;
|
prevPosition = (Vector2)targetPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -441,21 +443,24 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void UpdateDrawPosition()
|
public void UpdateDrawPosition()
|
||||||
{
|
{
|
||||||
drawPosition = Timing.Interpolate(prevPosition, body.Position) - offsetFromTargetPos;
|
drawPosition = Timing.Interpolate(prevPosition, body.Position);
|
||||||
drawPosition = ConvertUnits.ToDisplayUnits(drawPosition);
|
drawPosition = ConvertUnits.ToDisplayUnits(drawPosition);
|
||||||
|
|
||||||
drawRotation = Timing.Interpolate(prevRotation, body.Rotation);
|
drawRotation = Timing.InterpolateRotation(prevRotation, body.Rotation);
|
||||||
|
|
||||||
if (offsetFromTargetPos == Vector2.Zero) return;
|
if (offsetFromTargetPos == Vector2.Zero)
|
||||||
|
|
||||||
float diff = offsetFromTargetPos.Length();
|
|
||||||
if (diff < 0.05f)
|
|
||||||
{
|
{
|
||||||
offsetFromTargetPos = Vector2.Zero;
|
return;
|
||||||
}
|
}
|
||||||
else
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,13 @@ namespace Barotrauma
|
|||||||
return current * (float)alpha + previous * (1.0f - (float)alpha);
|
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)
|
public static Vector2 Interpolate(Vector2 previous, Vector2 current)
|
||||||
{
|
{
|
||||||
return new Vector2(
|
return new Vector2(
|
||||||
|
|||||||
Reference in New Issue
Block a user