0 votes

I asked a similar question some time ago to no avail, but how do I resize a CollisionPolygon2d? I've heard you can't use scale, and I don't actually want to. I'm doubling the size of all my sprites (outside of godot, the new imported textures are doubled) and I want to just resize all of the collision bodies.

Is there a way to double every element in the PoolVector2Array or something like that? Any built in function or plugin?

in Engine by (251 points)

2 Answers

0 votes

each polygon shape has a coordinate for each point, you can see them on the inspector, just change the position of the points to make the polygon bigger or smaller you can see them by selecting the polygon shape and clicking on the text that says PoolVector2Array on the inspector

by (428 points)

I know of this option, I was hoping for something automated. I have about 40 collision shapes and each shape has about 50 points.

0 votes

I found your question because I wanted to dynamically scale my CollisionPolygon2D at runtime. After being disappointed that there was no "easy" solution I could find, I thought about it for a while and realized that resizing a CollisionPolygon2D via script is a relatively simple process under the following conditions:

  • You want to uniformly scale each vertex.
  • The scale is relative to the origin (0,0) of the polygon.

Here's what I came up with:

# how much to scale each vertex by
var scaleAmount : float = 2
# reference to the PoolVector2Array on the CollisionPolygon2D
var polygon : PoolVector2Array = $CollisionPolygon2D.polygon

# scale each vertex
for i in polygon.size():
    polygon.set(i, polygon[i] * scaleAmount)

# assign the newly scaled vertices to the CollisionPolygon2D
$CollisionPolygon2D.polygon = polygon

I'm inexperienced with Godot and GDScript, so there are probably better ways to do it. Nonetheless, the above code could probably be modified to work as an editor tool.

by (14 points)
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.