TX2D CameraFollow Component

The CameraFollow component allows the camera to follow the player without being mounted to the player, this allows a gentle “lag” between the player movement and camera movement. This component works great with all types of games, similar camera movement can be found in games such as the acclaimed “I Made A Game With Zombies In It” or even in classic games like Mario Brothers.

Component Screen Shot

CameraName The name of the scene camera that needs to follow the player.

PlayerName The player’s source name. I decided to expose this field to allow for more uses.

MinVelocitySquared The required velocity of the player, before the camera starts to follow the player.

MaxVelocitySquared The camera will mount 1:1 to the players movement if players velocity exceeds this.

CameraFollow.cs Code:

// CameraFollow.cs v1.2
// Aaron Scovel - TopNotched.com

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using GarageGames.Torque.Core;
using GarageGames.Torque.Util;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T2D;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.MathUtil;

namespace StarterGame2D

    [TorqueXmlSchemaDependency(Type = typeof(T2DWorldLimitComponent))]
    public class CameraFollow : TorqueComponent, ITickObject

        #region Public properties, operators, constants, and enums

        public string CameraName
            get { return _camera; }
            set { _camera = value; }

        public string PlayerName
            get { return _player; }
            set { _player = value; }

        [TorqueXmlSchemaType(DefaultValue = "300")]
        public int MinVelocitySquared
            get { return _minVelocitySquared; }
            set { _minVelocitySquared = value; }

        [TorqueXmlSchemaType(DefaultValue = "250000")]
        public int MaxVelocitySquared
            get { return _maxVelocitySquared; }
            set { _maxVelocitySquared = value; }


        #region Public methods

        public virtual void ProcessTick(Move move, float dt)
            // If the player is moving faster than the threshold level, reposition the camera
            if (player.Physics.Velocity.LengthSquared() > 0 && player.Physics.Velocity.LengthSquared() > _minVelocitySquared)
                // Calculate the direction the player is moving
                Vector2 normalizedVector = Vector2.Normalize(player.Physics.Velocity);
                // Position the camera mount at an offset from the player
                Vector2 targetDirection = Vector2.Multiply(normalizedVector, camera.Extent.Y / 2);
                // Move the offseted position to the player's new location
                targetPosition = Vector2.Add(targetDirection, player.Position);
                // Interpolate the camera speed
                float interpolationSpeed = MathHelper.Lerp(0.1f, 1.0f, MathHelper.Clamp(player.Physics.Velocity.LengthSquared() / _maxVelocitySquared, 0.0f, 1.0f));
                // update the cameras mount position
                cameraMount.Position = Vector2.SmoothStep(cameraMount.Position, targetPosition, interpolationSpeed);

        public void InterpolateTick(float k)


        #region Private, protected, internal methods

        protected override bool _OnRegister(TorqueObject owner)
            if (!base._OnRegister(owner) || !(owner is T2DSceneObject))
                return false;

            camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>(_camera);
            player = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>(_player);
            // Setup a new object that the camera will mount to
            cameraMount = new T2DSceneObject();
            cameraMount.CollisionsEnabled = false;
            cameraMount.Position = player.Position;
            // Set the camera World Limits to the Players World Limit
            T2DWorldLimitComponent playerWorldLimits = player.WorldLimit;
            camera.CameraWorldLimitMin = new Vector2(playerWorldLimits.MoveLimitMin.X + (camera.Extent.X / 2), playerWorldLimits.MoveLimitMin.Y + (camera.Extent.Y / 2));
            camera.CameraWorldLimitMax = new Vector2(playerWorldLimits.MoveLimitMax.X - (camera.Extent.X / 2), playerWorldLimits.MoveLimitMax.Y - (camera.Extent.Y / 2));
            camera.UseCameraWorldLimits = true;
            // Mount the camera to the cameraMount object
            camera.Mount(cameraMount, "", true);
            camera.TrackMountRotation = false;
            camera.UseMountForce = true;
            camera.MountForce = 3.0f;
            // Set the inital the target position
            targetPosition = player.Position;

            ProcessList.Instance.AddTickCallback(Owner, this);

            return true;


        #region Private, protected, internal fields

        T2DSceneCamera camera;
        T2DSceneObject cameraMount;
        T2DSceneObject player;
        string _camera;
        string _player;
        int _minVelocitySquared;
        int _maxVelocitySquared;
        Vector2 targetPosition;

Powered by WordPress | Designed by: Download Free WordPress Themes | Thanks to wordpress themes 2012, Themes Gallery and Best Free WordPress Themes