What is the BEST way for mouse-selecting objects?
by Dave Calabrese · in Torque Game Builder · 03/16/2005 (1:24 pm) · 13 replies
I want to have a large amount of objects on my screen, all of which the player can click on with the mouse. After thinking about it and reading forums, I'm finding a few different possibilities...
1.) On every millisecond, check if the cursor is over an object. If so, return the object and wait for a click.
2.) Have the cursor act like another object and have it interact with the collision meshes around objects (but reading around it sounds like this is currently not able to be done without some serious code additions?).
3.) Cast a ray from the camera and see if it intersects anything. (But I keep thinking "This is a 2D engine - WHAT camera?" But I could be way off on thinking that here, so I still see it as a possibility until I read that there truly is no Z axis in the T2D engine).
That's what I've come up with so far... anyone got other ideas, suggestions, hopes or dreams for this?
Thanks,
-Dave C.
1.) On every millisecond, check if the cursor is over an object. If so, return the object and wait for a click.
2.) Have the cursor act like another object and have it interact with the collision meshes around objects (but reading around it sounds like this is currently not able to be done without some serious code additions?).
3.) Cast a ray from the camera and see if it intersects anything. (But I keep thinking "This is a 2D engine - WHAT camera?" But I could be way off on thinking that here, so I still see it as a possibility until I read that there truly is no Z axis in the T2D engine).
That's what I've come up with so far... anyone got other ideas, suggestions, hopes or dreams for this?
Thanks,
-Dave C.
About the author
Recent Threads
#2
03/16/2005 (1:29 pm)
Ya know, I glanced at that, but saw it was shooting vectors about a 3D plane and thought it wouldn't work in T2D... perhaps it will! And perhaps I should download and try it... and perhaps I should stop saying Perhaps..
#3
Check the fxSceneGraph2D section of the reference doc. pickPoint() takes a bunch of optional parameters as well and it gives a pretty good description of them.
03/16/2005 (1:54 pm)
You could use the pickPoint() method in fxSceneGraph2D by passing it the mouse x and y coordinates on mouse down. It returns a space separated list of the IDs of the objects occupying the given point. I haven't dealt with mouse input yet, so I can't say whether you have to convert the mouse click position from screen coordinates to world coordinates, but it wouldn't be difficult.Check the fxSceneGraph2D section of the reference doc. pickPoint() takes a bunch of optional parameters as well and it gives a pretty good description of them.
#4
I'm not able to test this right now, but I will definitly take a look at this tonight and post a followup on if this worked or not (along with any additional sample-code to make it easier for other people looking for this solution!)
Thanks!
03/16/2005 (2:04 pm)
Excellent!I'm not able to test this right now, but I will definitly take a look at this tonight and post a followup on if this worked or not (along with any additional sample-code to make it easier for other people looking for this solution!)
Thanks!
#5
The mouse callbacks return everything in world units so it's nice and easy to do...
There's also a "pickArea()" and a "pickRay" (line).
- Melv.
03/16/2005 (2:45 pm)
Perhaps you should Dave. ;)The mouse callbacks return everything in world units so it's nice and easy to do...
function sceneWindow2D::onMouseMove( %this, %modifier, %worldPosition, %mouseClicks )
{
// Pick some Objects from my scene.
%pickList = mySceneGraph.pickPoint( %worldPosition );
// Get Object Count.
%objCount = getWordCount( %pickList );
// Ignore if no objects picked.
if ( %objCount == 0 ) return;
// Handle Objects.
for ( %n = 0; %n < %objCount; %n++ )
{
// Get Object.
%obj = getWord( %pickList, %n ) );
// Do something with object.
%obj.blah();
}
}There's also a "pickArea()" and a "pickRay" (line).
- Melv.
#6
You guys thought of everything with this engine! It's almost TOO easy to use... almost like you guys are planning something... something evil.... involving those things....
03/16/2005 (5:13 pm)
Woohoo!You guys thought of everything with this engine! It's almost TOO easy to use... almost like you guys are planning something... something evil.... involving those things....
#7
*ahem*
- Melv.
03/17/2005 (12:20 am)
That would make you part of our evil army! Our soldiers of 2D doom ....... mwwwaarrrrrrrrrrrrrrr.*ahem*
- Melv.
#8
03/17/2005 (12:30 am)
Mmm... chainsaw goodness will be had by all!
#9
03/17/2005 (2:14 pm)
Only thing i'll add is with the code melv added u may wonna add a flag that gets set to 1 when the mouse button goes down and gets to 0 when it comes back up and only do the %obj.blah() if the flag is set to 0 so it only gets done once, if i click mouse button it can execute the code about 15+ times in the time it takes me to release the mouse
#10
03/17/2005 (3:04 pm)
OnMouseDown() is better for catching mouse clicks.
#11
03/17/2005 (3:11 pm)
Tis true, as i just found out, lol was going thru my code a realised where i copied the top part of Melv's code was my problem, i should read in future then i'll see the onmouse move
#12
03/17/2005 (3:16 pm)
Mousemove might be good for creating a rectangle for a pickArea() function...like for an RTS.
#13
Sorry for the confusion.
- Melv.
03/18/2005 (12:43 am)
Oops, my bad. Sorry, I meant to put that in the "onMouseDown" callback.Sorry for the confusion.
- Melv.
Associate Joseph Euan