Bezier Goodness

Showcase your libraries, tools and other projects that help your fellow love users.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Bezier Goodness

Post by Germanunkol »

Image

Download:
Bezier.love
Version 4 - now with shaders, automatic normalmap rendering and different materials.
(28.13 KiB) Downloaded 292 times
(For newest version, check Version on Github.)

Original post:
I love Bezier curves!

Image

Use the code however you want to.

There is still some optiomization that could be done:
  • Change number of segments to draw (currently fixed at 500 in the demo)
  • When moving points, curve is recalculated every frame - could be changed to a max of 20 times a second or so.
Can you tell me how it performs when you move a point? Does anyone get below 45 FPS with less than 20 points?
Attachments
Bezier.love
Old - Bezier curve implementation with simple create/remove point commands
(4.92 KiB) Downloaded 273 times
Last edited by Germanunkol on Mon Sep 30, 2013 1:37 pm, edited 10 times in total.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Bezier Goodness

Post by Roland_Yonaba »

Good work!

With LoveNACL (Chrome Browser):
KkF87Ds.jpg
KkF87Ds.jpg (88.16 KiB) Viewed 592 times
With Love:
ctLXrF7.jpg
ctLXrF7.jpg (76.78 KiB) Viewed 592 times
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Bezier Goodness

Post by vrld »

Good effort.

Some pointers for further optimization:
  1. You directly evaluate the Bezier basis functions. While this works most of the time, it's not numerically stable, i.e. for some values the calculations can go wrong (caused mainly by the the binomial coefficients). Luckily, there is an easy to understand and numerically stable algorithm: De Casteljau's Algorithm.
  2. While straightforward, the rendering algorithm is suboptimal. A better solution is recursive subdivision. Usually about 3 or 4 recursion steps produce good looking curves. You can also stop recursion if the current segment is "suffieciently smooth". You can check for that using the second forward differences.
Side note: LÖVE 0.9 will introduce a BezierCurve object.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Bezier Goodness

Post by Germanunkol »

Hey, thanks for the good advice!

I've implemented a new version using de Casteljau's Algorithm, which was very, very slow at first. Then I used the subdivision algorithm and it's still very slow. I will check again after getting some sleep - maybe I missed something.
The problem with the recursion seems to be that it very quickly grows in computation time when adding more points.

I think it would be no Problem if I only allow cubic Beziers (and automatically split everything above cubic into cubic-and-less Beziers)... I'll have to fiddle with it.

Roland, thanks a lot for the tests! 39 FPS is too low, but then again this is not optimized. Also, the final program will probably not need curves with more than 6 points...
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Bezier Goodness

Post by Germanunkol »

I've reworked the rendering algorithm according to vrld's suggestions.

It's now much more configurable (dynamically chooses the number of segments according to a maximum-segment-length setting) and adding more points does not increase the number of rendering points as much any more. Works for me until up to 150 construction points!

New version in first post.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Bezier Goodness

Post by Germanunkol »

Updated:
  • Multiple shapes
  • Only cubic Bezier curves
  • Remove points, create new points between other points
  • Zoom, move, all the good stuff.
    And, best of all:
  • Interior now rendered in separate thread
Bezier.love
Version 3 - also in first post.
(18.58 KiB) Downloaded 200 times
(Also added to first post, and also on github: https://github.com/Germanunkol/BezierDrawing)

Post your creations! :D (Press F5 for screenshot)
Also, tell me if you find any bugs...
Image
Last edited by Germanunkol on Sat Sep 28, 2013 11:31 pm, edited 1 time in total.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Bezier Goodness

Post by Germanunkol »

I'm quadriple-posting. Sorry :(

I am finally getting somewhere. I'm not posting a .love for every change, if you're interested just get it from github.

Added:
  • Real time lighting (used this tutorial and shamelessly ripped most of the shader code from there)
  • Auto-generated normalmap, diffusemap and specular maps
  • Multiple materials (Feel free to add your own. If they're cool I might include them :) )*
  • Duplicate and flip shapes for easy symmetry
  • Move shapes up and down in the layers
Image
Image

* To make a material, simply copy and paste the metal.lua inside the Material subfolder. Then change it around. the profile function and specular functions give the normals and spec-shading at the edge of a shape. So returning 0,0,1 for the profile function just gives you a flat surface, while returning 1, 0, 0.1 gives you sharp edges all pointing left. Dir is the default normal of the edge, amount is the distance from the edge (ranging from 0 to 1).

profileDepth makes the inset larger.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Bezier Goodness

Post by micha »

The light effect looks awesome.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Bezier Goodness

Post by Lafolie »

Germanunkol wrote:I'm quadriple-posting. Sorry :(
Perfectly acceptable, you posted new, relevant information!
  • Real time lighting (used this tutorial and shamelessly ripped most of the shader code from there)
Thanks so much for this link.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
LoneArtisan
Prole
Posts: 12
Joined: Thu Apr 11, 2013 6:20 pm

Re: Bezier Goodness

Post by LoneArtisan »

Nil
Last edited by LoneArtisan on Mon May 26, 2014 8:36 pm, edited 1 time in total.
You know that moment when you finally do that thing you've been trying to accomplish for hours and you finally get it exactly right? No matter how small the task was, it's one of the greatest things that could happen when you're programming!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests