简体   繁体   中英

How do you do pixel perfect Collisions in GODOT ENGINE

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?

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.

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM