Jump to content

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?

Link to comment
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);


Link to comment
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

Link to comment
Share on other sites

  • 1 year later...

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...