AboulEinein

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.clearShapes();
                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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.