Creating polygons with smooth curves

Godot Version

4.5.1

Question

Hi, everyone. I’m trying to recreate these shapes using 2D polygons with collisions for a test project I’m working on. The problem is that I can’t seem to create polygons with curves smooth enough to be useful, since I need them to be identical to the ones in the image so that a ball can roll smoothly over them.

I tried replicating them with path2d and a script, but it doesn’t quite replicate them faithfully to the original.

I tried using the SmartShape2D add-on, but I didn’t get a usable result either.

I tried looking for tutorials or examples of curves, but in every case they are larger polygons with less curvature, like platforms or objects.

So my question is, what do you consider to be the best way to replicate a shape like this in a polygon to which I can assign collisions?

I should mention that I’m fairly new to video game development, so if the answer is very obvious, I apologize in advance.

Thank you.

Interesting problem! It’s probably not an easy to solve, but I might be wrong.

I can think of one way, but I’m not sure if that’s the best way or if it actually works.

For each curve, you create a StaticBody2D, assuming that these curves won’t move.

Then you need to add multiple CollisionShape2D as a child node to this StaticBody2D.

Each CollisionShape2D should have a SegmentShape2D as a shape. So it’s basically a line. Since a curve is basically multiple straight lines next to each other…

The tricky part is, you need to create the curve from these lines. You need to create this by script - like creating a circle, depending on the radius, and the angle (that shows the curved part of the circle).

I’m also not sure how having very small line segments works with physics.. So it may not work how you want, or it can be too slow.

I think your best bet is to load your image as a texture to Sprite2D and use Create CollisionPolygon2D Sibling option in the Sprite2D submenu of the 2D view.

This’ll create multiple CollisionPolygon2D nodes to match your image as best as it can. It’s not perfect, it’s just an approximation of the shape, but it should do the job for most cases.

4 Likes

This is the right way to do it!

I had to manually arrange the nodes to form a perfect curve, but the polygons were generated quite accurately with the correct settings.

Thank you!

2 Likes