Making a Path2D curve square

Godot Version

4.2.1

Question

I’m trying to make paths for my NPC’s, and would like to make them more square instead of the standard curve. How could I accomplish this? The red is how I would want the paths to be.

The in-editor view isn’t strictly accurate to the path you’ll actually get by sampling the curve, so take it with a grain of salt and verify the actual output. There are some important caveats with the BakeInterval, however, and just fundamentally with how bezier curves work.

Example: you can see here the sampled position is at the corner, despite the editor preview showing significant deviation of the path.

Out of the box, you have a few options:

  • Set a bake interval of some specially selected value, preferably a fraction of your sprite size, e.g. 4. Make sure your path points are at multiples of this value. You’ll reliably hit the corner points, but you’ll see some minor deviation (outward “bulge”) as you enter and exit the area around the corner.

  • Set a bake interval of 1, only sample at integer values, and make sure your path points are pixel-aligned. This will reliably hit corner points on the dot, but obviously doesn’t have sub-pixel movement, and there’s a higher memory cost by setting the bake interval so low.

  • Do some extra work to determine your origin and destination point indices within the path’s curve, and linearly interpolate movement between them yourself. Either sidestep the Curve2D sampling, or maybe extend the class with your own implementation.

1 Like

Thank you for your answer!
I’ve tried changing the baked interval and moving the points around, but it’s really fiddly and doesn’t work well in tight spaces.
To be honest, I thought for 2D there would be an option to use line2D, as many popular games in the genre primarily use the 4 cardinal directions.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.