Game Development Community

Mouse events w/ sprites

by John Burkert · in Torque Game Builder · 03/14/2005 (7:17 pm) · 11 replies

Is there a quick way to determine if the mouse position is inside of a sprite?

#1
03/15/2005 (2:05 am)
Yes.

Assuming you don't want to pick a point, area of line (see pickPoint, pickArea, pickRay ) to check if the object is there you can use the function "fxSceneObject2D::getLocalPoint()" which will convert a world-position into a local-space position for the object. This space is in the same format as the mount/collision-polygon system where (-1,-1) means top/left, (+1,+1) means bottom/right. The returned point, if outside of the sprite, will be outside of this range.

This function takes position, size and rotation into account but it does not have anything to do with the collision-polygon.

It would probably be a useful function to be able to ask an object if a specific point is within it. I'll add that to my list of things to do as I can see if being generically useful in many situations.

Hope this helps John,

- Melv.
#2
03/15/2005 (7:01 pm)
Thanks for the reply. since coords are in the format "x y" how would i compare a mouse coord to a sprite coord? sorry if this is a dumb question, i am used to using integers for x and y coords.
#3
03/15/2005 (7:16 pm)
As Melv said, the "coords" are relative to the sprite in question. So anything within the range -1 to +1 along x or y is inside the sprite. "0 0" ("x y") would be the exact center, "1 0" would be far right, center. Etc.

A point outside the sprite (for example) would be "-2 3" ("x y"). Fractional values are also acceptable (ie, "-0.524 0.877", which is inside the sprite because 1 > x > -1 and 1 > y > -1).

Hope that helps!
#4
03/15/2005 (7:51 pm)
Thanks, but that isn't what i mean. i get that part. what i don't get is how do i use the x and y values when they are stored in a string, "x y"?

traditionally i would do something like

if (mouse.x > sprite.x && mouse.y > sprite.y && mouse.x < (sprite.x + sprite.w) && mouse.y < (sprite.y + sprite.h))
{
// mouse coord is inside of sprite
}
#5
03/15/2005 (7:55 pm)
%point = "10 -15";
%point.x = getWord(%point, 0); // %point.x is now 10
%point.y = getWord(%point, 1); // %point.y is now -15

At this point, since TGEScript is a typeless language, you can treat your new variables as numbers directly, such as the logic checks you listed.
#6
03/15/2005 (7:59 pm)
That's what i was looking for. many thanks :)
#7
03/15/2005 (8:19 pm)
Since John was appeased, allow me to hijack since it is on topic. Documentation says there is a good object picking system. Is there an object picking function that returns a reference to a sceneObject?
#8
03/16/2005 (12:56 am)
@Jeremy: fxSceneObject2D? pickPoint(), pickArea() and pickRay() only work on objects in a fxSceneGraph2D and should be able to pick anything from fxSceneObject2D upwards.

Does this answer your question?

- Melv.
#9
03/16/2005 (3:39 pm)
Sort of,

Your response to

www.garagegames.com/mg/forums/result.thread.php?qt=27483

was what I was looking for. It does return not only an object, but a list of objects(which makes more sense). Still getting undesirable effects, but I'll figure that one out on my own. Thanks!
#10
03/17/2005 (12:23 am)
Cool. :)

- Melv.
#11
03/20/2005 (5:12 am)
FYI

Just thought I'd mention that you can use "getIsPointInObject()" to determine if a world-point is within the objects collision-polygon.

I completely forgot about this function and thought I should mention it. See the ref doco for more information.

- Melv.