Programming & Using Splines - Part#1
ฝัง
- เผยแพร่เมื่อ 25 พ.ย. 2024
- Splines, in this case Catmull-Rom splines, offer a simple way to have curves in your applications. This video explores the programming to use spline paths and loops that go through all control points yielding an effective way to have more natural NPC AI behaviour.
Github: github.com/One...
Blog: www.onelonecode...
when i see you programming, it always looks soo easy...
This tutorial finally connected all the dots I collected when researching the usage of splines for my voxel drawing engine. Thanks for the nice and graphical explanation, especially also the note about the gradients of the spline.
Hey thanks and no problem Weird Constructor - I'll let the connecting the dots pun slide this time :D
To this day, this is one of my very favorite programming tutorials.
That gradient concept is what I needed to recap my calculus! Thank you for amazing video.
no problem Brian!
this is a hidden gem of a channel, keep it up c:
Cheers prankster, I will!
I was looking for this algorithm for my application! Overlapping the control points to make smooth closed path is smart! Thanks a million!
Well, i spent a few days making a crazy algorithm that splitted my paths in such a way that a bezier curve could closely pass through all the points, just to find out about splines. Your explaination was really helpfull, specially translating all the intimidating math from I found on the wikipedia to code and doing the gradient rotation.
This is lovely and clear! 12:30 for where the magic happens.
Cheers Balor!
Thank you so much for a great tutorial. Finally, I learned how to program splines. I am not a c ++ programmer but still understood the math so now I have programmed my first spline routine in assembler for an old computer that was popular in the 80/90s. Uploaded the routine to my TH-cam channel.
I've been beating my head against ren'py's implementation of splines for like a month because I couldn't find any resources that Actually explained what a control node even is & I have finally run out of other stuff to do instead of figuring it out so I can actually animate with splines & this video was SO HELPFUL, THANK YOU
Fantastic vid and I love your way of explaining things, answers the question of a younger me who always wondered how the homing laser from raiden fighters would have been implemented
Thanks Segolia!
Theres a thing in the android API called PathDashPathEffect that helps you stamp a path along another. Now i understand how its underlying code works. Brilliant 👏.
I just discovered this channel and i have to say it's a true treasure trove of knowledge 😉
I guess I will have to watch part 1&2 a 3rd time.
To access a "negative" index, you can just add the length and do the modulus on that:
left_i = (i - 1 + length) % length;
Doing this will get you rid of the ternary operator and you can go as much as you want in the negative axis.
this is gold, you programmers are geniuses
This video is useful and amazing, I really love your content. Great job!
Marvelous explanation! Thank you! This has been very useful to me.
I’ve seen the apparent quality of a vector contour map product improved this way. It takes a grater of authentic sample points to make a curved line. If the map data is preprocessed so more points can be added, the apparent quality is improved, but it would depend on probability to do with the way mountains form, as to whether the result better represented the real terrain or not.
That's quite cool - the earth is having an effect on the efficiency of an algorithm?! The splines I use here are processor costly because I want to twiddle with them, but you can pre-process a lot of it if they remain static.
Yes it’s done before the GPS unit (or even the consumer) sees the map data. You don’t have to make the GPS unit draw differently (even if you could), the only overall cost is the map data becomes larger with extra points to trace around curves.
Thank you for learning me new words like "orthogonal"...
I just found your channel, awesome Videos man. I just started learning c++ and your videos helped alot.
Hi Flo, that's great, thanks! Good luck with your study and remember to keep it fun!
Thank you, i'll remember to keep it fun ^^
I heart your work. From what I could gather from watching your great videos, I am quite surprised that you prefer Catmull-Rom splines over Béziers curves. I find the absolute elegance of Béziers curves (especially when used through quads) and the superior control they give you, very satisfying. As for the rasterization, check out the Casteljau algorithm. Not optimal but so beautifully simple to implement. The « point-on-path » argument for CR splines becomes a non-issue with just a tiny bit of implementation abstraction (which you’ll need anyway as soon as _things get real_).
Great explanation, thank you (and you still have 0 dislikes, wow)
Thanks MrSerozka!
Thanks for the video. Very informative. A suggestion for future video: show us how to smooth a set of values z(x,y) as a continuous color map for visualization, much like altitude maps
Hmm, OK Elvis, I'll add it to my list!
Great video, thanks for the knowledge!
pure gold !!
thanks for this !!
Great video. A helpful thing would be to use word wrap, so that you wouldn't have to scroll left and right. (Alt+Z on vscode)
that is vs studio not vs code. they are quite different.
*writes down "straight line is NOT curve"*
this triggers my ptsd in math class lmao
Actually is a curve in some sense
04:00 "....we would just get a straight line which you don't need me to tell you isn't a curve" - rofl - really like your videos please keep going!!
Thanks, man. I needed this tutorial
No problem Sonu, man splines are so useful...
Can anyone explain how he got the equations q1 to q4 at around 12:52?
How can i make first point connected to second and last to the one before it? Great video btw, helped me so much
May I offer a nifty suggestion? At 22:00, you show how you can use the atan2() function to get the angle for the gradient. But you know what? You can just use gl.x and gl.y directly for this. They already are your gradient! You don't need to use r = atan2(-gl.y, gl.x), cuz you are then using sin(r) and cos(r) to recalc gl.x and gl.y. Just be sure to normalize gl.x, gl.y.
This is gold
Amazing channel!
great stuff!
Am I correct in assuming this is the means through which they made the creature creator in Spore? It would be cool to know the basics to how they made that because its relevant to something I wish to do.
I ❤ your contents 😊
Aww thanks POG! 😄
Its crazy how I can fail grade 12 math twice but I can watch 1 video of yours and learn the basics of parabolas in minutes... I blame the teacher lmfao
Thank you!
Excellent!
Hey Thanks William.
I wonder if it would be possible to do some kind of depth-first iteration for the spline drawing that divides the t increment by 2 until the pixel to be painted next is the same as the pixel that was just painted, and then move back up. This should have a runtime equal to the path length (best case scenario) and a memory usage equal to the log_2 of the path length. For guaranteeing filled in spline segments of any distance without any tricky issues of cusps and discrete derivatives, I think that's pretty good.
Sounds like its worth trying Tim, give it a go!
@@javidx9 Will do. I got sucked into the rabbit hole of centripetal parameterizatioms and their unreasonable effectiveness after this, so I've got way too much math floating around in my head right now.
Well done
Thanks Carsten.
How does the gradient help you find the angle that is orthogonal to the path?
Where can I find more info about that?
hello - im a young programmer (23) been in the industry since i was 16 and clueless about so much. Thinking of going for a phd next year so i have time to really get nice and personal with some concepts. Do you have any advice for programmers or a previous video related to impostor syndrome? Or literally started out. There seems so much to learn and not enough time
It takes some spine to use a spline! Just found your channel and now i am hooked. I've been coding for over 15 years, and your videos got me back to writing code for fun. Console reminds me of mode 13h and the hours spent drawing gouraud shaded triangles or trying to anyways . I wanted to fork it to and port it ncurses for the *nix and OSX crowd. Thoughts?
Lol bluescan thats great! Thanks! The community developed a portable version that uses SDL as a back end for linux. Though we've not many mac testers.
@@javidx9 Cool! Is the SDL version somewhere in your Repo?
It is. You need the image file that goes with it. On the whole ive moved to olcPixelGameEngine.h its exactly the same but doesn't use the terminal and is natively cross platform.
@@javidx9 just found the video about it thanks :)
I just don't quite understand where do you get these equations: q1 = -ttt* 2+tt - t, etc.?
What about bezier curves?
Dont think thisll get answered considering its been 5 years but im wondering if by changing the equations used for the influence each points have on one point, i can change the way curves are formed? the way im seeing it its like gravity between objects. the stronger the bias each control point has on the point, the more the curves turns towards it. hard to explain what im thinking but from what im seeing it might even be able to do gravitational physics in a way-
Altering the coefficients will affect the trajectory. Don't forget there's infinity correct solutions for joining 2 points, so you're choosing one which is the most aesthetic for your needs
20:47 Hunger
How do you create a perfect 90 degree turn with a cubic spline?
This is why compound beziers are frequently used
when you take out the -3.0f from path.points.size - 3.0f. it makes the struct sSpline be out of index for me. Say i have 10 points. at T = 7 it will be fine but once it goes to t=8 it will have p0 = 7 , p1 = 8, p2 = 9, p3 = 10 which is out of index since its 0,1,2,3,4,5,6,7,8,9. any idea why that is? like it wont draw from the first point to the second point and from the second last one to the last one, and i been stuck on this.
Well this is super late, but I had issues with index out of range for non-looped paths but I fixed by adding this before setting the points ( only for the non/looped bit ):
t = (int)t > Points.Count -4 ? Points.Count-4 : t ;
Would you make an explanation on how to find the cubic curve that has a gradient of 0 at 2 given control points?
Differentiate the curve, and then solve for when dY=0. If its cubic, when you differentiate it, you'll have a quadratic, which can be solved using the well known quadratic formula. If you know your two control points, then start with that quadratic equation, and then integrate it to form the cubic.
#3:43 Giggity giggity!
which programming language was used here
C++
Some how this does not make sense to me.
Why does 13:08 work?
isnt points[p0] a force from 0/0 to p0 witch is scaled by q1?
And not, how intedet, a force from p0 to p1 witch is scaled...
Sorry for the horrible gramma, its late and i am majorly confused.
My curve isnt visible...
Next: collision with splines
But how do you reticulate them‽
Am working on this too. Need to parameterize via calculating derivatives, with hematite curves its the second order for curvature in terms of the magnitude which is the square root of the distance between p0, p1, to reticulate the curve evenly. You can't do it the way he's doin, I was able to accomplish via one loop.
Accidental swastika at the beginning there
edit: or was that the joke with "mind of its own"? 😄
Can you explain why p1 is 0, but to calculate it you take the floor + 1? Why is the +1 necessary if p1 should be 0?
p1 isn't always 0 in the general case. When you start adding segments, t becomes larger than 1. When 1
How do you get the Windows 7 title bar on Photoshop in Windows 10??
Does this work in 3d?
It does! Very easy too, just follow the pattern and add a z term.
@@javidx9 Thanks!
@@javidx9
Great video! I'm learning a lot. But I think you are oversimplfying a little bit when you say it all carries over to 3D just by adding a Z-term, haha.
In 3D it becomes obvious that we don't really care much about the gradient/normal. It is very unstable, an agent following the gradient in 3D will hardly seem to point in the direction of the path, frequently changing direction suddenly or turning around slowly over the course of a section of the curve. At first I thought I was finding the gradient/normal wrong, but that's just how normals are. For some reason, this isn't so noticeable in 2D. Right now I'm trying to implement "rotationally minimalized frames". Basically you set the orientation of the first plot point and then calculate each next points rotation using the prior. Obviously this must be recomputed each time the control points are moved.
Search terms for anyone looking for a solution to the 3D normals problem:
"getting consistent normals along a 3D (Bezier) curve" (math stack exchange)
"rotationally minimized frames" (paper on microsoft)
An easy method is to sample two points along the spline, the difference is the gradient, the normal is now a plane defined by the rear point and the gradient unit vector. Where you sample circumferentially in that plane is up to you, but yeah you tend to care about gradient more than normal, and gradient is trivially computable.
Is there a difference between using splines and bezier curves?
A Bezier curve is a type of spline, so no, no difference.
3:42 not my proudest fap
4 years and he still wont elaborate
0:43 hmmmm
LOL
I have no use for this. Absolutely none. Still gonna watch it though