Reputation: 209
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?
And the second question. If I do so, how can fill this area picture? Something similar to the style of worms.
Upvotes: 2
Views: 1208
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
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:
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