How do you do pixel perfect Collisions in GODOT ENGINE

2.9k Views Asked by At

In Godot, from what I can see, you must hand draw polygon colliders or use a primitive shape like a capsule. I need to do pixel perfect collisions with hundreds of frames on a sprite sheet. Thus I need to be able to have the bit mask of the sprite given as the collider shape. Like you can do in pygame. How do you do that in GODOT?

2

There are 2 best solutions below

0
On

A brief answer would be:-

  • Take each sprite and create a BitMap (https://docs.godotengine.org/en/stable/classes/class_bitmap.html) from it is using Bitmap.create_from_image_alpha().
  • Then use BitMap.opaque_to_polygons() to generate an almost pixel-perfect collision shape.
  • Add a CollisionPolygon2D to your Node (e.g. KinematicBody2D).
  • Set the collision shape as the shape of the CollisionPolygon2D.

It obviously takes a bit of code to do this, but this is the general idea that I've used before.

4
On

You have two main options for pixel perfect collisions in Godot (neither of which are built in):

  • You can write a system that can create polygon colliders from your image. Thus, just using polygon colliders in runtime.
  • You can write a new collision system that can use aabb collision for broad phase and image data for precise phase.

However, 99% of the time pixel perfect collision is not required and not efficient. So, if you could give us more information on why you want to have pixel perfect collision, then we may be able to help you more.