Pixel Perfect Collision Detection Extension

  ... the case you feel the need for a new feature or want to submit one.

Pixel Perfect Collision Detection Extension

Postby makers-f » Mon Aug 06, 2012 3:44 pm

I finished developing an extension that allows for pixel-perfect collision.
  • It is fast (with a few collision it easily runs at 60FPS on my very low-end ZTE blade)
  • you set the threshold for alpha
  • it supports pixel perfect collision against whatever rectangle shape (that means that if you have a star as a pixelperfect sprite and a cube as a normal sprite, when the cube do not touch the effective star, the collision check will return false)
  • it has different tools for debug (a logger to see performance, an utility method to see the solid mask, ..)
  • it is really easy to use: just use PixelPerfectSprite and PixelPerfectAnimatedSprite instead of the normal ones

You can find the extension here
https://github.com/MakersF/AndEngineCollisionsExtension

and a project that tests and can be used as example here (it uses normal bitmap textures, but buildable textures are supported as well)
https://github.com/MakersF/CollisionTest
makers-f
 
Posts: 36
Joined: Sat Sep 17, 2011 6:11 pm

Re: Pixel Perfect Collision Detection Extension

Postby Mintir4 » Tue Aug 07, 2012 1:21 am

I got some errors.

Image

properties
Image

PixelPerfectTextureRegionFactory - lines 134 and 156 - getBitmapTextureFormat()
Image

PixelPerfectCollisionChecker - lines 25, 54, 55 and 58 - TransformationUtils
Image
Mintir4
 
Posts: 9
Joined: Mon Dec 19, 2011 5:35 am

Re: Pixel Perfect Collision Detection Extension

Postby makers-f » Tue Aug 07, 2012 10:48 am

Yes, as i mentioned in the readme it needs 2 little changes to andengine.
https://github.com/MakersF/AndEngineCol ... /README.md
(both just add a few lines, without any drastic change to our beloved engine ;) )

https://github.com/MakersF/AndEngine/co ... fb53f2a04d (the reason for the error in PixelPerfectTextureRegionFactory. Ps: ignore the change to .project)
This one is needed in order to load correctly the images if you use the buildable texture atlas (otherwise you would be forced to a single bitmap config, eg ARGB_8888, instead of automatically find the right one)
I don't really know why it wasn't already implemented, since as you can see it is implemented in bitmap texture atlas, and the buildable texture is just a wrapper around it.

https://github.com/MakersF/AndEngine/co ... 07ab25d31f (the reason for the other errors)
The second one adds a few methods for transformations.
invert: as the name says it inverts a matrices. It could be possible not to use it, but not using it would need the recalculation of many matrices, thus slowing the code down. In addition it is a useful method
What transformNormal (similarly to the function of C# XNA)do could be achieved by using transform of the vector and then subtracting the result of transform on a 0s vector, thus being slow and making code harder to follow.
As you can see, none of this method is strictly necessary, but without the change it would make the collision check code slower or harder to read.

I explained the reasons of the changes because i thought carefully if it was worth to modify the original andengine, and in addition i don't like to modify my libraries without an explanation(even very few lines), so i thought you'd appreciate the additional informations ;)
makers-f
 
Posts: 36
Joined: Sat Sep 17, 2011 6:11 pm

Re: Pixel Perfect Collision Detection Extension

Postby ctlayon » Thu Aug 09, 2012 3:15 am

Got it setup easily and am using this in my project! This is amazing, good work! :D
ctlayon
 
Posts: 155
Joined: Thu Jun 14, 2012 9:41 pm
Location: Champaign, Illinois

Re: Pixel Perfect Collision Detection Extension

Postby unionbeach » Fri Aug 17, 2012 3:26 pm

I also need to change:

import org.andengine.opengl.vbo.VertexBufferObject.DrawType;

for:

import org.andengine.opengl.vbo.DrawType;

Thank you, good work!
unionbeach
 
Posts: 4
Joined: Thu Aug 09, 2012 11:04 pm

Re: Pixel Perfect Collision Detection Extension

Postby makers-f » Sun Aug 19, 2012 12:12 pm

I checked on the github repository and as you stated DrawType is now a separated class.
Really strange, since i just pulled from the repository and in my local repository it is still inside VertexBufferObject..
makers-f
 
Posts: 36
Joined: Sat Sep 17, 2011 6:11 pm

Re: Pixel Perfect Collision Detection Extension

Postby ministeve412 » Mon Aug 20, 2012 1:00 am

I have your extension installed and have run your collision test project to see how it works. I then tried to get my own to work and for some reason it is not working. I'm wondering if your images were created differently from mine? Are you using any other properties or doing something different?
ministeve412
 
Posts: 15
Joined: Wed Nov 23, 2011 10:05 pm

Re: Pixel Perfect Collision Detection Extension

Postby unionbeach » Tue Aug 21, 2012 2:29 pm

Hi makers-f

I tested your extension with the last version of andengine, and compiled with the changes made to the engine.
When running the test, I see the two stars and the triangle, but does not show me the messages to collide, where should appear?, looking at the code, I do not understand where you see posts.

Thank You. :roll:
unionbeach
 
Posts: 4
Joined: Thu Aug 09, 2012 11:04 pm

Re: Pixel Perfect Collision Detection Extension

Postby makers-f » Tue Aug 21, 2012 2:35 pm

Solved my problems with git and now the extension is updated to the last AndEngine revision.

@ministeve412
Absolutely not, they are normal png. I remember you that you need to use a format that supports alpha channel in order to use this (for example png, and not jpeg).
Showing the code that create the sprite and the texture region can be helpful to understand the problem.

Btw, as a reminder to everyone, i already put in the source the class MaskUtils that can be useful in this cases: use the writeMaskToSDCard(IPixelPerfectMask pMask, String pRelativePathFromSDRoot, String pFileName) method in order to check if the generated mask is correct, and then track down if the problem is in the generation or in the collision code.

Btw, what do you mean with "not working"? Compile error? Crash? Wrong result when calling collidesWith?
makers-f
 
Posts: 36
Joined: Sat Sep 17, 2011 6:11 pm

Re: Pixel Perfect Collision Detection Extension

Postby ministeve412 » Wed Aug 22, 2012 2:02 pm

I'm using a png image and it is only checking collision around the bounding box of the sprite. I created the image in gimp so it should have an alpha threshold, but maybe I'm not using a correct value? Also, I'm not using any code that obtains the bitmapmask, is that neccesary to check for collision within the bounding box of the sprite? If so, could you include an example of how to do that? I don't remember seeing any in your collision test project and that worked fine without it, so I assumed its not needed.
ministeve412
 
Posts: 15
Joined: Wed Nov 23, 2011 10:05 pm

Next

Return to Features

Who is online

Users browsing this forum: No registered users and 5 guests