Click n Pick
by J. Donavan Stanley · 04/21/2003 (12:56 pm) · 27 comments
Download Code File
This resource provides an improved mechanism for mouse selection of objects within the 3D environment. This resource improves on the mechanism used in the "Object Selection" resource to provide a more complete and general solution. This resource does NOT deal with higher level tasks such storing the selected object or "click to move" but rather makes them simpler to implement. At some point in the future, I may release my version of object selection and click to move, but implementation details for these are a design issue that varies from team to team.
Within the zip file are two patches:
world_click.patch - This adds mouse button handlers to GameTSCtrl. When the user left or right clicks over the GameTSCtrl, it will extrapolate some positional information and forward the request up to the server.
Servers can implement "serverCmdOnWorldClick" to receive these messages. This function receives the following parameters:
%client - As with all server commands, this is the client issuing the request.
%mouseVec - The mouse vector which can be used for rayCasts.
%cameraPoint - The position of the camera when the mouse was clicked.
%button - An integer identifying the mouse button. 0 for left 1 for right.
%clickCount - An integer identifying how many times the button was clicked.
The variable "$pref::ratelimits::maxClicks" controls the maximum number of clicks the user is allowed to perform per second. It currently has a default value of 4. If this click rate is exceeded a warning will appear in the console and the client will refuse to send the command to the server.
pick.patch - The patch adds two helper functions to the Sim namespace as well as the script code:
pickObject - This function accepts the mouseVector and cameraPosition from the OnWorldClick and returns the object (ID) for 3D object the user clicked on (if any). Optional parameters allow you to specify a different mask to use (more on this later) as well as indicate objects to be exempt from selection. This function expects: A Point3F vector for the mouse. A Point3F position for the camera. A pointer(c++) / ID(script) to a SceneObject that is exempt from being selected. And a S32 mask to override the default selection mask. The C++ version will return a pointer to an object or NULL. The script version will return the object ID or 0.
Sim::pickPoint / pickPoint (console version) - This function is very similar to the above except that it returns a Point3F with a world position of the mouse click.
There are a handful of script variables for tuning the behavior of these functions:
"$pref::Video::PickPointDepth" and "$pref::Video::PickObjectDepth" these two control the depth of the raycast used to determine what the user clicked on. They default to 200.
"$pref::Video::PickPointType" and "$pref::Video::PickObjectType" these two provide the default masks used. The point mask contains objects that could serve as locations such as terrain, interiors and the like. The object mask contains things the user might want to select such as items, other players, vehicles and what not.
Finally this patch adds support to the various Point classes to allow them to be created from strings. I was tried of writing dSscanf every time I needed to convert so I added new constructors and operator= functions.
This resource provides an improved mechanism for mouse selection of objects within the 3D environment. This resource improves on the mechanism used in the "Object Selection" resource to provide a more complete and general solution. This resource does NOT deal with higher level tasks such storing the selected object or "click to move" but rather makes them simpler to implement. At some point in the future, I may release my version of object selection and click to move, but implementation details for these are a design issue that varies from team to team.
Within the zip file are two patches:
world_click.patch - This adds mouse button handlers to GameTSCtrl. When the user left or right clicks over the GameTSCtrl, it will extrapolate some positional information and forward the request up to the server.
Servers can implement "serverCmdOnWorldClick" to receive these messages. This function receives the following parameters:
%client - As with all server commands, this is the client issuing the request.
%mouseVec - The mouse vector which can be used for rayCasts.
%cameraPoint - The position of the camera when the mouse was clicked.
%button - An integer identifying the mouse button. 0 for left 1 for right.
%clickCount - An integer identifying how many times the button was clicked.
The variable "$pref::ratelimits::maxClicks" controls the maximum number of clicks the user is allowed to perform per second. It currently has a default value of 4. If this click rate is exceeded a warning will appear in the console and the client will refuse to send the command to the server.
pick.patch - The patch adds two helper functions to the Sim namespace as well as the script code:
pickObject - This function accepts the mouseVector and cameraPosition from the OnWorldClick and returns the object (ID) for 3D object the user clicked on (if any). Optional parameters allow you to specify a different mask to use (more on this later) as well as indicate objects to be exempt from selection. This function expects: A Point3F vector for the mouse. A Point3F position for the camera. A pointer(c++) / ID(script) to a SceneObject that is exempt from being selected. And a S32 mask to override the default selection mask. The C++ version will return a pointer to an object or NULL. The script version will return the object ID or 0.
Sim::pickPoint / pickPoint (console version) - This function is very similar to the above except that it returns a Point3F with a world position of the mouse click.
There are a handful of script variables for tuning the behavior of these functions:
"$pref::Video::PickPointDepth" and "$pref::Video::PickObjectDepth" these two control the depth of the raycast used to determine what the user clicked on. They default to 200.
"$pref::Video::PickPointType" and "$pref::Video::PickObjectType" these two provide the default masks used. The point mask contains objects that could serve as locations such as terrain, interiors and the like. The object mask contains things the user might want to select such as items, other players, vehicles and what not.
Finally this patch adds support to the various Point classes to allow them to be created from strings. I was tried of writing dSscanf every time I needed to convert so I added new constructors and operator= functions.
About the author
#2
04/22/2003 (2:31 am)
This should do the trick for you. Be warned, this doesn't cover saving off the pointer to the selected object, it's one of those subjective things. Personaly I think that the player's selected object belongs as a member of the player class not the client connectio class.
#3
09/18/2003 (2:12 pm)
This seems to be out of date. The patch file doesn't seem to work with the latest head. I am a bit of a newbie, so I may have done something wrong, but I think that some thing has changed in the head that is incompatible with these patches, and I am new enough so I am not sure how to fix it. I would like to be able to use this behavior, so if the creator of this resource could update it, I would appreciate it.
#5
Is it possible to get this resource up with the latest head?
Thanks.
Alex
02/20/2004 (11:31 am)
Hello JDS.Is it possible to get this resource up with the latest head?
Thanks.
Alex
#6
brian quinn
02/21/2004 (7:44 pm)
Hi, sounds like what I was looking for. Can I click on the terrain and find its altitude and could I read info from objects I put on the terrain like who lives in a house?brian quinn
#7
02/22/2004 (5:23 am)
I believe that you can do that but JDS release this as a patch for the older torque. It doesn't work anymore with the latest head. :(
#8
05/26/2004 (6:12 am)
#9
08/14/2004 (4:19 pm)
With a bit of wrangling, I got it attached to the current head. If anyone's trying to do similarly, gimme a kick.
#10
10/21/2004 (9:02 am)
How do i use patches
#11
10/22/2004 (10:33 am)
This dosent work with version 1.3 of torque, can someone fix this up.
#12
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2173
that covers a good bit of this!
10/22/2004 (2:26 pm)
Just don't have enough time to look at Torque much these days. There's an object selection atwww.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2173
that covers a good bit of this!
#13
10/27/2004 (12:10 pm)
I built it with no errors, but how do i use it there is no info on this
#14
Thank you;
10/13/2005 (10:27 am)
Anybody out there resolve the console commands getMouse3DPosition and getMouse3DVector. I cannot get the values returned to me for these functions. I added the initConsole() method to GameTSCtrl and it compiled ok but no return values. I am using build 1.3 TGE with Lighting PackThank you;
#15
04/04/2006 (8:37 pm)
in gameTSCtrl.cc:static const char* cGetMouse3DVector( SimObject* ptr )
{
GameTSCtrl* obj = static_cast<GameTSCtrl*>(ptr);
char* retBuffer = Con::getReturnBuffer(256);
const Point3F &vector = obj->getMouse3DVec();
dSprintf( retBuffer, 256, "%f %f %f", vector.x, vector.y, vector.z );
return retBuffer;
}
static const char* cGetMouse3DPosition( SimObject* ptr )
{
GameTSCtrl* obj = static_cast<GameTSCtrl*>(ptr);
char* retBuffer = Con::getReturnBuffer(256);
const Point3F &position = obj->getMouse3DPos();
dSprintf( retBuffer, 256, "%f %f %f", position.x, position.y, position.z );
return retBuffer;
}
ConsoleFunction( getMouse3DVector, const char*, 1, 1, "GameTSCtrl.getMouse3DVector();" )
{
SimObject * obj = Sim::findObject(argv[2]);
return cGetMouse3DVector(obj);
}
ConsoleFunction( getMouse3DPosition, const char*, 1, 1, "GameTSCtrl.getMouse3DPosition();" )
{
SimObject * obj = Sim::findObject(argv[2]);
cGetMouse3DPosition(obj);
}
#16
04/05/2006 (3:31 pm)
Thanks much. I will check this out. By the way I am now using TGE 1.4. Does this still apply?
#17
Is anyone familiar with a tutorial (or sample code) for dragging and dropping in the 3D environment.
[I was told the Editor does this, but since I'm a newbie doing it by myself is too difficult...]
Any help would be greatly appreciated!
06/28/2006 (1:26 am)
Tx for this resource. Really helpful!Is anyone familiar with a tutorial (or sample code) for dragging and dropping in the 3D environment.
[I was told the Editor does this, but since I'm a newbie doing it by myself is too difficult...]
Any help would be greatly appreciated!
#18
If you are needing picking, dragging, dropping and such then I have heard that you can get these things as part of the RTS Pack. I am looking at Click'n'Pick because ALL I need is picking, but I'm still quite honestly tempted to pick up the RTS Pack.
07/02/2006 (8:57 am)
Itay -If you are needing picking, dragging, dropping and such then I have heard that you can get these things as part of the RTS Pack. I am looking at Click'n'Pick because ALL I need is picking, but I'm still quite honestly tempted to pick up the RTS Pack.
#19
Guess I'll just hafta buy the RTS Pack.
07/02/2006 (10:54 am)
FWIW, I am completely failing to get this to compile in 1.4. I was able to get it merged in, but no love on the compile.Guess I'll just hafta buy the RTS Pack.
#20
07/29/2006 (12:07 pm)
. 
Torque Owner Alex \"bathala\" Rufon
We'll a few weeks back, I'ved decided to NOT to use the "Object Selection" resource anymore ... now ... I have to branch my CVS and apply this code. Thank God for CVS!!!
Kidding aside, thanks for this resource J. Donavan. I'll try it out as soon as I get home and branched my project's CVS. :)
Actually, one of the reasons why I'ved abandoned the object selection through mouse click was the lack of support for the right mouse button. I hope this is the answer. :D
Thanks.
Alex