Colliders for 3D sprites in Godot

While I work on a post-jam update for our game The Culling I thought I'd share how we did the colliders for our sprites. It was the first thing I did and it's quite simple thanks to Godot's built-in helpers but it does involve a few things. Hopefully someone finds this useful!

Unfortunately you can't have pixel perfect collision out-of-the-box, but you can get a pretty close approximation by generating a polygon with the Bitmap class based on your image (it uses the Ramer-Douglas-Peucker algorithm under the hood).

```python

have some image with transparency

var tex : Texture2D = load("res://path/to/texture.png")

create a bitmap instance and load the texture into it

var bitmap := BitMap.new() bitmap.createfromimagealpha(tex.getimage())

create a rectangle based on our texture size

var rect := Rect2i(Vector2.ZERO,tex.get_size())

generate our polygon based on the rect with a precision of 5

the lower you go the more vertices you'll get

I take the first element from the returned list here

since all of our sprites have a single island

var polygon := bitmap.opaquetopolygons(rect, 5)[0]

let's transform the polygon from pixel-space to world-space

we need to match the pixel-size property from our Sprite3D

var pixel_size := 0.01

and transform all vertices to the center and flip them

for i in polygon.size(): polygon[i] = (polygon[i] - rect.size * 0.5) * -pixel_size ```

Later you can assign this polygon to the polygon property of a CollisionPolygon3D added to an Area3D or PhysicsBody on your Sprite3D. I didn't do it here since we generate these polygons once at launch and reuse them a lot for spawned targets.

Don't forget to enable Debug / Visible Collision Shapes in Godot to see the colliders.

Below are some examples of the generated colliders from our game.

screenshot1713453220.80545.png screenshot.png screenshot1713453245.56212.png

The Culling