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 / Pixel perfect sprite collision with eXtends

Author
Message
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 11th Dec 2010 09:22
Hi,

I'm building a game which requires pixel perfect sprite collision so I bought the eXtends plugin which comes with commands to deal with this. However so far, I've been unable to achieve anything other than normal area sprite collision results.

I started trying to use png files with their own transparency but from the advise other similar threads of this forum, I've reverted to using bmp files with the transparency achieved through the set image colorkey command. None of the sprites being checked for collision have been rotated and the origin is set to 0,0.

Below is the code in my main loop which is supposed to deal with this. The first loop updates the logical position of each sprite in turn. The next loop updates the actual position of each sprite and then prepares of for collision checking. The third loop goes through all the sprites and checks whether they collide with any relevant sprites. If they do, the sprite is destroyed. All the words in capitals are constants.



The result of this code is that a collision is detected as soon as the two box perimeters of the sprites come into contact rather than when visible pixels overlap. Any help on how I can change this to achieve pixel perfect sprite collision would be greatly appreciated.

Obviously, if there is any further information you need, don't hesitate to ask and I will post it here. Thanks in advance.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Dec 2010 16:48
Unfortunately I don't have eXtends but if your sprites are never going to be rotated there are several examples of pixel perfect sprite collision using memblocks. Just use the Thread Subject Search box below and type in Pixel Perfect Collision.

Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Dec 2010 16:57
Why is it necessary for it to be pixel perfect? It's slow and sprites will pass through each other if moving too fast. I'd suggest using vector-based collision instead.
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 12th Dec 2010 17:59
Thanks for your responses. I should probably have a go at memblocks but as they look quite tricky and I've already bought something which claims to make it easy, I hoped it could get it to work. Doesn't matter though. If I have no luck with it I will definitely look into memblocks properly. As for vector collision, I'm not really sure how to do that either.. Am I right that that is where you create a logical shape with is approximately similar to the outline of the sprite and see if there is any overlap between them?

The situation is that there is a sprite of an island from top down view in the middle of the screen which has a very jagged outline including a couple of inlets. I need to check of the dots on the screen representing boats have crashed into it. I also need to check for collisions with ports which, of course, means they have safely made it. As the ports are on the coast of the island, I need to have very accurate collisions. Otherwise, as at the moment, it is impossible for the ships to get to port because a collision with the island is always detected first.

I've attached a screenshot which I hope makes this clearer. The blue squares are the ports, the green blip the ship and the thing in the middle the island.

Anyhow thanks for your help so far. I'll look into both of your suggestions. If anyone does know how I can get extends working, it would be very useful too.
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Dec 2010 20:46
Quote: "Am I right that that is where you create a logical shape with is approximately similar to the outline of the sprite and see if there is any overlap between them?"


Correct. In your case it would probably be best to use memblocks though, assuming you want very accurate detection of when a boat hits the island.
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 13th Dec 2010 07:11
Here is a program I wrote up using eXtends pixel perfect sprite collision commands.

One slight draw back I have found with these commands is that it only appears to work with bitmap images. I have yet to get it to work with other image foramts.

Hope this helps

A clever person solves a problem, a wise person avoids it - Albert Einstein
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 13th Dec 2010 11:14
Hodgey, thanks very much for your example, it runs perfectly for me and it's great to see the plugin working. However I must admit I'm still struggling to understand what I'm doing that is causing it not to work...

I use the spr load image and spr sprite commands but as these are the native ones for eXtends, I assume they are compatible so this is not causing the problem. I use an animated sprite and the set sprite alpha command but I've tested both of these in your example and they don't seem to have any significant effect on the accuracy. I use flag 0 rather than 1 for collision checking but again, when tested in your example, this works just as expected. The sprites I am attempting to collide are bmp although I have pngs loaded. However again, having modified your example, this doesn't seem to cause a problem.

Anyway now I have a fully working example, I'm sure I'll be able to figure out the crucial difference which is causing a problem in my code. Thanks again. It's good to know that the plugin is capable of of this and it's my code which is the problem; this way round should be much easier to fix!
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 13th Dec 2010 17:14 Edited at: 13th Dec 2010 17:15
I'm guessing your images are the problem.
You MUST use images without any compression like a .jpg has, so no .jpg's, that is why .bmp's work well
You MUST have all non colored pixels be color 0 if that is your alpha transparency color in each image.

Attach a few of your images used in your pixel perfect collision
that we can look at.
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 13th Dec 2010 19:37
Thanks BillR. Having gone back to my bmps, I found that there was a strange blue background colour for the ship sprite. However having corrected this to black, the problem continues. I've attached my island sprite and the sprite which is a green blip which represents the ship (this is the animated sprite).

Also thinking about it, I'm not sure I want to be constrained entirely to bmps and non-rotated sprites.. Are there any other alternatives which might work?

Thanks for your help with this.
DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 13th Dec 2010 20:29
I got (almost) pixel perfect collision using the standard DB point command. Slower than memblocks, but still managed to get a fast 2d shooter running no problems on both my test rigs. I basically duplicate the sprites and collide-able scenery on to a hidden screen and colour each sprite etc a different colour. Then use the point command to check 4 or 6 points around your ship/player, checking for all the colours defined.
Still a distance check would be far faster for any circular or close to circular object, just not exact(Unless it is a sphere lol).

http://s6.bitefight.org/c.php?uid=103081
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 13th Dec 2010 21:52
Try this code, it uses your bmp files
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 14th Dec 2010 06:03
Quote: "Hodgey, thanks very much for your example, it runs perfectly for me and it's great to see the plugin working."


Glad to hear it!

Quote: "Also thinking about it, I'm not sure I want to be constrained entirely to bmps and non-rotated sprites.. "


Who said anything about not being able to rotate sprites with eXtends?

Here is an example of rotating sprites with extends. The up and down arrowkeys move and rotate the island.

A clever person solves a problem, a wise person avoids it - Albert Einstein
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 14th Dec 2010 09:40
Thanks for your continued help.

DVader, thanks for the thought. I certainly see how that could work but as Hodgey has pointed out that I can use eXtends with rotation and you say that the point technique is slower, I think I will have another go with eXtends first.

BillR, great example. I've tried your code and it works perfectly.

Hodgey, your example works great too. I've got some small islands which need rotation so I'm glad I can still use eXtends.

However I must confess I still don't see what I'm doing differently to these examples. That said, with so many good working examples, I'm sure I'll be able to figure it out eventually. I'll keep you posted on any progress. Do say if you have any brainwaves on what I might be doing wrong. If I can't figure it out soon, I'll post my code up here but as it's quite long and I'm not a great annotator... Well you know.
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 14th Dec 2010 11:36
Are you using the ,1 flag (load image "island.bmp",1,1 <---here)
when you load an image to say don't create a mipmap image?
Just a thought
DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 14th Dec 2010 14:14
Of course, you bought the addon and want to make use of it! Just giving an alternate option if all else fails. I couldn't say if using point is slower or faster than extends, as I have never used it, Memblocks are faster than point though, simply because it is faster to read the data from a memblock than using the point command. I will have to fish out an exe of a demo I made a while back lol, and see what peeps here get.

http://s6.bitefight.org/c.php?uid=103081
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 15th Dec 2010 00:11
Hi all. I think I've figured the solution to this. I was changing the size of the sprites using the 'size sprite' command. This seems to cause problems for the plugin leading to very strange results. However 'stretch sprite' seems to be fully compatible so I've just written a little function which takes the same parameters as 'size sprite' but which changes the size using the 'stretch sprite' command. Code is here if anyone else comes across this problem and wants it:



Thanks so much for your suggestions and examples. It was only through playing around with your working examples that I was able to figure out the problem.

Login to post a reply

Server time is: 2026-07-21 07:03:27
Your offset time is: 2026-07-21 07:03:27