Mouse object select?
by Adios, Man! · in Torque Game Engine · 01/31/2005 (9:16 am) · 2 replies
Does anybody happen to know where the code is that takes the screens x-y coordinates and checks to see if they collide with any objects?
I'm sure that I've seen this code somewhere before.
Basically, I would like to click somewhere on the screen and get an object ID of whatever was there.
Thanks in advance!
I'm sure that I've seen this code somewhere before.
Basically, I would like to click somewhere on the screen and get an object ID of whatever was there.
Thanks in advance!
#2
If you want to search for objects in a scene, you use ContainerRayCast(). But where do you start looking?
If you want to search based on where your mouse is, you convert the mouse coordinates to (x, y, -1) and pass those to your GameTSCtrl's unproject() function, which returns a 3d coordinate. unproject simply calls the gluUnProject function, which maps window coordinates the 3D world space based on the current modelview, projection and viewport matrices. You'd have to make a conosole function in gameTSCtrl.cc to get this info.
Once you have this 3D coordinate, let's call it clickPos, you can use that as the starting position for your ContainerRayCast call. You also need to know what direction you are facing. For that you can get your camera's eye vector.
In the end, the search would look like this:
%result = ContainerRayCast(clickPos, clickPos + eyeVector * searchLength, myMask);
... where the myMask variable contains the types of things that you want to search for. %result would then contain a list of all of the objects that collide with the vector.
If you wanted to make a "Duck Hunt" style game, you could do this search every time the player pulled the trigger (or hit the mouse button), and check to see if you have hit any flying duck objects.
Does anybody happen to know the specifics of how the engine is able to take a vector and check for object collisions? I'm guessing that they are traversing the BSP tree in the area of the vector, finding all of the objects that are "close" and checking those objects collision poly list against the vector... but somehow this seems like it would be very, very slow.
01/31/2005 (9:59 am)
Good stuff. A little bit complicated, though. Here's what I understand:If you want to search for objects in a scene, you use ContainerRayCast(). But where do you start looking?
If you want to search based on where your mouse is, you convert the mouse coordinates to (x, y, -1) and pass those to your GameTSCtrl's unproject() function, which returns a 3d coordinate. unproject simply calls the gluUnProject function, which maps window coordinates the 3D world space based on the current modelview, projection and viewport matrices. You'd have to make a conosole function in gameTSCtrl.cc to get this info.
Once you have this 3D coordinate, let's call it clickPos, you can use that as the starting position for your ContainerRayCast call. You also need to know what direction you are facing. For that you can get your camera's eye vector.
In the end, the search would look like this:
%result = ContainerRayCast(clickPos, clickPos + eyeVector * searchLength, myMask);
... where the myMask variable contains the types of things that you want to search for. %result would then contain a list of all of the objects that collide with the vector.
If you wanted to make a "Duck Hunt" style game, you could do this search every time the player pulled the trigger (or hit the mouse button), and check to see if you have hit any flying duck objects.
Does anybody happen to know the specifics of how the engine is able to take a vector and check for object collisions? I'm guessing that they are traversing the BSP tree in the area of the vector, finding all of the objects that are "close" and checking those objects collision poly list against the vector... but somehow this seems like it would be very, very slow.
Associate David Montgomery-Blake
David MontgomeryBlake