Ballz.CameraTrajectory.GetCurrentPoint C# (CSharp) Method

GetCurrentPoint() public method

public GetCurrentPoint ( GameTime g ) : Vector3
g Microsoft.Xna.Framework.GameTime
return Vector3
        public Vector3 GetCurrentPoint(GameTime g)
        {
            Vector2 speed = Vector2.Normalize (EndPoint - StartPoint)*_speed;
            float distance = (EndPoint - StartPoint).Length ();

            float timeTraveled = (float)(g.TotalGameTime - StartTime).TotalSeconds + (float)(g.TotalGameTime - StartTime).TotalMilliseconds / 1000.0f;
            float traveledDistance = speed.Length() * timeTraveled;

            if (traveledDistance > distance) {
                valid = false;
                return new Vector3(EndPoint, 1.0f);
            }

            // zoom( x ) = a* (x - b)^2 + c;
            // zoom ( 0 ) = 1;
            // zoom ( distance ) = 1;
            // zoom ( distance/2 ) = 0.5;
            //
            float b = distance/2.0f;
            float c = 0.7f;
            float a = -0.3f / (distance*distance/4.0f);
            float zoom = -a * (traveledDistance - b)*(traveledDistance - b) + c;

            // I have no idea what is happening above, so let me just reduce the zoom by 80% and leave it alone.
            zoom = 0.8f + zoom * 0.2f;
            return new Vector3 ((StartPoint + (speed * timeTraveled)), zoom);
        }

Usage Example

Beispiel #1
0
        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));
            }
        }