Game Development Community

RayCastQueryData Question

by Dave DAmico · in Torque X 2D · 09/11/2008 (8:23 am) · 1 replies

I modified some of the code from the FPS to detect if my camera is about to walk into certain objects. Here is the code:

Matrix transform = SceneGroup.Transform;
            Vector3 forward = MatrixUtil.MatrixGetRow(1, ref transform);
            Vector3 right = MatrixUtil.MatrixGetRow(0, ref transform);
            Vector3 direction = (forward * move.Sticks[0].Y) + (right * move.Sticks[0].X);
            if (direction.LengthSquared() < Epsilon.Value)
                return;


            direction.Normalize();

 
            CollisionBoxShape box = new CollisionBoxShape();
            box.Center = new Vector3(0.0f, 0.0f, 0.0f);
            box.Size = new Vector3(50.0f, 50.0f, 266.0f);

            _rayCast.Position = SceneGroup.Position + box.Center;            
            _rayCast.Direction = direction;
            _rayCast.Length = box.HalfSize.X + 10.0f;
            _rayCast.ObjectType = _stepObjectTypes;


            PlayerComponent playerComponent = Owner.Components.FindComponent<PlayerComponent>();


            if (Rigid.RigidManager.CollisionScene.CastRay(_rayCast))
            {
                Game.Instance.ActionMessage.Text = "Press the \"e\" key to begin";
                Game.Instance.ActionMessage.Position = new Vector2(
                    Game.Instance.SceneView.Size.X / 2 - Game.Instance.ActionMessage.Size.X / 2,
                    Game.Instance.SceneView.Size.Y / 2 - Game.Instance.ActionMessage.Size.Y / 2);


                if (null != playerComponent) playerComponent.AvailableAction = true;
            }
            else
            {
                if (null != playerComponent) playerComponent.AvailableAction = false;
            }

            base._UpdateControl(move, dt);

The scene consists of a desk object and a chair object. If I'm near the chair I want to sit. If I'm near the desk I want to do something else. How do I determine which object the ray cast detected?

#1
09/11/2008 (9:38 am)
I figured out a way to do it... but i'm not sure if it is the correct way.

IAction actionObject =  _rayCast.Body.RigidComponent.Owner as IAction;

NOTE: IAction is an interface I created