PieKing1215/FallingSandSurvival: 2D survival game inspired by Noita and slightly Terraria (github.com)
The game has to generate a mesh for the rigidbody, which takes a few steps:
First, the image is run through an algorithm called "Marching Squares"-This algorithm basically takes in an image and spits out one or more outlines depending on if there are holes in the shape.
In the end, the physics engine (in this case Box2D) wants the mesh to be a bunch of triangles, so you need to use a triangulation algorithm to turn the outline into triangles. In this case, I'm using the PolyPartition library.
Consider the result you get by directly plugging the result of Marching Squares into PolyPartition:
Notice how the result has a very unnecessarily large amount of triangles (284!).
This is because the output of Marching Squares is generally pixel-perfect, so you get a lot of 90 degree angles.
Having this many tris is bad for performance, so before converting the mesh into triangles, we need to simplify the mesh.
These triangles can now be used to create a RigidBody in Box2D.
I am using something called the Douglas-Peucker algorithm, which takes a mesh and "smooths it out".
Here's the result of doing Marching Squares -> Douglas-Peucker -> PolyPartition:
Comments