Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Bézier curves  (Read 36649 times)

0 Members and 2 Guests are viewing this topic.

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Bézier curves
« on: June 09, 2013, 09:13:40 am »
Wiki:http://en.wikipedia.org/wiki/B%C3%A9zier_curve

Not quite sure how hard it would be to set this up in something like SFML but I could definitely use/need it! I want to use it for in-game wires and some minor details like wind or Stink lines(like in cartoons). Maybe even tough geometry where if I did it in something like gimp it would seem too pixelated or pregenerated.

I was also thinking it could also be adapted to improve the text code, so the lines on it would look less blurred and more smooth and sharp! Now there is nothing wrong with straight lines... I just really need those awesome curvy lines!
Who's with me for this?
If you notice I put "....", in my sentences way too much... I'm sorry.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Bézier curves
« Reply #1 on: June 09, 2013, 10:21:19 am »
Quote
Who's with me for this?
Not me ;D
Laurent Gomila - SFML developer

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Bézier curves
« Reply #2 on: June 09, 2013, 10:45:07 am »

I thought that would work.... Well what if I made a commit on github? Maybe then?
If you notice I put "....", in my sentences way too much... I'm sorry.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bézier curves
« Reply #3 on: June 09, 2013, 10:55:19 am »
Well what if I made a commit on github? Maybe then?
If Laurent says no, it's rarely because he needs help with the implementation ;)

Bezier curves are outside the scope of SFML. But Stroke or Zoost and Zoom may help you.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Bézier curves
« Reply #4 on: June 09, 2013, 11:02:45 am »
Quote
If Laurent says no, it's rarely because he needs help with the implementation
Sometimes it is. Such a feature (wich could really be "a SVG module for SFML") is too big and I can't start it. If you come up with a good implementation, and most important a very good design, it might work ;)
Laurent Gomila - SFML developer

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Bézier curves
« Reply #5 on: June 09, 2013, 11:16:25 am »
Quote
Bezier curves are outside the scope of SFML. But Stroke or Zoost and Zoom may help you.
Both Stroke,Zoost, and zoom are either out dated or totally evaporated due to the jump to SFML 2.0 and/or MegaUpload was taken down a long time ago. :|

Quote
Sometimes it is. Such a feature (wich could really be "a SVG module for SFML") is too big and I can't start it. If you come up with a good implementation, and most important a very good design, it might work ;)
Time to whip out my math skills and a good open source graphics editor(Gimp most likely) for reference!
It might take awhile so I will probably make a prototype and and build on it... then work my way up to making a commit towards SFML.Wish me luck!  ;D
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Bézier curves
« Reply #6 on: June 09, 2013, 11:40:45 am »
I once made a quick and dirty port of Stroke for SFML 2 and hoped the original author would port it properly (as promissed), but I guesd, it won't happen. I think I've posted my port on the offical forum thread for Stroke.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Bézier curves
« Reply #7 on: June 09, 2013, 10:13:01 pm »
If it's going to be implemented, look into the recursive definition. It can be very useful for finding the different points. Also consider using a finite number of "steps", like the circle shapes do. Circles use 40 points by default, so the angle betwen two consecutive points is 2pi/40. Bezier curves use a parameter t instead of angle, so perhaps split that into 40 pieces and creating a 40 point line?
I use the latest build of SFML2

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Bézier curves
« Reply #8 on: June 10, 2013, 10:38:56 am »
Of course it can be done similar to circles. Essentially, all you have to do is determining the point coordinates for a given number of points, then connect them using lines. It's rather trivial once you got the calculation right.

Might give it a try once I've got some free time to burn. :) But only if I get to see some funny games with lots of stink lines. :P

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Bézier curves
« Reply #9 on: June 10, 2013, 02:43:35 pm »
Made a quick function to calculate the points for a cubic bezier curve. To use it, you'll need:

- A start point.
- A end point.
- Two control points determining the initial slope at the start/end.
- The number of segments to draw (1 = straight line).

It will then return a list of points on the actual bezier curve to draw.

There's probably room for improvements here, but you can use it as a starting point. This is no sf::Drawable, so you'll have to use your own drawing (e.g. drawing lines or using a sf::VertexArray).

Code on the Wiki

Might be interesting to have some premade class similar to sf::Line I guess.

Example drawing (20 bezier curves with 50 segments each):
« Last Edit: June 10, 2013, 04:35:34 pm by Mario »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Bézier curves
« Reply #10 on: June 10, 2013, 03:02:13 pm »
You should put it on the wiki, with an example of drawing that uses a vertex array.

Some minor improvements:
- I don't think that the template parameter is needed, I can't imagine a use case for another type than float
- p * p * p should be more efficient than std::pow(p, 3)
Laurent Gomila - SFML developer

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Bézier curves
« Reply #11 on: June 10, 2013, 03:10:52 pm »
I thought about replacing the pow() as well, although I always thought it's better to assume "the compiler knows what it's doing".

I added the template as this might be something to be used outside drawing. Playing around with the wiki...

Edit: Done - https://github.com/SFML/SFML/wiki/Source%3A-cubic-bezier-curve
« Last Edit: June 10, 2013, 03:33:27 pm by Mario »

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Bézier curves
« Reply #12 on: June 10, 2013, 03:56:45 pm »
The way to test would be to compile std::pow and x*x*x with maximum levels of optimisation and compare the assembly.

I do know that g++ with -O3 can optimise std::pow(x, 2) down to the same assembly. I'd assume it can do the same for power of 3.

Now whether this is actually more readable than x*x*x is a whole other issue, and much more down to personal preference...
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bézier curves
« Reply #13 on: June 10, 2013, 04:05:02 pm »
Why std::list and not std::vector?

Also, I think you can work directly with sf::Vector2f for the computation, there's no need of separating the components...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Bézier curves
« Reply #14 on: June 10, 2013, 04:12:53 pm »
Just personal preference. Use whatever you want to implement it (should be easy to replace/update) and what you think fits your specific use case.

As for working with **sf::Vector2** only, this won't work, it's lacking operators taking simple numbers (you'd have to remove all multiplications).