Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / looking for an Algorithm for bezier curve or spline... For 2D movements

Author
Message
Freddix
AGK Developer
21
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 30th Jan 2018 13:40
Hello,

I've searched the forums but nothing seem to fit my need.
I need something that works this way :

1. I setup a spline using some dots.
2. The 1st dot = 0%, the last one is 100%
3. I need a calculation that tell me the 2D Position in the spline when entering a % value
4. It must also tell me the tangent angle to the spline at the choosen dot.

Do you know if there is any math algorithm for that somewhere ?

Thank you,
Regards,
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 30th Jan 2018 14:18 Edited at: 30th Jan 2018 14:20
This is from somewhere. Might have been someone elses but I found it in one of my projects. Edit v# between 0.0 and 1.0 for percentage along the line (I think):

Using AppGameKit V2 Tier 1

Attachments

Login to view attachments
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Jan 2018 15:59
You didn't look hard enough then. There's several bezier examples out there.

Quote: "3. I need a calculation that tell me the 2D Position in the spline when entering a % value"


That's a little trickier. I know people have tried before without much success, myself included. I went to a math forum trying to find a way to parameterize the bezier equation to do just that, it doesn't seem possible. My current solution isn't 100% accurate as it relies on estimations limited to the precision at which you draw your curves. I'd say for 99.9% of folks, it'll be plenty accurate enough.

Clonkex made a nice bezier library, and if you read through the comments you'll see my explanation for interpolating the spline.
https://forum.thegamecreators.com/thread/210460


As far as the tangent, you'll probably have to calculate that yourself, I don't have a solution off the top of my head for that but it should be trivial.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 30th Jan 2018 22:29
What would be the fastest way to draw the curvy line?
puzzler2018
User Banned
Posted: 30th Jan 2018 22:34
Been tested on performance of the draw calls by Bengismo on another thread

Will need to draw dots for a curvey line, fasted one is

drawline(x,y,x+1,y+1......

It seems
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Jan 2018 23:17
You'd have to draw multiple line segments for the curve. The more segments, the smoother the curve. Think of drawing a circle. Technically infinite points. An octogon has 8 sides by a somewhat circular shape. Make it with 20 sides and you got a polygon that looks even more like a circle. More lines, better precision.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 31st Jan 2018 00:06 Edited at: 31st Jan 2018 00:14
Not sure its the fastest...but using the bezier algorithm posted above by baxslash and using DrawLine() gives this



The top curve is using 100 points
bottom uses just 10 points...

you can choose how many you want
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2018 02:04
my path editor will plot and draw those points https://forum.thegamecreators.com/thread/221096
it uses Clonkex's Bezier Library available here https://forum.thegamecreators.com/thread/210460
Features:
- Fast bezier curve calculation
- Open and closed curves
- Easily find closest point on curve
- Can divide curve into arbitrary number of segments for line drawing
- Floating point precision for sub-pixel alignment
- Both Tier 1 and Tier 2 support

the list of clonkex's functions marked in bold to show you the ones to cacluate position in a curve for example
fubar
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 31st Jan 2018 10:27
I'd say DrawLine() is pretty fast. I had it at like 3000 segments with no noticeable drop in frames lol.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 31st Jan 2018 12:12
An alternative to using drawline is to use sprites instead. It's one method folks have used to draw thicker lines. You could then potentially use the sprites to design roads and whatnot as well.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Jerry McGuire
7
Years of Service
User Offline
Joined: 25th Mar 2017
Location:
Posted: 1st Feb 2018 23:30 Edited at: 2nd Feb 2018 00:08
Quote: "
I need something that works this way :

1. I setup a spline using some dots.
2. The 1st dot = 0%, the last one is 100%
3. I need a calculation that tell me the 2D Position in the spline when entering a % value
4. It must also tell me the tangent angle to the spline at the choosen dot.
"


Hi Freddix, splines are typically made with polynomials. Like f(x)=2+3x+4x^2..., which you can set as the equation y=2+3x+...
If you have n points, then you need a polynomial with n coefficients (write them like c_i, where i is a subscript). Like y=c_1+c_2*x+c_3*x^2+c_4*x^3+...+c_n*x^(n-1)
Your list of points is something like (20%,12), (30%,4),..., (x_n, y_n)
To calculate the coefficients you use the n equations that result from substituting n times the corresponding coordinates in the equation. Thus getting n equations.
To get the tangent you get the slope using the derivative evaluated at the point of interest. You get the intercept of the tangent by substituting the slope into the linear equation of the tangent, together with the point of interest coordinates.

For example,
Suppose you want a spline for the 2 points (0, 32), (1, 3). Then you know you need a polynomial with 2 coefficients which gives the equation
y=c_1+c_2*x

Since you have two points you can substitute the x,y coordinates two times in the equation, thus getting two equations like:
32=c_1+c_2*0
3=c_1+c_2*1

Now you solve this equation system of two equations. In this case, you can immediately see from the first equation that c_1=32,. Substituting that into the second equation you get the equation 3=32+c_2*1. Which gives c_2=-29.
Your spline would be y=32-29*x.
Suppose you want to get the tangent for x equals 50%=0.5; the tangent is a line given by Y=A+B*X. Then the derivative of your spline gives you the slope B: y’=-29. Now you substitute the slope and the values of your point (X=0.5, Y=32-29*0.5=32-14.5=17.5, B=-29) into the tangent equation: 17.5=A-29*0.5 ; you get A=32. Thus the equation of the tangent associated with the point (the one with x=50%=0.5) is Y=32-29*X. In this case the tangent has the same equation as the spline because the spline itself is just a straight line, but that will not be the case with a more elaborate spline.

You can substitute your x=0%=0 to get the corresponding y. And substitute your 100%= 1 to get the corresponding y. But more interestingly, you can substitute any number between 0% and 100% to get the corresponding y.

This example is extremely easy. But you can do it for lots of points in an Excel spreadsheet.

P.S. If you are looking for more complicated curves like a spiral or an infinity sign or an S like that of baxslash, were both the X and the Y coordinates go back-and-forth, then you will need what is called a parametric curve. Let me know and I will explain it to you.
iMac Book Pro, MacOS 10.12.4, Xcode 9.0.1;
iPhone 6, iOS 9.35; iPhone 5s, iOS 9.35; iPad (3rd gen), iOS 9.35;

Dell Precision T7400, Windows 7 Professional 64bit, Visual Studio Community 2015;
hendron
8
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 2nd Feb 2018 17:49
No idea if it's the best way or not, but I use a recursive approach. This is a very stripped-down example adapted from my personal Spline library. It can handle any number of points. The tangent vector is visualized as a yellow line running across the spline. Note that it's not normalized in this example, so its magnitude varies across the curve. I did my best to explain what's happening in the comments.

Login to post a reply

Server time is: 2024-04-26 01:58:28
Your offset time is: 2024-04-26 01:58:28