Yuri
Yuri

Reputation: 209

box2d. platformer. destructible terrain

So, I want to create a platformer with destructible terrain. I use a physics engine box2d for java. I have an idea to do a lot of small static balls that will be destroyed at some impact on them. Are there better ideas? Not much this will slow down performance? enter image description here

And the second question. If I do so, how can fill this area picture? Something similar to the style of worms.

enter image description here

Upvotes: 2

Views: 1208

Answers (2)

Felix K.
Felix K.

Reputation: 6281

Beside the fact that your idea should also work it might have a lack of resolution which makes me prefer this method.

Upvotes: 1

FuzzyBunnySlippers
FuzzyBunnySlippers

Reputation: 3397

I had a similar idea and was going to try it out in the next couple of weeks. I was going to make a "castle vs castle" shooting game for my kids. Here was the solution I was going to do:

  1. Create a static U-shaped box that stretches across the bottom of the terrain and has two sides about half way up the side of the screen.
  2. Use lots of small balls, each with a fixed radius (nominally 0.1m, the smallest size you can use in Box2d).
  3. Execute lots of iterations of the physics without updating the screen until the end to let it settle, or check for everything sleeping.

Option #1 for terrain: Divide the screen area up into vertical boxes, a few pixels across each. They would stretch from the bottom to the top of the screen and divide it into bins. Then calculate the AABB (in box2d coordinates) for each bin and then use the AABB query in box2d to find all the bodies in that box. Take the max Y position of the bodies found and use that (+radius) to find the highest point in that bin. Connect all the highest points in all the bins and that will give you a continuous line you can draw, etc.

Option #2 for terrain: Create a long chain of 0.2m long x 0.1m high rectangles, connected on each end with rotating joints. Lay the chain over the top of the terrain from the top. Now characters can walk over it.

In either Option #1 or Option #2, when a "bomb" hits, destroy a large number of the balls (or whatever polygon you use) near the bomb center and the physics should make them drop.

I will of course try this out myself to see if I can get it to work.

UPDATE Well...sometimes...it seems like a good idea in your head, but in actuality...not so much. I did make a demonstration of dropping lots of different sized balls to create a "random" terrain. The terrain is not particularly exciting. I also used an AABB query to remove balls where the screen got touched. This works...but is also not particularly exciting. So, a good basic "get stuff running in box2d" demonstration, but probably not a good answer to this question. FWIW, the code is posted on github here.

Upvotes: 0

Related Questions