Game Development Community

Parallax Component

by Steve Wigmore · in Torque X 2D · 05/21/2009 (8:38 am) · 4 replies

Tx2d doesn't exactly come with a whole lot of much needed and simple components. So I figure I'd share what I come up with as I make it. If enough people do the same, then in no time, we'll have a nice variety of components for the community.

So here's my first component, Parallax. Place this on your sprite, and set it's depth.
100 = extreme background.
0 = no parallax
-100 = extreme foreground.

and all of the values in between as a float.


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
{
    [TorqueXmlSchemaType]
    public class Parallax : TorqueComponent, ITickObject
    {
        //======================================================
        #region Static methods, fields, constructors
        #endregion

        //======================================================
        #region Constructors
        #endregion

        //======================================================
        #region Public properties, operators, constants, and enums

        public T2DSceneObject SceneObject
        {
            get { return Owner as T2DSceneObject; }
        }
        public float ObjectDepth
        {
            get {return _objectDepth;}
            set {_objectDepth = value; }
        }
        //public float Initial_X
        //{
        //    get { return _initial_X; }
        //    set { _initial_X = value; }
        //}
        //public float Initial_Y
        //{
        //    get { return _initial_Y; }
        //    set { _initial_Y = value; }
        //}

        #endregion

        //======================================================
        #region Public methods

        public virtual void ProcessTick(Move move, float dt)
        {
            // todo: perform processing for component here
            _sceneObject.Position = new Vector2(( _initial_X + ((_camera.Position.X - _initial_X) * (ObjectDepth/100))), (_initial_Y + ((_camera.Position.Y - _initial_Y) * (ObjectDepth/100))));
        }

        public virtual void InterpolateTick(float k)
        {
            // todo: interpolate between ticks as needed here

        }

        public override void CopyTo(TorqueComponent obj)
        {
            base.CopyTo(obj);
        }

        #endregion

        //======================================================
        #region Private, protected, internal methods

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

            // todo: perform initialization for the component
            // tell the process list to notifiy us with ProcessTick and InterpolateTick events
            _sceneObject = owner as T2DSceneObject;
            ProcessList.Instance.AddTickCallback(Owner, this);
            _initial_X = _sceneObject.Position.X;
            _initial_Y = _sceneObject.Position.Y;

            
            // todo: look up interfaces exposed by other components
            // E.g., 
            // _theirInterface = Owner.Components.GetInterface<ValueInterface<float>>("float", "their interface name");            

            return true;
        }

        protected override void _OnUnregister()
        {
            // todo: perform de-initialization for the component

            base._OnUnregister();
        }

        protected override void _RegisterInterfaces(TorqueObject owner)
        {
            base._RegisterInterfaces(owner);

            // todo: register interfaces to be accessed by other components
            // E.g.,
            // Owner.RegisterCachedInterface("float", "interface name", this, _ourInterface);
        }


        #endregion

        //======================================================
        #region Private, protected, internal fields
        T2DSceneObject _sceneObject;
        float _objectDepth = 0;
        float _initial_X;
        float _initial_Y;
        T2DSceneCamera _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
        #endregion
    }
}

#1
05/21/2009 (8:59 am)
Nice man, though there is a parallax component in the Platformer Starter Kit ;)

In the PSK the parallax component must (I think) be used with a scroller, is that the case with your component?
#2
05/21/2009 (9:01 am)
Well I haven't bought the Platformer Starter Kit as of yet (it's on my things to do list). And no, this doesn't use scrollers... this will work for any sprite. non repeating parallax.

Say you wanted to put in... a specific mountain in the bg, or a sun, or a lens flare relative to a point in the bg... that's what this is for.
#3
05/21/2009 (9:05 am)
What do you think about adapting it to fake shadows as described in this post?
#4
05/21/2009 (10:31 am)
it can totally be done... u just have to alter the code to reflect the light source, instead of the camera position.

it'd work nicely for lens flares from bright static objects in the bg tho.