The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

I would like to programatically create a simple polygon based on a sprite. I am doing:

var bm =
var rect = Rect2(position.x, position.y, texture.get_width(), texture.get_height())
var my_array = bm.opaque_to_polygons(rect)

but the received my_array is empty. I confirmed that the bitmap itself is not empty (146/396 bits on). I tried it with a two images (including default godot.png that comes with the engine), yet the array is always empty.

Any idea why it may be the case?

Similar questions: 1, 2.

Godot version 3.1.1-stable
in Engine by (143 points)

1 Answer

+2 votes
Best answer

I think you should not use the position when creating your rect. Because the position will not be 0,0, the rect won't represent your image size.

So, if you try with:

var bm =
var rect = Rect2(0, 0, texture.get_width(), texture.get_height())
var my_array = bm.opaque_to_polygons(rect)

my_array won't be empty. You can then use the sprite position and add it to your points if you want to draw your polygon at the sprite location and not centered on (0, 0).

EDIT: Just to make the reply more complete, here's how you would add the polygon based on the points

# Create the polygon
var poly =
var pointArr = PoolVector2Array()
for p in my_array[0]:
poly.polygon = pointArr
poly.texture = load("res://sometexture.png")


# Place the polygon at sprite location
# We need to offset by width and height because sprite position is from the center, whereas the polygon position is from the top left
poly.position = $Sprite.position - Vector2($Sprite.texture.get_width()/2, $Sprite.texture.get_height()/2)
by (643 points)
edited by

Geez, thanks so much! I spend hours trying to figure out what's going on.

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 Frequently asked questions and 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 [email protected] with your username.