Game Development Community

Detecting when Mouse is over an Object...

by Clark Kromenaker · in Torque Game Engine · 12/30/2005 (9:16 am) · 3 replies

Hey guys,

I'm sure most of you've played games where the cursor lights up or changes when over a selectable object; the world editor even does that itself for object selection. I'm interested in doing that in the actual game. However, I looked at the world editor code and decided since I wanted to avoid engine source changes as much as possible, I could do it differently. So, I simply went to the gameTSCtrl source code and added a con::executef to the bottom of the "onMouseMove" function, so that I could expose it in script and do my stuff there. I stuck an echo statement in script-side and it works fine. Phew.

I have some questions, though, about how to detect objects in 3D space. Since I've got the onMouseMove being called every time the mouse moves a pixel, I thought it would be ideal. I have code from a previous resource that gives me the mouse's 3D position, so I've got that. Now, is there a function that can check whether a given 3D Position is inside of a bounding box? If not, how might I get anything related to the bounding box that would allow me to perform this check? It would be ideal if the function returned the object that the bounding box belonged to...

So, at this point, if the cursor is in an object's bounding box, I just change the cursor with Canvas.setCursor(), I believe.

I guess my final question would be whether there is a better way of doing this...

#1
12/30/2005 (9:44 am)
I got this functionality working in a short amount of time by basicly copying what the Editor GUI does. I do a cast ray in onMouseMove(all of this in C++) and if I've hit a selectable object(types defined in a static U32), switch to the "hover over" cursor image.

Edit: Doing the cast ray in script is plain redundent, unless you need to do it server side(which for mouse overs, I'd assume you don't).
#2
12/30/2005 (9:44 am)
You want to use a rayCast to determine if the mouse is over anything.

The "ray" in this case is the line starting at the 3D position of the cursor and projecting into the scene away from the eyepoint.

Have a look at this resource: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7335

- you'll want to modify it fairly heavily 'tho in at least two respects:

* do everything client-side

* cut out the calls to script except for the final notification the hover-over object has changed.
that is, you don't want calls to script being generated every time the user moves the mouse,
only when they move the mouse and it's interesting.
#3
12/30/2005 (10:26 am)
Thanks,

I actually used that resource heavily for getting the player to move where a mouse click is. My project is in no way professional and it isn't going to include multiplayer in any form, so I'm not too worried about server-side and client-side, just so long as it works ;).

But, I'll try this out. Thanks again.