Game Development Community

How to perform an "ACTIVATE" Event/Trigger?

by Laurence Grant · in Torque Game Engine Advanced · 12/30/2008 (9:15 am) · 4 replies

Hey all,

I want to be able to walk up to an object and press a key (active) to initiate a trigger/event. I haven't seen any reference on how to do this? In short, I think I need to know that I'm within a certain distance to an object and facing it. On collision I just want to bounce/slide off of it, but if I'm standing in the right place and hit the "active" key, I want it to trigger the event appropriately. It can be a little more complicated in that if there are multiple objects around, I need to decide which one I'm more closely facing and closer to, then that's the one I activate. Any suggestions/samples on how to achieve this would be greatly appreciated.

Thanks

#1
12/30/2008 (1:20 pm)
Search under Binding a Key command.
Or CommandToServer(....something like that!)
..Thats if you want a key to trigger and event.
,..You could use an "if " statement to test they key command when you within the triggered area.


This might help!

Old School Code Sample Tuts

Use the Search Box!
#2
01/01/2009 (7:14 am)
I recently had to do something similar. I built a complete object interaction framework in the engine, but to get started you can just bind a key to your interaction key, and in the event handler do a raycast from your eyepoint along the direction the camera is facing. Then determine if the closest object that the ray intersects is an interactive object, and then perform your interaction if it is. The raycast rather than proximity is important because otherwise you may be able to activate objects that are on the other side of a wall, for instance.
#3
01/01/2009 (10:43 am)
Thanks Surge and Gerald.

Surge, those links are good. They don't specifically help with what I need, but they are good references and I appreciate you providing them.

Gerald, I think you've got the right idea to what I'm looking for, but I was hoping the engine might have some of this built in. It seems to do so much already, and this seems so obvious that I'm shocked the engine doesn't have it built in. I've seen a lot of other engines that do have this behavior built in.

Community, does anyone know if there's anything built in to help with the raycast and also help with controlling the # of objects you need to loop through? If I have a scene with hundreds or even thousands of objects I want to eliminate as much as possible quickly. In my old engine I wrote from scratch I had placed objects into quadrants (outdoor) and for indoor I knew where things were based on room. Therefore, at most I only needed to look at the objects in the current quadrant/room. Can Torque do anything similar? Perhaps I'd need to manually place objects in a trigger area then based on the player being in the trigger area I'd only need to find other objects in the same area? Does something like that make sense? How does TGEA optimize collision detection? If I have thousands or tens of thousands of objects on a planet, and with Atlas I can have really big planets (levels), I have to assume TGEA somehow optimizes the collision detection not to waste time on objects far away? I would hope I could leverage something similar for my needs?

Also, I've seen some engines that have a built in call to see if an object is infront of the camera. Does TGEA have anything similar or do you need to perform a brute force ray casting?

Thanks
#4
01/01/2009 (2:32 pm)
Hi Laurence,

The engine has some pretty useful stuff built-in. You don't need to do any kind of brute force ray casting yourself, just use the castRay method on either the server or client container, and it will handle things pretty efficiently.

I don't do very much in script, so I'm not sure how you would do it just in script but I presume it's possible. But I'll show you how I am doing it in my C++ code. This is out of my interaction framework but I think you should get the idea of what you need to do. I removed a bunch of extraneous stuff to try and simplify things a bit.

void Character::Interact()
{
   GameConnection* connection = dynamic_cast<GameConnection *>(NetConnection::getConnectionToServer());
   if (connection)
   {
        //get the current control camera
        MatrixF mat;
        if (connection->getControlCameraTransform( 0.032f, &mat ))
        {
           //start of the ray is the camera position
           Point3F start = mat.getPosition();

           //take a point 5 units out in front, and then multiply it by the camera matrix
           //that will be the end of the ray
           Point3F end( 0, 5, 0);
           mat.mulP(end);

           //cast a ray against all interactive object types and interior types
           //to be sure that no walls are between the camera and any interactive objects
           RayInfo info;
           if (gServerContainer.castRay( start, end, InteractiveObjectType | InteriorObjectType, &info ))
           {
               //our ray hit something, was it an interactive object?
               SceneObject* obj = info.object;
               InteractiveObject* intobj = dynamic_cast<InteractiveObject*>(obj);
               if (intobj)
               {
                   //yep, let's interact!
                   intobj->Interact();
               }
           }
       }
    }
}