0 votes

I have been trying to figure out a way to do sprite overlap detection in Godot 3.

Answers to a previous question of mine recommended that I content myself with bounding rectangles or polygons. Stubbornly, I am still pursuing the dream of sprite overlap detection.

I was spoiled by Pygame, which has a simple method to create masks from sprites, and then check them for overlap using a collide_mask() method.

Godot doesn't have anything like this built in, but it should still be possible to do it in the engine.

I discovered that Textures have a get_data() method which returns an Image. Further, Images have a get_pixel()method which returns the color data of each pixel in the image. Here is a link to the documentation on Images.

I was thinking that it should be possible to use this data to construct a mask, whose coordinates can then be translated to world space. Once we have this, we can easily test if two masks overlap. And voila, sprite overlap detection!

The problem is that I don't know how to implement this.

Any advice?

in Engine by (1,549 points)

1 Answer

0 votes

I'm very new to godot 3

I've just been reading through the docs and I think what you are asking is described in the physics documentation. Collision Layers and Masks


  • This describes the layers that the object appears in. By default, all bodies are on layer 1.

- This describes what layers the body will scan for collisions. If an object isn’t in one of the mask layers, the body will ignore it. By default, all bodies scan layer 1.

by (78 points)

Thanks for the response. Those docs deal primarily with CollisionObject2D and the nodes that inherit from it. Unfortunately, Sprites and Textures do not inherit from this node and so don't have the same type of in-built collision detection resources. The type of "mask" I'm talking about in my question isn't the same as what Godot calls a "mask" in that article.

Oh, sorry couldn't be of help then.

Can you not wrap the texture or sprite in a KinematicBody2D node then use the move_and_collide ( Vector2 rel_vec ) method. see KinematicBody2D

KinematicCollision2D moveandcollide ( Vector2 relvec )
Moves the body along the vector rel
vec. The body will stop if it collides. Returns a KinematicCollision2D, which contains information about the collision.

That method would use the KinematicBody2D's collision shape to determine collision, not the child Sprite. There is currently no in-built way to detect Sprite collision in Godot, so I'm fairly sure you have to use the image data in a way similar to what I suggest.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.