0 votes

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: https://ibb.co/NtTnHNf

image of the shapes

asked Jun 16 in Engine by peterhoglund (38 points)

2 Answers

0 votes

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

answered Jun 16 by jgodfrey (5,706 points)

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 :)

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?

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

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 :)

0 votes

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.

answered Jun 17 by haydenv (20 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.