# Dynamically Change Sprite Body/Polygon (Worms - like game)

## Recommended Posts

Hey everyone,

I am making a simple Worms-Armageddon-like game for educational purposes in my University course. I have a problem figuring out how to implement the following with Phaser.

THE SETUP:

1.) Standard map with loaded Polygon from PhysicsEditor - image 1. I am using P2 physics but can change to Box2D if necessary.

2.) Making a hole with some weapon - image 2.

THE PROBLEM:

I believe I have an idea how to make the visual hole in the sprite, by using this example http://phaser.io/examples/v2/bitmapdata/alpha-mask (correct me if I am wrong or if there is a better method)

I have no idea, though, how to change the body of the map by using Phaser - as in image 2. I would like to avoid doing it "by hand" with per pixel manipulation and making a new polygon manually every time.

Is there an "automatic" way of calculating the polygon of a sprite by using its non-transparent pixels?

Could you please help  The code can be seen here and I will share the project once it is done.

##### Share on other sites

To do this, I would probably walk around the edges of the visible pixels and create a point at the edge of each.  This would create a very detailed polygon that perfectly matches the pixels.  Then I would use Ramer–Douglas–Peucker algorithm to simplify the polygon.

Honestly, though, I wouldn't do any of that.  To do a Worms game, I probably wouldn't use polygons at all.  I would probably represent the collidable parts of the level and sprites as simple bitmasks.  You could then use bitwise operators to detect collisions and to cause terrain destruction.

##### Share on other sites
47 minutes ago, fillmoreb said:

To do this, I would probably walk around the edges of the visible pixels and create a point at the edge of each.  This would create a very detailed polygon that perfectly matches the pixels.  Then I would use Ramer–Douglas–Peucker algorithm to simplify the polygon.

Honestly, though, I wouldn't do any of that.  To do a Worms game, I probably wouldn't use polygons at all.  I would probably represent the collidable parts of the level and sprites as simple bitmasks.  You could then use bitwise operators to detect collisions and to cause terrain destruction.

Thanks fillmoreb. I will think about and assimilate your suggested approach.

##### Share on other sites
On 3/28/2016 at 7:56 PM, fillmoreb said:

To do this, I would probably walk around the edges of the visible pixels and create a point at the edge of each.  This would create a very detailed polygon that perfectly matches the pixels.  Then I would use Ramer–Douglas–Peucker algorithm to simplify the polygon.

Honestly, though, I wouldn't do any of that.  To do a Worms game, I probably wouldn't use polygons at all.  I would probably represent the collidable parts of the level and sprites as simple bitmasks.  You could then use bitwise operators to detect collisions and to cause terrain destruction.

So after a bit of reading I think I get the idea. The problem I have is how to start, meaning how to do this: " represent the collidable parts (...) as simple bitmasks" with Phaser. Does it involve CollisionGroups? The only example I could find was with actually setting the body to either a circle or rectangle...

##### Share on other sites

Phaser's physics doesn't support bitmasks as the collidable body.  If you choose to use them, you would most likely have to ignore Phaser's physics and create your own physics engine.  If you're more comfortable working within the system that Phaser has set up, you probably want to stick with polygons.  Once you've got it figured out they'll work just fine for you.  Just choose whichever method feels like the best fit for you.  I prefer bitmasks, but that's probably because I'm an old-school programmer.

Here's a link to a worms clone that was written for HTML5.  It's not phaser, but it's using polygons.  You can go and check the method they are using for terrain deformation to help you get going.

##### Share on other sites
1 hour ago, fillmoreb said:

Phaser's physics doesn't support bitmasks as the collidable body.  If you choose to use them, you would most likely have to ignore Phaser's physics and create your own physics engine.  If you're more comfortable working within the system that Phaser has set up, you probably want to stick with polygons.  Once you've got it figured out they'll work just fine for you.  Just choose whichever method feels like the best fit for you.  I prefer bitmasks, but that's probably because I'm an old-school programmer.

Here's a link to a worms clone that was written for HTML5.  It's not phaser, but it's using polygons.  You can go and check the method they are using for terrain deformation to help you get going.

Thanks fillmoreb, once again! I will have a look at your link. I was getting confused that it was somehow incorporated in Phaser and I have missed it. I will try with polygons not to overcomplicate things for my course

## 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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.