Pixel perfect collision on animated spritesheet

Recommended Posts

I have a sprite of a character moving and I'm trying to apply physics collide on it that when it hits a rock for example health is decreased.

I managed to achieve collision but as rectangular shape of the image, what I want to achieve is the collision happens on the boundaries of the character itself.

I used PhysicsEditor to generate the JSON file, but I'm still not sure how to use it to achieve pixel perfect collision.

Any thoughts?

Share this post

Link to post
Share on other sites

First up you need to apply the P2 physics engine to the body. Then apply the shape you made with the Physics editor to the body.

In one of my games I do something similar with player grenades so they can roll and bounce around in a believable manner. The code snippet looks like:

Loading the shape file exported from Physics Editor

		this.game.load.physics("objectPhysics", "img/weapons/objects_physics.json");


Applying the shape to the sprite

                myGame.phaser.physics.p2.enable(playerGrenade, false);

                // Add our PhysicsEditor bounding shape
                playerGrenade.body.loadPolygon("objectPhysics", "Grenade", playerGrenade);


Share this post

Link to post
Share on other sites

I did that, but my problem is that my sprite has animation so each frame has a different polygon thus collision detection should apply to the current frame in the animation.

What I did is that I loaded all the polygons to the sprite body then with each frame change I disable and enable the polygons that correspond to that frame.

But this is a very tedious and extensive solution, and I'm wondering if there's a better solution or if I'm missing something

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.