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 / [T1] "Automatic" Bezier Path Generation?

Author
Message
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Mar 2014 08:07 Edited at: 20th Mar 2014 12:27
Hi all,

This is kind of a complex subject (mathematically), so I would prefer a more conceptual answer.

Basically, I currently have bezier path code working in AGK. It works perfectly and does exactly what I tell it; but that's the issue, I can't know what to tell it.

Ok, so I want to be able to place down "control points" in realtime, and draw a smooth flowing curve between each one. This can be achieved by hand by manipulating the tangents of the curves, but I need some way to make this automatic. Here, pretty much exactly like this.

What kind of code would I use to work out where the tangent points need to be to give a smooth flow like that?

I know I haven't explained myself very well, but my brain's tired and I hope someone can work out what I'm trying to say and help me

Thanks

----------------------------------------------------------------------------------------

Update [13/03/14]: I solved the issue myself Here's the code: DON'T USE THIS CODE NOW. USE THE UPDATED VERSION AT THE BOTTOM OF THIS POST.



curvepoint is defined as such:



More info on code usage in this post.

----------------------------------------------------------------------------------------

Update [16/03/14]: AGAIN, DON'T USE THIS CODE ANY MORE - THERE'S A BETTER VERSION HERE More changes to the code. Well, not so much changes as I didn't realise I hadn't included some of the functions used by my bezier code....so here is the latest version. Also included now is a function that finds to closest point on the curve to a specific point elsewhere. You should define 4 globals:



The distance-to-curve function uses a pretty basic brute-force method, so it could probably be faster, but you can change distanceiterations to adjust how accurate (and how slow) the function is at finding the closest point.



This post still applies for setting up and using the code.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 12th Mar 2014 09:03
4 point bezier is much more easier, the curve go exactly through points.
its a simple function, i have at home.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Mar 2014 09:29
What do you mean by "4 point"? I'm currently using a cubic bezier, points 0 & 3 are the end points and 1 & 2 are tangent points. I'd appreciate any code you can throw at me

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 12th Mar 2014 09:47 Edited at: 12th Mar 2014 15:31
i believe this bezier i had used , the curve gone through this 4 points.
if not i remember i had use something similar, i can give u later.
see attachment

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670

Attachments

Login to view attachments
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Mar 2014 12:09
What you have in the image there looks to be what I'm trying to achieve.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 12th Mar 2014 12:37
This might help:
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Mar 2014 12:53
Ah, but that uses control points, right? That's virtually the system I have now, only mine's easier to use, but I need a control-point-less system.

After some more research I've realised what I need is more correctly called spline interpolation. I just need something that takes a set of (x,y) points and a distance along the line and returns the (x,y) position at that point along the line. The line needs to be a smooth, flowing curve passing through every point. Like this, but something that can also go in circles.

JohnnyMeek
11
Years of Service
User Offline
Joined: 23rd Apr 2013
Location: Slovenia
Posted: 12th Mar 2014 13:08
I had a similar issue, and started out using Bezier but couldn't calculate the control points.

In the end I went with a Catmull curve instead. It will give you a nice curve between all (4) points, and always travels through the points.

Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Mar 2014 13:27 Edited at: 12th Mar 2014 13:28
Quote: "I had a similar issue, and started out using Bezier but couldn't calculate the control points.

In the end I went with a Catmull curve instead. It will give you a nice curve between all (4) points, and always travels through the points."


Thanks, that's very helpful indeed (concept-wise), but I need to be able to place an arbitrary number of points and I see no way of easily using the Catmull curve for this

However, I have come up with a cunning plan of my own. Well, I saw it in the picture on page 11 Implementing this is going to fry my brain, though, so wish me luck

Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Mar 2014 14:27 Edited at: 13th Mar 2014 09:07
Ok, I solved it (and my brain didn't melt! ). This is the code:

WARNING! DON'T USE THIS CODE! IT'S SEVERELY BUGGED!


It seems to work perfectly, except occasionally when one or two "dots" (what I'm drawing the curve as) appear at random places on the screen - probably just a little bug, I'll go through the code carefully later.

For anyone else using the code, curvepoint is defined like so:



Each point is simply a point, there's no control points or anything. The curve will start at the first point in the array and flow smoothly past/through each point.

Also, I realised I actually want my curve to NOT flow directly through each point but flow past them. As a result, you can increase or decrease the 0.3333 values (which could be moved to variables for easier changing) to relax or stiffen the corners, but 0.3333 is the smoothest.

Lastly, if you need the curve to be a closed loop, you could just make the endpoints join (which I will almost certainly be doing anyway, so you can have that code as well when I finish it).

The next thing I have to do is work out how to find the closest point to a bezier curve, which, luckily, I should be able to do, because my system is built on normal cubic bezier curves. The difficulty will be finding information on how to actually implement such a thing in code

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 12th Mar 2014 15:29
That looks nice and easy to use. Thanks for sharing
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 13th Mar 2014 06:20
Quote: "That looks nice and easy to use. Thanks for sharing"


No problem, I like to share my work

I've decided to go for a brute-force method rather than a clever method for solving closest point on the line. Hopefully it will be fast enough, but if not, it shouldn't be impossible to use a calculated method.

Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 13th Mar 2014 07:56 Edited at: 13th Mar 2014 08:04
Update: I recommend NOT using the code I posted above just yet. I found a pretty severe bug which calculates the ends of the curves completely wrongly. It's my fault and a pretty easy fix, I just have to find the exact location of the fault and I'll post the updated code

Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 13th Mar 2014 08:59
Update: Here's the latest code, with bug fixes. Now no longer incorrectly calculates the final curve point.



Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 16th Mar 2014 12:37
Update: More changes to the code. Well, not so much changes as I didn't realise I hadn't included some of the functions used by my bezier code....so here is the latest version. Also included now is a function that finds to closest point on the curve to a specific point elsewhere. You should define 4 globals:



The distance-to-curve function uses a pretty basic brute-force method, so it could probably be faster, but you can change distanceiterations to adjust how accurate (and how slow) the function is at finding the closest point.



Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 20th Mar 2014 12:08
Update: Here's the latest (and probably final, as it seems exactly as I need it now) version. Now included is a way to precompute the "closest point" points for extra speediness.

What I've also done is separate the bezier curve code from my project code and make it a library (much easier to use). You can get it here, now living in its very own thread: http://forum.thegamecreators.com/?m=forum_view&t=210460&b=41&p=0

Login to post a reply

Server time is: 2024-11-25 03:58:16
Your offset time is: 2024-11-25 03:58:16