Page 1 of 1

[SOLVED]How to make destructive terrain like worm?

Posted: Sun Jun 16, 2019 1:28 am
by AdrianN
Hi all, I have a question, about how to make a destructive environment like worms games in love2d.
Is possible create that functionality in Love2d, using box2d?

I've been doing some research and I found some examples:

https://www.reddit.com/r/love2d/comment ... C3%B6ve2d/
http://antonior-software.blogspot.com/p ... worms.html

I thought to use box2d chain-shape to make the terrain and use a library like clipper to modified the shape.
If someone has better information or understanding, I would be grateful

https://github.com/lzubiaur/clipper-lua
https://luapower.com/clipper

Sorry for my bad english.

Re: How to make destructive terrain like worm?

Posted: Sun Jun 16, 2019 3:26 am
by raidho36
If you need box2d then it's a fair bit more difficult, but if you only need the degree of physics like in worms then you could use the same technique: bitmap. The map graphics IS your physics object, when you check collisions against terrain you check for non-transparent pixels in the image. Given how modern computers work, it would help to have the main bitmap in RAM, and a texture of it in VRAM. When you need to destroy a chunk of terrain, you simply replace appropriate pixels with transparency and upload this bitmap to VRAM to render - presto!

If you want to use box2d collision mesh, then you'll have to generate one. You can use some library that can do boolean operations on volumes (flat surfaces in case of 2d) to generate new geometry, so when you'd want to remove a chunk of terrain you'd boolean subtract the shape of the damage from terrain, and that would leave a cut. Or you could use the same bitmap technique, but also generate chain shape of its surface every time the bitmap is modified. You can do this by finding a solid point on the bitmap and following along it and creating new edges on each bend. You'll have find a way to do this for every surface, i.e. disjoint chunks of terrain and holes in the bulk of it. Finally, you can probably do just with the chain shapes alone, if your boolean geometry library supports working on just outline splines rather than filled geometry.

Re: How to make destructive terrain like worm?

Posted: Sun Jun 16, 2019 4:16 am
by AdrianN
raidho36 wrote: Sun Jun 16, 2019 3:26 am If you need box2d then it's a fair bit more difficult, but if you only need the degree of physics like in worms then you could use the same technique: bitmap. The map graphics IS your physics object, when you check collisions against terrain you check for non-transparent pixels in the image. Given how modern computers work, it would help to have the main bitmap in RAM, and a texture of it in VRAM. When you need to destroy a chunk of terrain, you simply replace appropriate pixels with transparency and upload this bitmap to VRAM to render - presto!

If you want to use box2d collision mesh, then you'll have to generate one. You can use some library that can do boolean operations on volumes (flat surfaces in case of 2d) to generate new geometry, so when you'd want to remove a chunk of terrain you'd boolean subtract the shape of the damage from terrain, and that would leave a cut. Or you could use the same bitmap technique, but also generate chain shape of its surface every time the bitmap is modified. You can do this by finding a solid point on the bitmap and following along it and creating new edges on each bend. You'll have find a way to do this for every surface, i.e. disjoint chunks of terrain and holes in the bulk of it. Finally, you can probably do just with the chain shapes alone, if your boolean geometry library supports working on just outline splines rather than filled geometry.
I've been thinking about using pixel collision, but in my current game I use box2d to manage the physics of my characters.
I'll need to know how clipper works or use another library for Boolean operations with polygons,

Combining bitmap and chain shape also sounds simpler, but I would have to generate it every time a collision is generated, in that case a number of edge-shapes could be more easy to modified.

Thanks for your reply.

Re: How to make destructive terrain like worm?

Posted: Sun Jun 16, 2019 6:11 am
by raidho36
Sounds like "clipper" can do exactly what's needed there - it's specifically a boolean geometry operations library. I guess the way you work with it is that you create your terrain shape and keep that, and create destruction shapes as appropriate and use them to cut away at the terrain shape. If you have textured polygons to render graphics then you can use this mesh for rendering. If you'd rather use a freely editable texture, then you'd need to render destruction into it, with the same shape and area.

Re: How to make destructive terrain like worm?

Posted: Sun Jun 16, 2019 9:24 pm
by AdrianN
raidho36 wrote: Sun Jun 16, 2019 6:11 am Sounds like "clipper" can do exactly what's needed there - it's specifically a boolean geometry operations library. I guess the way you work with it is that you create your terrain shape and keep that, and create destruction shapes as appropriate and use them to cut away at the terrain shape. If you have textured polygons to render graphics then you can use this mesh for rendering. If you'd rather use a freely editable texture, then you'd need to render destruction into it, with the same shape and area.
Sure, searching in the web I found another lua library for clipping polygon . I think I'll use it because clipper is very complicated
I've already done some tests and it works well.

https://github.com/AlexarJING/polygon

Yeah, I'm going to use mesh to draw textures in the polygon

https://love2d.org/wiki/love.graphics.newMesh