Archived

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

Hit testing on a D3DXSprite

This topic is 5572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would you mouse hit test on a texture drawn with D3DXSprite::Draw? Would you just use the coords and size of the texture drawn and compare those against the mouse coords? How do you account for transparency? For example, if I had a square texture that has an irregular alpha component around the outside, how would you hit test only on the texture and not what''s behind it?

Share this post


Link to post
Share on other sites
Not sure about he D3DXSprite class, but yes, if you had the screen coordinates and screen width of the sprite (in pixels), you can just check those against the mouse coordinates. Just check if the mouse x value is between sprite.x and (sprite.x + sprite.width), and if the mouse y value is between sprite.y and (sprite.y + sprite.height). (Where sprite.x and sprite.y are the top left coordinates of the sprite.)

With transparency, you''d need some way to determine the alpha value of a certain pixel. You''d have to determine on what pixel was clicked, and then check the alpha component. To determine what pixel you clicked on, you''d say, for instance, that x = mouse.x - sprite.x and y = mouse.y - sprite.y. Then, on your sprite, check the alpha component of the pixel at [x,y]. If it''s transparent, ignore it, and if it''s opaque, it''s hit.

Share this post


Link to post
Share on other sites
Awesome, thanks.

Now, if I know the transparency color key and I have a pointer to the bitmap loaded as a IDirect3DTexture8, how do I get information on a particular pixel in that texture/bitmap? I looked at the docs for IDirect3DTexture8 and didn''t see anything.

Share this post


Link to post
Share on other sites
Best not read from the texture during gameplay, for performance reasons. Your best bet is probably to calculate some sort of bitmask corresponding to the transparent regions at the start of the program.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites