How can I generate a collider for a procedurally generated sprite?

Hey all. I couldn’t find any solutions to my problem from other people asking about this, so I figured I’d take a stab at asking myself.

I’m working on a 2D game with randomly generated voxel terrain called Fissure. I’m taking inspiration from Noita more specifically and having smooth terrain made up of “materials” rather than blocky, tile-based terrain like Terraria or Core Keeper. After some finagling, I got the chunks to generate and draw with relative ease.9424676--1321028--upload_2023-10-22_18-5-12.png

However, my problem lies in the fact that the terrain actually has no collision. I’d like to use a polygon collider rather than an edge collider so I can click inside the terrain to break it. What I need to do is calculate the outline of the non-empty space of each chunk and create points on a new PolygonCollider in order to create a shape that’s as accurate as possible. How can I do that?

So what exactly is the problem though? You don’t know how to use a PolygonCollider2D? You don’t know how to create the path (outline) that it wants? If it’s about the collider, there’s plenty of info on how to use it online but if it’s how to calculate the outline then you’re in the best position to know how to turn your “voxel” data into an outline.

Maybe you can ask a more specific question here.

You’re absolutely right, I edited to original post to clarify a bit. I’m trying to calculate the edges of the non-empty space (the terrain) and generate a new PolygonCollider with the proper points so I can have geometry that’s about as accurate as possible. Generating a “sprite collider” at runtime, essentially.

I’m not seeing any clarification here. Yes, you want to set up a PolygonCollider2D but again, what part don’t you understand? You have the “voxel” data, what part do you need from anyone helping you? Clearly we don’t know anything at all about your “voxel” data.

When asking for help, you need to be precise about what specific things you do and don’t understand and to not make assumptions that others know or can see your data, code or anything else.

I know everything there is to know about the PolygonCollider2D, I wrote it but I’m in the dark here on how to help you.

Sorry for the repeated confusion, having trouble putting it into words haha. I have the PolygonCollider2D set up, but as you can see in the image below, it’s a bit too “expanded” for accurate enough collision. Additionally, some gaps (like in the top left) are filled in when they should be counted. Is there a reliable way to shrink the edges to better fit the geometry?

9426032--1321259--upload_2023-10-23_10-50-38.png

Right now, the code is simple as:

public void CreateCollider()
        {
            PolygonCollider2D col = gameObject.AddComponent<PolygonCollider2D>();
        }

You should generate accurate physics shape outlines when you generate the sprites. You have all this data to generate sprites, also add physics shape data to the sprites.

It’ll then use this outline. Without it, it’ll only create a rough approximation of an outline for subsequent editing.