Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in '2D' started by Hypogriffh, Oct 23, 2023.

  1. Hypogriffh

    Hypogriffh

    Joined:
    Nov 26, 2022
    Posts:
    3
    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. 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?
     
    Last edited: Oct 23, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,082
    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.
     
  3. Hypogriffh

    Hypogriffh

    Joined:
    Nov 26, 2022
    Posts:
    3
    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.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,082
    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.
     
  5. Hypogriffh

    Hypogriffh

    Joined:
    Nov 26, 2022
    Posts:
    3
    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?

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

    Right now, the code is simple as:

    Code (CSharp):
    1. public void CreateCollider()
    2.         {
    3.             PolygonCollider2D col = gameObject.AddComponent<PolygonCollider2D>();
    4.         }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,082
    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.

    https://docs.unity3d.com/ScriptReference/Sprite.OverridePhysicsShape.html

    It’ll then use this outline. Without it, it’ll only create a rough approximation of an outline for subsequent editing.
     
    Hypogriffh and Kurt-Dekker like this.