Coder Social home page Coder Social logo

andengine-pixel-perfect's Introduction

Pixel Perfect Collision Detection

Sloppy collisions are boring. This library extends AndEngine's Sprite and AnimatedSprite with pixel-perfect collision detection.

Note: this will only work for collisions between unrotated, unscaled sprites.

Installation

  • Save the jar file to your project's libs directory.
  • If you are using eclipse, right-click on the jar, and click on build path > Add To Build Path
  • Done!

Note: If you are using a version of AndEngine built before July 2011 (ie. you're using TextureRegions instead of BitmapTextureAliases), use this jar instead.

Usage

PixelPerfectSprites work just like regular sprites, but are initialized with PixelPerfectTextureRegions, which are created by the PixelPerfectTextureRegionFactory.

Your code should look something like this:

public class Game extends BaseGameActivity{
    private BitmapTextureAtlas texture;
    private PixelPerfectTextureRegion diamondRegion;
    private PixelPerfectTiledTextureRegion triangleRegion;
    ...

    public void onLoadResources(){
        PixelPerfectTextureRegionFactory.setAssetBasePath("gfx/");
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); // if you're using non-pp bitmaps
        ...
      
        texture = new BitmapTextureAtlas(256, 64, TextureOptions.NEAREST_PREMULTIPLYALPHA);
        diamondRegion = PixelPerfectTextureRegionFactory.createFromAsset(texture, this, "diamond.png", 0,0);
        triangleRegion = PixelPerfectTextureRegionFactory.createTiledFromAsset(texture, this, "triangle.png", 65,0);

        getEngine().getTextureManager().loadTextures(texture, ...);
    }
     
    public void onLoadScene(){
        ...
        PixelPerfectSprite diamond = new PixelPerfectSprite(0,0,ppRegion);
        scene.getTopLayer().addEntity(diamond); 

        PixelPerfectSprite spinningTriangle = new PixelPerfectSprite(100,100,ppRegion);
        spinningTriangle.animate(20, true);
        scene.getTopLayer().addEntity(spinningTriangle); 

        spinningTriangle.collidesWith(diamond);
    }
} 

For a more complete example, check out the demo

Technical Notes

The alpha channel of each sprite's bitmap is packed into a 1-bit-per-pixel int[][] mask. If the bounding boxes of the sprites intersect, logical shifts and &'s are used to test the intersection for a pixel-level collision.

The running time of the collision algorithm is proportional to the area of the intersection of the bounding boxes of the sprites being tested. 1000 collision checks against a 100x100 pixel intersection ran in 200ms on my HTC Incredible. At that rate, it shouldn't be any sort of bottleneck in your game. However, my benchmark was crude and my game required only a fraction of that speed, so YMMV.

Contributors

Contact

If you have any questions or find any bugs, let me know.

[email protected]

andengine-pixel-perfect's People

Contributors

alximik00 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.