Game Development Community

Triggers Question

by Sean T. Boyette · in Torque X 2D · 11/15/2008 (3:10 pm) · 2 replies

Hey Guys -
I am having a bit of difficulty with Triggers that I want to attach to a moving object.

I am using this code
//Trigger for Player 1
            //Attach an object type to the player
            TorqueObjectType typePlayer = TorqueObjectDatabase.Instance.GetObjectType("PlayerObject_0");
            objPlayer.ObjectType += typePlayer;
            //Trigger Shape
            CollisionSphereShape colTrigger = new CollisionSphereShape();
            colTrigger.Radius = 1.5f;
            colTrigger.Center = new Vector3(0.0f, 0.0f, .5f);
            //Add the trigger component
            T3DTriggerComponent componentTrigger = new T3DTriggerComponent();
            componentTrigger.SceneGroupName = "EnemyTemplate";
            componentTrigger.CollisionBody.AddCollisionShape(colTrigger);
            componentTrigger.CollidesWith = typePlayer;
            componentTrigger.OnCollision = AttackPlayer;
            componentTrigger.RigidManager = rigidManager;
            componentTrigger.Immovable = true;
            objEnemyTemplate.Components.AddComponent(componentTrigger);
but I do not ever hit the onCollision event -
I have tried to change thee .immovable to false - however, it seems that that overides the character and he is no where to be found.

Any ideas?
Sean

#1
11/15/2008 (8:20 pm)
I figured it out, and I must say ode to the art of components !!!! I got it...

Just wanted to share - as I am sure it can come in handy.

The Below component simply follows my character around. (dynamic Trigger, if you will)
Component
Trigger..
Make sure to add to the Object you want to trigger:
//Attach an object type to the player
                TorqueObjectType typePlayer = TorqueObjectDatabase.Instance.GetObjectType("Player");
                objPlayer.ObjectType += typePlayer;

Adding the Component to the enemy:
//Trigger Component
            EnemyTriggerComponent componentTrigger = new EnemyTriggerComponent();
            componentTrigger.Triggers.Add(componentTrigger.CreateTrigger("EnemyTrigger"));
            objEnemyTemplate.Components.AddComponent(componentTrigger);

The Component itself:
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using GarageGames.Torque.Core;
using GarageGames.Torque.Util;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T3D;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.MathUtil;
using GarageGames.Torque.Platform;
using GarageGames.Torque.T3D.RigidCollision;
using GarageGames.Torque.Materials;

namespace RobotRevolution
{
    [TorqueXmlSchemaType]
    public class EnemyTriggerComponent : TorqueComponent, ITickObject
    {
        //======================================================
        #region Public properties, operators, constants, and enums
        public List<TorqueObject> Triggers
        {
            get { return _arrayTriggers; }
            set { _arrayTriggers = value; }
        }
        #endregion

        //======================================================
        #region Public methods
        public TorqueObject CreateTrigger(string name)
        {
            //find the physics resolver
            RigidCollisionManager rigidManager =
            TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            //get the Player object type
            TorqueObjectType typePlayer = TorqueObjectDatabase.Instance.GetObjectType("Player");
            TorqueObject objTrigger = new TorqueObject();
            objTrigger.Name = "Trigger";

            //add the scene component
            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = "Trigger";
            componentScene.Position = new Vector3(1028, 1035, 256);
            objTrigger.Components.AddComponent(componentScene);

            CollisionSphereShape colSphere = new CollisionSphereShape();
            colSphere.Radius = .5f;
            colSphere.Center = new Vector3(0.0f, 0.0f, .5f);

            CollisionSphereShape colSphere1 = new CollisionSphereShape();
            colSphere1.Radius = .5f;
            colSphere1.Center = new Vector3(0.0f, 0.0f, 1.51f);

            //add the trigger component
            T3DTriggerComponent componentTrigger = new T3DTriggerComponent();
            componentTrigger.SceneGroupName = "Trigger";
            componentTrigger.RenderCollisionBounds = false;
            componentTrigger.CollisionBody.AddCollisionShape(colSphere);
            componentTrigger.CollisionBody.AddCollisionShape(colSphere1);
            componentTrigger.CollidesWith = typePlayer;
            componentTrigger.OnEnter = ProcessEnter;
            componentTrigger.OnLeave = ProcessLeave;
            componentTrigger.RigidManager = rigidManager;
            componentTrigger.Immovable = true;
            objTrigger.Components.AddComponent(componentTrigger);

            TorqueObjectDatabase.Instance.Register(objTrigger);
            return objTrigger;
        }

        public static void ProcessEnter(T3DTriggerComponent trigger, T3DRigidComponent obj)
        {
            //perform enter trigger code here
            Game.Instance.playGui.player1Health.Size = new Vector2(Game.Instance.playGui.player1Health.Size.X - 20, Game.Instance.playGui.player1Health.Size.Y);
            int Score = Convert.ToInt32(Game.Instance.playGui.player1Score.Text) + 50;
            Game.Instance.playGui.player1Score.Text = Score.ToString(); 
        }


        public static void ProcessLeave(T3DTriggerComponent trigger, T3DRigidComponent obj)
        {
            //perform leave trigger code here
        }
        public virtual void ProcessTick(Move move, float dt)
        {
            T3DSceneComponent componentScene = _arrayTriggers[0].Components.FindComponent<T3DSceneComponent>();
            componentScene.Position  = _mySceneComponent.Position;
            //this.Components.FindComponent<T3DSceneComponent>().Position  = _mySceneComponent.Position;
        }
        public virtual void InterpolateTick(float k)
        {
        }
        public override void CopyTo(TorqueComponent obj)
        {
            base.CopyTo(obj);
            EnemyTriggerComponent obj2 = (EnemyTriggerComponent)obj;
            obj2._arrayTriggers = _arrayTriggers;
 
        }

        #endregion

        //======================================================
        #region Private, protected, internal methods
        protected override bool _OnRegister(TorqueObject owner)
        {
            if (!base._OnRegister(owner))
                return false;

            _mySceneComponent = this.Owner.Components.FindComponent<T3DSceneComponent>();

            ProcessList.Instance.AddTickCallback(this.Owner, this);
            return true;
        }
        #endregion

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

        private List<TorqueObject> _arrayTriggers = new List<TorqueObject>();

        private T3DSceneComponent _mySceneComponent;

        #endregion
    }
}
#2
11/16/2008 (1:59 am)
Cool! Thanks for sharing this with us :)