Archived

This topic is now archived and is closed to further replies.

Caster

Precise mouse (object / monster) targeting?

Recommended Posts

I''ve run into a little problem with mouse targeting. The easiest way (how I''m doing it atm), is to just be able to target the monsters/items is to just use the rectangular box that it is drawn with, to determine if the item is targeted. However with that, it is not precsise. You can click near the object and it''ll probably target it, even though the actual object is not drawn there(because of transparency). I want it to only be targeted by clicking exactally on the monster or object, not just the rectangle. Any suggestions?

Share this post


Link to post
Share on other sites
you could make a mask for them... that is, for every monster/thing/whatever sprite you want to use, make a bitmap that is identical in shape, but is white where you can click him and black elsewhere (around the edges, etc)... when the user clicks within the monster''s rectangle, check the coordinates against this mask... for example, if they click 2 pixels right and 3 pixels down from the upper-left corner of the monster''s box, check the pixel at (2,3) in the mask; if it is white you clicked him, otherwise you missed.
there might be better ways to do this, and it can get annoying if you have like thousands of differently shaped sprites... in which case i dunno

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
You don''t even need to create a new bitmap. Just use the one you already have made. If the user clicks a transparent pixel he did not hit the object, and else he did.

Share this post


Link to post
Share on other sites
I got a similiar problem and since this thread is already up I''ll ask here, how do I check the pixels color or alpha value?

Humanity''s first sin was faith; the first virtue was doubt

Share this post


Link to post
Share on other sites
Zredna:

I was thinking about that, but I don''t really see how that is possible as a transparent value doesn''t get drawn, the user would just be clicking on the tiles below the object. I guess the mask thing would work, but that would be a lot of extra artwork with isometric view, seeing as each monster has 8 directions.

Share this post


Link to post
Share on other sites
true... but you wouldn''t have to actually DRAW the extra artwork... you could take your original graphics and run a small program on it to change any non-transparent pixels to white, and transparent ones to black, then save it as a different file.
on zredna''s idea: don''t check the pixels on the screen, check them on whatever surface you have the sprite stored in (before the blit)...

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
I didn't mean that you should check the colour values on the screen. This would basicly be the same thing as krez talked about. But this would use less memory, because you don't create more bitmaps...

Anyway, let me explain the concept:
Calculate the (x,y) value of where the object is drawn to the screen (the upper-left corner), and then substract the it from the mouse coords and check that point.

Example:

// object is a bitmap (the same that have been drawn to the
// screen).
// mouse.x/y is the mouse location on the screen
// object.x/y is where the object is drawn to the screen
if (get_pixel(object, mouse.x-object.x, mouse.y-object.y)) == TRANSPARENT_VALUE) objecthitted=TRUE;

SkyRat: That depends on what language and graphic-libary you are using.


-Zredna

Edited by - Zredna on October 21, 2001 2:14:09 PM

Share this post


Link to post
Share on other sites
Presuming that you''ve managed to draw your sprite using transparency, then you already have a bitmask or an image with an alpha channel. You can just test against rectangles first to see which unit you clicked, then test against the bitmask or alpha channel.

Is there a particular reason why you need to have this kind of selection? If you''re having trouble selecting a unit because it''s obscured by a unit in front of it, then that''s fine.

I would say that if the coordinates match against a unit''s rectangle, and there is nothing selectable behind that unit, you should select it even if you clicked on a transparent area. Otherwise, a user who isn''t good with a mouse may have trouble selecting a unit that is small or has lots of transparent bits.

Also, if the problem is obscuration-based, I have a method that works fine for me: if you click on a point that contains one unit, you select it. If you click on a point that contains multiple units, then, if one of the units is already selected, you select the unit under it, or, if none of the units are selected, you select the first unit. That lets you select units that would otherwise be unselectable. In many games, for example, you can lose a unit behind a selectable building and be unable to select the unit because mouse clicks are eaten by the building. (You can always multiselect them both, and then unselect the building, but that''s a long-winded approach.)


Uuuuuulrika-ka-ka-ka-ka-ka

Share this post


Link to post
Share on other sites