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 / Sprite, Pixel Perfect collision?

Author
Message
Dark Dragon
18
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 28th May 2010 04:14
I Cant seem to pull it off......

DBpro has Two Spr Collision commands, Sprite HIT(), and Sprite Collision(). Neither allow pixel collision, only bounding box collision. So is there any way to use pixel perfect collision in DB?

GIDustin
17
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 28th May 2010 07:06 Edited at: 28th May 2010 07:06
Dug this up in the codebase back when the codebase existed....



Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 28th May 2010 16:40
In DBPro you have to consider the best option - because since DBPro uses DX9, it's sprites are 3D now, DBClassic has 2D sprites so pixel perfect collision works on that.

Personally I prefer to use line based collision with sprites, because if you take 2 sprites and rotate them, then working out pixel collision with that can be really slow, if you can even get that working - that snippet does not allow for rotation.

I use a segmented line around the sprite, stored as the angle and radius from the centre of the sprite. This covers 2 bases, rotation, and scaling - with my technique, checking collision with rotation and scaling is actually standard. One other benefit is that it's possible to ascertain which line gets shot, or collides. This might allow you to detect head shots, or decide how much to spin the sprite, factoring centre of gravity and all that stuff in as well. A space ship might have really strong legs for landing, but land on the wing and it's curtains. I think it facilities proper game creation, nobody actually uses pixel perfect collision all that much - it's far more likely to be box collision or line collision.

I keep meaning to find the time to make an example, will try and do that over the next couple of weeks. I only used it in 1 game, a sidescrolling shooter, and was accurate as hell. One neat thing is that it's quick and easy to detect circle line collision too. IMO line collision is pretty much all win, more people should adopt it, it takes a horrible problem and provides a somewhat complex solution - but it has benefits all over the place, that pixel perfect collision could only dream of.


Health, Ammo, and bacon and eggs!
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 28th May 2010 17:06
If you look around you'll see that (practically) no commercial game uses pixel-perfect collision, why? Because it's extremely processor intensive, and not usually necessary.

As Van B said, line-based collision (and other shapes if you want to implement them) is a much better way to go. It's much, much faster and should be just as accurate as the pixel-perfect collision you're looking for.
Dark Dragon
18
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 28th May 2010 19:41
Ok, maybe pixel collision isnt what I need, But, I have no Idea how to use line collision.......and while my game is 2D, I need to factor in a "Z" position too......Anyone have an explaination of line collision?

@GIDustin

Thanks! I'll take a look. But,Is it slow?

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th May 2010 22:06
Yes, although it's about as fast as I could make such a brute-force collision check

Personally, I'd use smaller bounding boxes or circles for the sprites, maybe using more than one per sprite for better coverage of the shapes.

Circle/Circle collision is trivial, Circle/Rectangle is simple once you rotate your rectangle to 0 degrees and rotate the circle centre point in the same way and Rectangle/Rectangle is a fast set of line intersection checks (up to 16 checks worst case, but the worst case is most of the time ).

Dark Dragon
18
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 29th May 2010 00:31
IanM,

I'm not one to just ask for code just straight, but, Could you show me an example? and if it helps, I'm helping a mate with a Beat 'em up/Fighter.....

DVader
21
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 31st May 2010 16:52 Edited at: 31st May 2010 16:56
Well a technique I tried on a 2D shooting engine a while ago, was to use a hidden screen with the scenery drawn in a single colour. I set up points around the ship to roughly match its shape. Then checked those points on the fake screen, if the colour was anything other than black it collided. It worked well, was still fast and smooth and had the advantage of allowing as intricate a design as you wanted.
I'm not sure how it would work for sprites on sprites, but off the top of my head can't see any reason why it wouldn't. You could colour the collision sprite with basic colours for head, body,hands, legs etc. Then you would need to check for collision at a certain frame, or frames of your animation. So when you have your character punch, it only impacts within a range of frames. Then you would check your control points colour and play the relevant animation for being hit.
To set up control points use your sprites x,y and offset as needed. If you wanted to be really clever they could change as the anim runs.
Edit - you need to use paste sprite on the hidden screen and sort out the screen refresh yourself btw. Then check the screens colour co-ords against your sprites control points. I would feel fairly confident getting the basic engine of a beat em up up and running. The AI of the fighters however, would be another matter.

http://s6.bitefight.org/c.php?uid=103081
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 31st May 2010 17:12
With that idea DV, I would suggest ImageKit, pasting the images onto images, and using a set colour for CLS instead of making mask sprites.

The concern I have is that POINT is just too slow, memblocks are too slow, so it would need something that can calculate pixel colours very quickly. I think that every POINT call in DBPro results in a sync, so you can imagine how much it affects frame rate.

Would be the ideal technique for a Worms style game.


Health, Ammo, and bacon and eggs!
DVader
21
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 2nd Jun 2010 01:02
Point may be a little slow, but it worked fine in my shooter engine. I had about 6 points on my spaceship to check. Possibly it would slow down with a few more. But as that was all I needed it sufficed. I found the parallax scroll effect I did was the worst for slowing the game down. More than 4 background layers started to make it slow a little, more so on slower machines. There was no collision on those layers.

http://s6.bitefight.org/c.php?uid=103081

Login to post a reply

Server time is: 2025-08-09 01:32:11
Your offset time is: 2025-08-09 01:32:11