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 Not Working

Author
Message
Sli3her
15
Years of Service
User Offline
Joined: 20th Feb 2011
Location: Deep in code...
Posted: 21st Feb 2011 06:10
Hey all, new here at the forums. I just started programming with DBPro not so long ago and I decided to work on a frogger-like game. Anyways I've got it almost working except for collisions between the frog and cars. I have two cars that move across the screen in opposite directions, the frog of course can only be moved forward or backward. I thought I would try using the SPRITE HIT() command, but cannot get it to execute properly. Here's an example of what I'm attempting to do:


IF SPRITE HIT(FrogSprite, 0)
PLAY SOUND 1
DEC LivesVar
Reset the frog
ENDIF

If you need the full source code just let me know. Thanks for your time!
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 21st Feb 2011 09:15
Try using SPRITE COLLISION() instead. SPRITE HIT() only registers a hit around the edge of the sprite while SPRITE COLLISION() registers a hit within the sprite.

The following code snip creates two sprites and checks for SPRITE HIT() and SPRITE COLLISION(). Move the mouse to the center box.



Sli3her
15
Years of Service
User Offline
Joined: 20th Feb 2011
Location: Deep in code...
Posted: 21st Feb 2011 17:33
Grog, I failed to mention that I've also tried using that command with no luck. Maybe I will try checking for X, Y, coordinates if I can't get either of these commands to work. Below is the source code.

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 21st Feb 2011 18:22
It's because of this:


And this:


In the first code snip you place all sprites at 999,999. But because you use PASTE SPRITE all the sprites are still on top of each other and thus colliding all the time. Change PASTE SPRITE to SPRITE and any collision code you add should work fine.

Sli3her
15
Years of Service
User Offline
Joined: 20th Feb 2011
Location: Deep in code...
Posted: 22nd Feb 2011 00:12
Thanks Grog, works like it should, except that I was under the impression the SPRITE HIT command detects actual impact where as SPRITE COLLISION detects a bounded rectangle collision. For some reason they both seem to do the same thing for me. I ended up cropping the images as close as I could to each object. That seems to help. I've included the final .exe if anyone cares to comment on it or my code. I used to code a lot in VBScript and Python years ago and I wanted to get back with it. So far, I like DBPro a lot. Thanks again Grog!
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 22nd Feb 2011 05:25
Np.

SPRITE HIT() looks for the outer edge being hit while SPRITE COLLISION() looks for sprites overlapping each other. Generally it's better to use SPRITE COLLISION() for more accuracy. Yeah, the image size of the sprite determines the area that is detected as colliding. If the actual graphic was only 25x25 but the actual image was 200x200 even if it's a transparency it'll detect collision anywhere within that 200x200 area. There are several pixel perfect routines that people use. Just use the search box at the bottom of this screen and type "pixel perfect".

Your game looks good. Was this for a school project?

Sli3her
15
Years of Service
User Offline
Joined: 20th Feb 2011
Location: Deep in code...
Posted: 22nd Feb 2011 05:48
Thanks for clearing up the difference between those two commands. I'm reading through DarkBasic Programming by Jerry Ford. I'm just about through the entire book and wanted to make a game from scratch, just to reinforce all the concepts in my own mind. As you can see I'm not much of an artist,lol. Here's the entire code if anyone cares to look through it.

Login to post a reply

Server time is: 2026-07-17 13:59:47
Your offset time is: 2026-07-17 13:59:47