2

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?

Theraot
  • 31,890
  • 5
  • 57
  • 86

2 Answers2

2

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.

  • One reason is because I want sword to sword collisions. Imagine how disenchanting it would be to the player to collide with the air above a sword sticking out from the enemy when you should of had to continue your swing, then hit the head. In general I don't want to mandate that all the poses each frame be in a general shape. I am using a actor -> rotoscope animation -> sprite sheet. Thus I want large eccentric theatrical movements and not character limitation to a capsule or primitive frame. Can you direct me to code for the none built in perfect collision methods.? – Have Mercy Jun 21 '21 at 14:33
  • Actually is it possible to just write python code and run it in godot? Like I use pygame to manage collisions and python for AI then Godot for front end and porting haha – Have Mercy Jun 21 '21 at 16:27
  • @HaveMercy Although stylistically similar to Python, GDScript is not based on Godot so, you can't just run python code into Godot. I don't have any code for the two systems I mentioned at the ready but if you search for some flood-filling or border detection algorithms you should be able to code the first one I mentioned pretty easily. You'd just need to save the collider data as resources which is easy in Godot. – Tecelli Akıntuğ Jun 21 '21 at 19:02
  • Ok. I will use a python script to generate a godat script which will load the verticies offset from pygame into a vector 2's in godot for the polygon collider data member. Ill keep you posted. Thanks for the help – Have Mercy Jun 22 '21 at 02:31
2

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.

Steve Smith
  • 2,244
  • 2
  • 18
  • 22