public void SwitchTarget(Vector2 targetPosition, GameTime t) { if (CurrentCameraTrajectory == null) { CurrentCameraTrajectory = new CameraTrajectory(CurrentPosition, targetPosition, t); } }
public void Update(GameTime t) { if (Zoom != ZoomTarget) { float zoomDirection = Math.Sign(ZoomTarget - Zoom); Zoom += zoomDirection * ZoomPerSecond * (float)t.ElapsedGameTime.TotalSeconds; if ((zoomDirection < 0 && Zoom < ZoomTarget) || (zoomDirection > 0 && Zoom > ZoomTarget)) { Zoom = ZoomTarget; } } if (CurrentCameraTrajectory != null) { if (CurrentCameraTrajectory.IsValid() == false) { CurrentCameraTrajectory = null; return; } Vector3 p = CurrentCameraTrajectory.GetCurrentPoint(t); CurrentPosition.X = p.X; CurrentPosition.Y = p.Y; float dynamicZoom = p.Z; float x_size = 40.0f / dynamicZoom; float y_size = 40.0f / dynamicZoom / AspectRatio; var left = p.X - x_size / 2.0f; var right = p.X + x_size / 2.0f; var bottom = p.Y - y_size / 2.0f; var top = p.Y + y_size / 2.0f; ClampToBoundary(ref top, ref right, ref bottom, ref left); // Clamp view frustum to boundary SetView(Matrix.CreateOrthographicOffCenter( left, right, bottom, top, -20, 20)); } else { Vector2 DiffPos = TargetPosition - CurrentPosition; double DiffTime = (t.TotalGameTime.TotalMilliseconds - lastMillis) / 1000.0; lastMillis = t.TotalGameTime.TotalMilliseconds; if (DiffPos.Length() > 0f) { float speed = 3.0f + DiffPos.Length() * 1.0f; Vector2 delta = Vector2.Normalize(DiffPos) * speed * (float)DiffTime; if (delta.Length() > DiffPos.Length()) { CurrentPosition = TargetPosition; } else { CurrentPosition += delta; } } float dynamicZoom = Zoom - DiffPos.Length() * 0.01f; float x_size = 40.0f / dynamicZoom; float y_size = 40.0f / dynamicZoom / AspectRatio; var left = CurrentPosition.X - x_size / 2.0f; var right = CurrentPosition.X + x_size / 2.0f; var bottom = CurrentPosition.Y - y_size / 2.0f; var top = CurrentPosition.Y + y_size / 2.0f; ClampToBoundary(ref top, ref right, ref bottom, ref left); SetView(Matrix.CreateOrthographicOffCenter( left, right, bottom, top, -20, 20)); } }