paths straight vs curvy

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Rigachupe
Party member
Posts: 100
Joined: Fri Jun 18, 2021 11:21 am

paths straight vs curvy

Post by Rigachupe »

Can you help me understand how path that objects travel on screen can be done?
path1.png
path1.png (7.5 KiB) Viewed 6239 times
I did the straight path code with an array of points and calculating the way from each point to next point until end of path.
But how to round the path with a certain precision that it would become like the last 3 on the picture?

I assume that it calculates distances from point to next point and then splits the distance into more points and then "somehow" round the path.

But i think it should visit each point and not round it towards the inside and away from a point. Maybe this function could be used

Code: Select all

curve = love.math.newBezierCurve( vertices )
and this to get a point by assuming that t from <0,1> means that path can by calculated by delta time let's say 5 seconds split into smaller time deltas and then thrown into <0;1> range of the function.

Code: Select all

x, y = BezierCurve:evaluate( t ).
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: paths straight vs curvy

Post by pgimeno »

I'm not sure I understand. If the path must not cross any control points, how is it defined? What points should it go through? Is a circle good enough?

A working definition would be to place the waypoints in the midpoints between two control points. In that case you could use quadratic Béziers, which are easy to convert to cubic for use with Löve. See https://simonhalliday.com/2017/02/15/qu ... urve-demo/ for how a quadratic Bézier looks like. In your example, you would use three quadratic Bézier curves: first, the midpoints between each pair of points are selected as endpoints; then, the control point between two such endpoints is selected as the control point of the Bézier.

If you want a path that goes through certain waypoints but in a curvy way, however, you can use Kochanek-Bartels curves. The Kochanek-Bartels method generates cubic Hermite splines, but these can be easily converted to Bézier too. I've done that a few times already.

I can get into more details after I understand what you're after, if you want.
Rigachupe
Party member
Posts: 100
Joined: Fri Jun 18, 2021 11:21 am

Re: paths straight vs curvy

Post by Rigachupe »

The curves are draw so that the points are not traveled but rounded. I have experimented with this and need something else to work through math. So this i will close for now. The beziers used in love can be used for some things but not in my case. My solution is adding more points and the curvature will be seen enough even without the use of bezier curve.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: paths straight vs curvy

Post by pgimeno »

*Shrug* Judging by your example, it would appear that the midpoint solution would work. As it turns out, Löve accepts N-order Bézier curves, not just cubic, therefore it was even more straightforward than I thought to implement quadratic Bézier segments.

qbezier.gif
qbezier.gif (29.61 KiB) Viewed 5854 times
If that does not suffice, maybe you can try with B-splines, but you'll have to do the math yourself: https://pages.mtu.edu/~shene/COURSES/cs ... losed.html

Here's the source for the above GIF.
Attachments
qbezier.love
(885 Bytes) Downloaded 153 times
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: paths straight vs curvy

Post by RNavega »

To give pgimeno's suggestion of Kochanek-Bartels a second look, the article that was pointed says that when all parameters are set to zero, the spline becomes a Catmull-Rom.
These Catmull-Rom splines were researched specifically for use in animation, as they pass through all of their control points which I think is what you're looking for.

There are three different ways to calculate the weights for a C-R spline, the best of which is the "centripetal " way.
This is all explained (including the code) in this article, where there's a live editor that you can draw the C-R spline and get a feel for how it behaves, it's pretty cool:
https://qroph.github.io/2018/07/30/smoo ... lines.html

There's also some Lua sample code in here:
- viewtopic.php?p=228432#p228432
- https://gist.github.com/alex-golovanov/1383576
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Hugues Ross and 11 guests