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.

2D All the way! / Pixel Perfect (rotated/scaled) Collision

Author
Message
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 29th May 2006 20:57 Edited at: 26th Oct 2023 04:23
While PlayBASIC has included vector based engine for nigh on 2 years now, we've finally implemented a traditional pixel level collisions. In case your unsure, Pixel Perfect Sprite Collision lets you detect collision between sprites on a per pixel basis. So if any pixel overlaps a collision is registered. It doesn't matter if the sprites are scaled, rotated, inverted whatever. The collision engine in PlayBasic can handle it for you.

To expand upon this, I'm also looking adding controls to detect collisions between vector shapes (rects / circles / lines / polygons) and sprites on a per pixel basis. So pixel perfect collisions can be combined with vector collisions.

PlayBASIC.com

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Richard Davey
Retired Moderator
24
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 29th May 2006 21:23
I'm confused - I thought PB had pixel perfect collision from day one? (unlike dbpro!!!) or did you mean between sprites and vectors? (which would be extremely cool)

Bite my shiny metal ass
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 29th May 2006 21:32 Edited at: 25th Mar 2011 06:42
PB has always used a vector based collision. Which makes pixel masking obsolete, but it's there now for those who just can't live without it.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 30th May 2006 20:50 Edited at: 25th Mar 2011 06:44
And here's the current working state of this new addition. Now you can just poll a sprite pair and you done.

So lets tick off another feature


Here's the demo code (requires PlayBasicV1.39)



Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 31st May 2006 18:16
Cool, so you'd use a mix of both? - vector based for less detailed collision (like maybe a pickup or a bullet point to point), then bitmap based for detailed sprites. Sounds like a good plan - I like vector based collision, used it in my last game (psuedo 2D).

One thing, is it all automatic? - like how can it take a sprite and apply a vector mask to it?, sounds hella complex. I just made a little editor and stored the line to line positions, then when loading I worked out the angles and radius from the centre of the sprite, rotating the mask was easy with that data. I'm maybe thinking about something else, but you got me stumped as to how you did the vector masks.

Aegrescit medendo
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 31st May 2006 20:21 Edited at: 26th Oct 2023 04:50
Quote: " Cool, so you'd use a mix of both? "


Probably not..

Quote: "- vector based for less detailed collision (like maybe a pickup or a bullet point to point), then bitmap based for detailed sprites. Sounds like a good plan - I like vector based collision, used it in my last game (psuedo 2D)."


I'd pretty much use collision shapes regardless. Since shapes can be complex or as simple as the user determines. Beside the obvious speed /memory benefit. They allow you to choose the 'hot' zone in each image.

There's a bit of a misnomer about pixel perfect collisions. Often what people might perceive as impacts based on the 'image' data, are actually performed between a mask versions of the image. So thought customization of the mask, this allows for the hard and soft pixels in the image. Which is a superior method. Since you don't always want just any none transparent pixel to register. But you know, it has to be 'fairly' newbie friendly..



Quote: "One thing, is it all automatic? - like how can it take a sprite and apply a vector mask to it?, sounds hella complex."


If you're using pixel perfect, you just load the image and compare (Obviously- See code above). There's nothing special about the image, or requirement form the user..

For collision shapes, you can define your own shapes manually or through a tool like PlayShape and just load them. Which is about as complex as this.



Then set the sprite collision mode to shape mode.

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 1st Jun 2006 13:50
Righto, neat .

One quick Q. Can it return the collision point in a vector collision? - or a decent guestimate?

Thinking that if the pivot point could be quickly set, and the collision point is returned, it would be quite straightforward to have some spinning debris effects, a simple 2D physics system if you like. Say you had a 2D sidescroller shoot em up, you might have vertical stacks of blocks that need to be cleared so you can progress, having these tapple and flip in the air would be very cool - or pivoted barriers that you have to shoot until they spin around enough to avoid (Like in Viewpoint), sounds like PB is fairly capable of such things.

Aegrescit medendo
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 1st Jun 2006 19:38 Edited at: 25th Mar 2011 06:46
Quote: " One quick Q. Can it return the collision point in a vector collision? - or a decent guestimate?"


A collision is registered when two shapes overlap. The movement path is not considered in inter sprite collisions. However, you can find the closest point on the sprites collision poly to a test point. So you could use that to find approximate potential first collider. Or run ray intersection against the collide object(s) to get a feel.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 1st Jun 2006 19:51 Edited at: 25th Mar 2011 06:47
Vector to Pixel Collision

I've made few changes to the how this is implemented. The most notable is that you can set an accuracy value. Which scales the span comparisons. So you can reproduce the overhead when segment overlap but setting the accuracy value bellow 1, For more accuracy set it higher that 1. The more accuracy the slower and more cache memory is used. Just so your aware setting it two 2 say, creates 4 times the pixel comparisons.

Anyway, i've also implemented the base segment to image comparison routines. So you can now check a vector shape for impacts against the pixels of a rotated, scaled sprite. See BoxHitSpritePixels bellow.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 1st Jun 2006 22:35 Edited at: 25th Mar 2011 06:47
PlayBASIC - Ellipse to Sprite Pixels Collision

This piccy shows that you can check if a ellipse intersects a sprites pixels. Rect are already implemented but i still have Dot / Quad & shape to do and probably a line/ray to sprite intersection also. But we'll see.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 2nd Jun 2006 00:42 Edited at: 25th Mar 2011 06:48
PlayBASIC - Triangle to Sprite Pixels

Added Triangle to sprite method, which will cover quads also.


(60fps with duron 800 + 100 pixel perfect/rotated)



Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 2nd Jun 2006 19:09 Edited at: 25th Mar 2011 06:49
PlayBASIC - Polygon Quads to Sprite Pixels

Added compare Quad (polygon) to sprite pixels collision method..

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 3rd Jun 2006 00:18 Edited at: 25th Mar 2011 06:49
PlayBASIC - Line To Sprite Pixel Collision

Here's another screenie. This one is testing the latest LineHitSpritePixels() command. While it can detect if an Intersection occurs, it can't return the first point/normal of intersection. That's not so easy..


Also added PointHitSpritePixels(). So you can detect if a pixel overlaps a rotated/scale sprites pixels





Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 4th Jun 2006 04:04 Edited at: 25th Mar 2011 06:50
PlayBASIC - Line To Sprite Pixel Collision Cont.

Here's another screenie. Had a few unexpected issues with LineHitSpritePixels() command. But it's fixed now.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 6th Jun 2006 00:37 Edited at: 25th Mar 2011 06:50
All the above collision features can be found in PB1.39b Retail beta

http://forum.thegamecreators.com/?m=forum_view&t=80902&b=20

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 11th Jun 2006 21:36 Edited at: 25th Mar 2011 06:51
PlayBASIC - Convex / Concave & Complex Polygon Shape to Sprite Pixel Collisions

This pic shows the most recent (and most difficult) intersection, that being comparing polygon shapes to sprite pixels. Speed wise it's ok, already. Even though implementation is still in it infancy, so there's certainly some room for some fine tuning. But, it's certainly doable...

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 13th Jun 2006 21:09 Edited at: 25th Mar 2011 06:51
PlayBASIC - Optimized Polygon Shape to Sprite Pixels Intersections

Here's another WIP (probably final) shot of the Shape to sprite pixel command. The command has been debugged and optimized some what. The optimization has given about *2 performance increase. Of course this only applies to certain types of overlapping segments, but hey, given that his anotherunique feature to PlayBasic, you can't complain ..

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 16th Jun 2006 22:37 Edited at: 31st Mar 2011 06:21
PlayBasic V1.41 BETA (With Pixel Perfect Features above) - (Avail for Registered Users ONLY)

PB 1.41 has the pixels modes embedded into SpriteHit + SpriteOverlap. So you can set a sprite to pixel mode (collision mode 6) and just continue on. Detection can also occur between a mixture of collsiion modes. So a rotated collision mode(1) can impact on a pixel mode(6), or a shape(3) can hit on pixel sprite(6) and vice versa. Too easy !

Pixel level collisions always assume were comparing either two transparent images or a solid vector region to a transparent image. Obviously, since there's no masking, you can't determine what's hard and whats soft in the image. So if the sprite is solid, this will make every pixel hard. ie. a solid rectangle.


Download


Download Betas


History



Login to post a reply

Server time is: 2026-07-05 11:52:33
Your offset time is: 2026-07-05 11:52:33