Can I set an exact size for my collision shape?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By peterhoglund

I have a StaticBody2D with a Sprite and a CollisionShape2D. However, I can’t get them to match in size. When I drag the handles or set the bounds the size always gets doubled/mirrored which means I can’t get it to be an uneven size. Can I set the collision shape to be more exact?

Image link: image hosted at ImgBB — ImgBB

image of the shapes

:bust_in_silhouette: Reply From: jgodfrey

Yes, you can.

Not sure what Shape you’re using, but for instance, with a Rectangle… If you click click the Rectangle in the Shape field in the inspector, it’ll open an Extents panel where you can directly supply the x,y values…

Yes, it is a Rectangle shape, but if I change the Extends Y from, for example, 3 to 4 it will add 1 both to the bottom and to the top. If I change it to 3.5 it will be offset with 0.5. Is the only way to make it be aligned with the Sprite to change the size to 3.5 and then change the Y Position with 0.5? Feels like it would be easier if there would be a width and a height for the Rect, but perhaps that have drawbacks that I don’t understand :slight_smile:

peterhoglund | 2020-06-16 19:35

Ah, sorry - guess I misunderstood your question. The CollisionShape2D object has a typical Transform property that should allow you to shift the whole shape around. Is that what you want?

jgodfrey | 2020-06-16 20:17

In addition to the Transform coords mentioned above, you can also simply drag the CollisionShape2D around to better “center” it on your sprite.

But, I don’t know of any way to modify the position of a single edge of the shape and I’m not sure what’s behind that design decision…

jgodfrey | 2020-06-16 21:36

Yes, I want to center it on the sprite. I think the problem is that the sprite size is from topleft corner and set with a width and height, and the collider is from the center and set with a radius from the center. so to match them if the size of the sprite is uneven I have to set the collider size to X.5 and then bump the position with .5

A bit weird but I suppose it works :slight_smile:

peterhoglund | 2020-06-17 13:50

1 Like
:bust_in_silhouette: Reply From: haydenv

One option may be to use a CollisionPolygon2D instead of CollisionShape2D.

After instancing a CollisionPolygon2D node in a scene it is possible (by using a series of left clicks in different positions) to create a polygon that better matches the sprite than a rectangle could.

If the point-and-click creation of a polygon is still not precise enough, then the creation of the polygon can be done in the inspector by using exact (x,y) coordinates: Find in the inspector the “Polygon” property of the CollisionPolygon2D node. Then set the “size” to the number of vertices you expect your polygon to have. Then, directly below “size” you can set the (x,y) coordinates of each individual vertex. This allows precise creation of the collision polygon.