Convert a Texture's pixels into a CollisionPolygon2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pmorim

I am trying to create an advanced Falling-Sand simulation (like Noita) in Godot with GDScript. I’m waiting on Godot 4.0’s GDExtension to rebuild this demo with Rust, but until then I’m using GDScript and slowly learning how to use this engine.

As I expected from the start, I quickly outgrew the built-in Tilemap and its capabilities, so I decided to use a Texture to draw the pixels and create a custom Grid system.

But for this game to be any fun, a Player KinematicBody2D (and other bodies) needs to be able to collide with this pixel-simulated world. Thus, I’m scratching my head around the idea of somehow converting the pixel “areas” into a CollisionPolygon2D. But how would I do that?

I didn’t find any solutions to this online, so I asked OpenAI’s ChatGPT about this and it told me to use the generate_sdf() and convex_decompose() methods of the SDF class, but I can’t seem to find any reference to an SDF class in the documentation.

How would you approach this problem?
Is this task even feasible?
Would it even be performant (with multi-threaded chunks)?
Should I consider building my Grid system in another way?
Or would it be simpler to just build my project in another (perhaps lower-level) engine?


:bust_in_silhouette: Reply From: SteveSmith

I would use a BitMap ( BitMap — Godot Engine (stable) documentation in English ). Create the BitMap using BitMap.create_from_image_alpha(), and then BitMap.opaque_to_polygons() to create polygons, which can then be added to a CollisionPolygon2D().

Thank you, this solution seems interesting. I’ll give it a try and experiment with its performance.

pmorim | 2023-01-04 20:59