Skip to content

Commit

Permalink
Smooth interpolation in viewport position (#112)
Browse files Browse the repository at this point in the history
* panning interpolation test

* smooth panning interp, camera ahead of worm

* revert to old pan interpolation

* different interpspeed
  • Loading branch information
caden-maxwell authored Apr 19, 2024
1 parent 140ae4b commit 249ff06
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/Client/Systems/Camera.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
using Microsoft.Xna.Framework;
using Shared.Entities;
using System;
using System.Diagnostics;
using Shared.Components;
using System.Diagnostics;

namespace Client.Systems;

public class Camera : Shared.Systems.System
{
private Rectangle m_viewport = new();
public Rectangle Viewport { get { return m_viewport; } }
private const float m_maxScale = 0.6f;
private const float m_maxScale = 0.7f;
private const float m_minScale = 0.3f;
private const int m_zoomInTime = 1000;
private const int m_zoomOutTime = 15000;
private float m_currentZoom = m_minScale;
public float Zoom { get { return m_currentZoom; } }
private PlayerData m_playerData;
private readonly PlayerData m_playerData;
private bool m_playing = false;
private bool m_newGame = true;
private float m_bezierCurveMS = 0;
private float m_interpTime;
private Vector2 m_prevDirection;
private float m_timeSincePrevDirection = 0;

public Camera(Vector2 viewportSize, PlayerData playerData) : base(typeof(Input))
{
Expand All @@ -46,6 +49,7 @@ public override void update(TimeSpan elapsedTime)

if (!m_playing)
{
m_newGame = true;
m_playing = true;
m_interpTime = m_zoomInTime;
m_bezierCurveMS = 0;
Expand All @@ -57,16 +61,30 @@ public override void update(TimeSpan elapsedTime)
if (e.contains<Name>() && e.get<Name>().name == m_playerData.playerName)
{
Entity player = e;
Vector2 pos = player.get<Position>().position;
Vector2 targetPos = player.get<Position>().position;

if (m_newGame)
{
m_newGame = false;
m_viewport.Location = targetPos.ToPoint();
}

// TODO: Change zoom depending on player size
Vector2 cameraOffset = targetPos - m_viewport.Location.ToVector2();
float interpolationSpeed = 0.05f;
Vector2 interpolatedPos = m_viewport.Location.ToVector2() + cameraOffset * interpolationSpeed;
m_viewport.Location = interpolatedPos.ToPoint();

m_viewport.Location = pos.ToPoint();
return;
break;
}
}
}

private static Vector2 RadiansToVector(float radians)
{
Vector2 directionVec = new((float)Math.Cos(radians), (float)Math.Sin(radians));
return Vector2.Normalize(directionVec);
}

private void BezierZoom(TimeSpan elapsedTime, bool zoomIn = true)
{
int direction = zoomIn ? 1 : -1;
Expand Down Expand Up @@ -119,7 +137,7 @@ public static Vector2 GetPoint(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, f
}

// Linear interpolation between two vectors
private static Vector2 Lerp(Vector2 a, Vector2 b, float t)
public static Vector2 Lerp(Vector2 a, Vector2 b, float t)
{
return a + (b - a) * t;
}
Expand Down

0 comments on commit 249ff06

Please sign in to comment.