Sign in to follow this  

Check mouse collision with sprites (C# DirectX 9)

This topic is 3856 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

Hi guys, i need your experience. I need to check if mouse pointer is over a sprite and i don't wanna check in every single render loop, if the mouse position is between the coordinates X->Width, Y->Height of every sprite in my scene cuz this would spend too much time. There's a less memory stressing way to do this? Thanks to everyone.

Share this post


Link to post
Share on other sites
There are two things you might want to consider. If you're doing simple bounding box intersection tests (as you seem to be doing), these shouldn't put too much of a strain on your performance. True, it might not be ideal to continously check all the sprites, but you should consider yourself lucky if that's the only thing eating your CPU time you need to worry about. You could reduce the number of tests with spatial partitioning techniques (a quadtree might be useful), but for a typical sprite scenario that might already be overkill.

Another thing to consider is using a fixed timestep for your updates (if you're using XNA, this is already done for you). This essentially entails using a timeout to update only on some fixed interval (for example once every 20-30 milliseconds), so you don't waste time on updates that aren't visible or important anyway. Using a fixed reasonably sized timestep also has the added benefit of reducing any errors introduced by very small updates you might get if you update at full framerate.

So I'd recommend using a fixed timestep, but I wouldn't worry too much about the small performance penalty you get from running this check every once in a while. I doubt it'd be even noticable unless you have hundreds of sprites on screen.

Hope this helps :)

Share this post


Link to post
Share on other sites
There might be some kind of trick you could do with the z-buffer (or even a destination alpha channel). Clear it, render your sprites, then check the brightness of the buffer where the mouse pointer is. I have no idea if this would work but this is the only type of option you might have as opposed to the spatial partitioning idea mentioned. I would use the brute force algorithm and make SURE it really is a CPU hog before you bother optimizing it. It should be able to handle a SOLID number of sprites before you notice any problems.

Share this post


Link to post
Share on other sites
It may sound costly on paper, but unless you have thousands of sprites to test against you'd be surprised how cheap point-in-box collision tests are, when seen in the light of the rest of your render loop.

As MasterWorks says, code now and profile later. There really isn't much you can do to optimise against this, and efforts to do so just may be counter-effective.

Admiral

Share this post


Link to post
Share on other sites

This topic is 3856 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this