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.

DarkBASIC Professional Discussion / 2d sprite collision / transparency issues

Author
Message
method321
15
Years of Service
User Offline
Joined: 28th Jan 2009
Location:
Posted: 3rd Feb 2009 19:46
Ok, i have been working on a 2d game where you have to use the mouse to click stationary/non animated sprites on the screem and they get deleted once clicked.

I have built a subroutine to place all the objects at specific locations, using the load image command. I have also built a special 1pixel x 1pixel sprite that i hide, and have its location mapped to the mousex()/mousey() of the mouse, so that i can use that specific 1 pixel hidden sprite to check if it is touching/colliding with one of the visible sptite objects on the screen.

These subroutines are working as intended, and i can move the mouse from one object to the other, and when it is colliding and the mouseclick()=1 then that sprite is deleted. When all the sprites are gone, then the level ends. Pretty simply so far.

The issue i am having is that you can click on the transparent parts of the boundry box and it also counts as a collision and will delete the object.

What i am trying to figure out is how to make pixel perfect collisions. I have been playing around with .PNG fiies and alpha maps.. but either i cant figure out what i am doing.. or i am doing it wrong.

to visualize what i am saying, i will demonstrate below in ascii.
i make on object in a graphics editor, lets call it objects S, or sprite S, with a black backgound T, for transparency.

I can place the sprite on the screen, and since i am using a black background, the only visible portion of the object are the S's
but if you click the T's area, it is detecting it as a collision and allowing the object to be deleted, as is the action action in my subroutine when a collision ocurs and the mouse is clicked, yet from the perspective of the player.. they clicked on a black background..

tttttttt
tsssssst
tsssssst
tttttttt

what do i have to do, to make the T area of the image not interact with the spite collision procedure, and only cause a collision when actually the S postion is clicked.

Sorry this is so long winded.. but i have been working on this for 2 days now.. and cant seem to get past this step..


here is a code snip of my routine for collision/deletion

for z =1 to 5 (this loops thru the 5 sprites i have on the screen)
if sprite hit(10,z)>0 (sprite 10 is my 1pixel that track with the mouse to determine x/y location and if it is hitting another sprite)
if mouseclick()=1 (checks for mouse left click while collision is occuring)
delete sprite z (if both collision and left click occur then that sorite is delete)else
endif
endif
next z


any advise would be helpful.. as i cant move on in this game until i overcome this issue.
pollywog
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location:
Posted: 3rd Feb 2009 20:41
alot of times I will use a scratch sprite.
that is smaller than the original sprite and is invisible.
I set its position equal to the original sprite.
then when I do collision detection I compare against the scratch sprite instead of the real sprite.
still not pixel perfect collision but it is usually so close you can't tell the difference.

I know there are some pixel matching commands for collision but I've never had alot of luck with them. but you can try them out.
I forget what they are at the moment but I am sure the documentation has them.
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 4th Feb 2009 02:36 Edited at: 4th Feb 2009 02:38
There you go, making me write my little libraries again.

Well, I dug around in the DBPro documentation and couldn't find any sprite collision commands aside from sprite hit and sprite collision, let alone anything that matched pixels. Maybe I'm blind.

Regardless, this should do the trick for pixel perfect sprite collision. I've attached the actual source file, but I'll past the code below anyway.



The library is pretty simple to use. Just call InitSpriteCollision() before using any of the other commands.

You are given the option of setting a transparent color by using SetTransparentColor(color as dword).

To test collision between two sprites, use SpriteCollide(spr1,spr2). There's also the option of checking a single point for collision with SpritePointCollide(x,y,spr). Currently, sprite on sprite collision does not support the scaling or offsetting of either sprite whereas point on sprite collision should be fine.

There are comments at the top of the library.

Edit: Wow, forgot the attachment. Here it is. *slaps forehead*

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-05-19 13:33:06
Your offset time is: 2024-05-19 13:33:06