Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Detecting mouse click on non-transparent area of a sprite

Author
Message
Pi314159266
User Deleted
Posted: 25th Jul 2009 05:46
There is probably a simple solution to this, but it has eluded me.

In my program, I want to detect when the user has made a mouse-click on a certain sprite, but only when the mouse has clicked on a part of the sprite that isn't transparent.

I was thinking that I could find the color of the point that has been clicked and checking if it's transparent, but I'm not quite sure how to execute this.

Thanks for any help.
Deego
16
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 26th Jul 2009 19:14 Edited at: 26th Jul 2009 19:24
There's not a simple solution, per se. Check out Cloggy's work on this: http://forum.thegamecreators.com/?m=forum_view&t=51743&b=6

For more information, search the forums for pixel perfect sprite collision.

You could try this; have a flag to toggle the collision check on or off, set a pixel sprite at the point of the projectile object, make sure the background isn't pasted yet so the backdrop is on, use GET IMAGE N,sprite x(pixel sprite),sprite y(pixel sprite),sprite x(pixel sprite)+1,sprite y(pixel sprite)+1, turn that image into a sprite, use SPRITE RED/GREEN/BLUE to find the color values, and if the color matches the backdrop color, keep the collision check flag off, and if it is anything else, turn the collision check flag on.

I haven't tried this, just thought of it.
Pi314159266
User Deleted
Posted: 26th Jul 2009 20:38
This is a lot like pixel perfect collision, but unfortunately DBPro hasn't yet made a function for that. Since I really only want to see if a single point is colliding with the sprite, your idea seems reasonable. I'm wondering if get image would do practically the same thing as memblocks. I could probably create a memblock from the background and the target sprite, and check if both sprites share the same color at the point that was clicked.
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 29th Jul 2009 00:47 Edited at: 29th Jul 2009 00:49
A while ago I wrote this little library to do pixel perfect sprite collisions, including point-on-sprite collisions. Check it out, and see if it's what you're looking for.



Note, this was actually intended to be included as a separate source file, but you could stick it anywhere, I guess.

Edit: I haven't tested this out on sprites using images with alpha transparency. These routines all ignore a transparent color (by default, black).
Deego
16
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 29th Jul 2009 06:45 Edited at: 29th Jul 2009 06:55
Here's the alpha test! Media in the download.



Four images; Completely transparent areas, Completely solid sprite, half transparent alpha, and solid sprite with the old sprite collision check.

Mouseclick to change the image number for the SpriteCollide check.

Works like a charm. Look at what the framerate does.

Attachments

Login to view attachments
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 29th Jul 2009 11:42
Hey, thanks a lot for doing the test!
Glad to see it worked.
Deego
16
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 29th Jul 2009 21:58
Playing around with it a bit more, and I found that by adding



at the beginning of the SpriteCollide function, when spr2=0, it will return the number of the colliding sprite, regardless of whether you use SpritePointCollide or not.

Excellent collision function, Kira!
Pi314159266
User Deleted
Posted: 30th Jul 2009 00:19
This all seems like some great code, but maybe I'm in the wrong part of the forum... My code is all in C++.

But thanks for the great ideas too. I don't think it would be too hard for me to translate this into something that can work for me.
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 30th Jul 2009 10:08
@Deego:
Hm? I'm not sure what you're saying. I was pretty sure SpriteCollide did that already, without that bit of code. And I haven't played around with it like that, but by deleting the dummy sprite, it seems as though SpritePointCollide would be disabled, as that function uses the dummy sprite to check for collision.
At any rate, it makes me very happy that people can find a use for my tinkerings.

@Pi314159266:
Ah, it sounds like you're using DGDK. There's a separate board for that, but I suppose it could be useful to take ideas from here. Hope it helped.
(I'm a little curious as to how you chose your name.. 3.14159266 seems like a rounding error to me... )
Deego
16
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 30th Jul 2009 18:04
I deleted the dummy sprite, because it kept returning 1000; the dummy sprite. SpritePointCollide() still works, because that function recreates the dummy sprite with it's first line. Just getting the functions to work together better, since I can see times when you'd like to check both and return the correct values.
Pi314159266
User Deleted
Posted: 5th Aug 2009 04:07
Yes, I think I'm on the wrong board, and I'll be sure to use that one from now on. And my name is a rounding error, because it almost always guarantees that the username won't be taken!

Login to post a reply

Server time is: 2024-11-25 04:33:56
Your offset time is: 2024-11-25 04:33:56