Game Development Community

Fading Walls as Hero walks behind them (ISO) - Screenshot

by Rich Hudson · in Torque X 2D · 02/25/2009 (7:26 pm) · 4 replies

Just a screenshot showing a cool effect. Whenever the hero walks behind an object the object fades.


http://photoshare.shaw.ca/view/17180754173-1235618537-73412/

Here is the code I have so far (work in progress) and comments/flames would be awsome. =)

private void ProcessWallTranslucence()
        {
            float distanceFromWallTolerance = 1.0f;

            //Get All objects that can be selected
            TorqueObjectType selectObj = TorqueObjectDatabase.Instance.GetObjectType("objWall");
            List<ISceneContainerObject> selectables = new List<ISceneContainerObject>();
            T2DSceneGraph sceneGraph = TorqueObjectDatabase.Instance.FindObject<T2DSceneGraph>();

            Vector2 heroPosition = Game.Instance.Hero.Position;

            sceneGraph.FindObjects(heroPosition, distanceFromWallTolerance, selectObj, (uint)0xFFFFFFFF, selectables);

            //Iterate through the List and get their distance from Aidan, then return the closest enemy
            foreach (ISceneContainerObject obj in selectables)
            {
                float DistX = ((T2DSceneObject)obj).Position.X;
                float DistY = ((T2DSceneObject)obj).Position.Y;

                float CamY = Game.Instance.Camera.Position.Y;

                //Get sort point of object
                float sortPointLocationY = DistY + (((T2DSceneObject)obj).SortPoint.Y * ((T2DSceneObject)obj).Size.Y) / 2;

                if (
                    heroPosition.Y < (DistY + ((T2DSceneObject)obj).Size.Y / 2.2) &&
                    heroPosition.Y < sortPointLocationY &&
                    heroPosition.X > (DistX - ((T2DSceneObject)obj).Size.X / 2.2) &&
                    heroPosition.X < (DistX + ((T2DSceneObject)obj).Size.X / 2.2))
                {
                    //This wall needs to be changed to translucent
                    SimpleMaterial material = (SimpleMaterial)((T2DStaticSprite)obj).Material;
                    material.IsTranslucent = true;
                    material.IsColorBlended = true;
                    ((T2DSceneObject)obj).VisibilityLevel = 0.4f;
                }
                else
                {
                    ((T2DSceneObject)obj).VisibilityLevel = 1.0f;
                }
            }
        }

#1
02/25/2009 (7:57 pm)
Very nice :)
#2
02/26/2009 (11:35 am)
@Rich:

Cool idea!

I didn't look into it in to much detail (I'm current at work), but all the casting of obj to T2DSceneObject did kind of stick out. You might want to cast it and store it in a variable at the top one time instead of casting it ~10x (might have you some precious CPU cycles :)). Though of course that all depends on how many selectables your iterating over and how many times your event calling that method all together during runtime.
#3
02/26/2009 (4:01 pm)
@Rich

Very, very nice!

I'll add a couple of suggestions as well:

1. You may want to consider using some kind of time out to prevent this code from running every tick as sceneGraph.FindObjects is somewhat costly. You just posted a snippet so maybe you are already doing this.

2. Replace foreach with for. John K. can say it better than I in his book:
Quote:The foreach operator is a convenient way to cycle through a list. However, it comes with a high performance cost because it creates a new enumerator instance at the start of each foreach, results in extra garbage collection cycles.

#4
03/03/2009 (7:44 pm)
Thanks for the comments guys - yeah this was a prototype code block. Both of the above suggestions where implemented after posting this - though I did forget about the for each loop which I should have known better.