Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Bezier curves

  1. #1

    Bezier curves

    Came to thinking ways to make 3D animation smooth and came across with bezier curve. Had no experience on it before and thought it would be so overly complicated and all... However, there was nice animated gifs at wiki that explained it all to my simple mind that cannot understand the formulas :lol:

    Anyway, i can already imagine so many different uses to this, maybe you do too.

    http://en.wikipedia.org/wiki/B%C3%A9zier_curve

    Screenshot: http://img216.imageshack.us/img216/1517/bezierxq3.jpg

    To my surprise, code became very very simple and seems very fast too! No sqrt or power functions needed. Here's code that i now have on my Next3D engine:

    Code:
    // Define these 2 functions overloaded
    
    type 
      TVector3f = record x,y,z: single; end;
      PVector3f = ^TVector3f;
    
    // delta = 0..1
    function nxBezier(const a,b: PVector3f; const delta: single): TVector3f;
    var d1: single;
    begin
      d1:=1-delta;
      result.x:=a.x*d1+b.x*delta;
      result.y:=a.y*d1+b.y*delta;
      result.z:=a.z*d1+b.z*delta;
    end;
    
    // Always call with count >= 2
    function nxBezier(p: array of TVector3f; const count: word; const delta: single): TVector3f;
    var v: array of TVector3f; i: integer;
    begin
      if count>2 then begin
        setlength(v,count-1);
        for i:=0 to count-2 do
          v[i]:=nxBezier(@p[i],@p[i+1],delta);
        result:=nxBezier(v,count-1,delta);
      end else if count=2 then
        result:=nxBezier(@p[0],@p[1],delta);
    end;
    Enjoy!

    Edit: Still, maybe for animations some other formula would fit better? Something that doesn't approximate as much to remove that much details...

    Thought of another function came to mind, when 3 points in animation frame are given (previous vertex, current and next), start curve from half way between time frames. That way details remain not much changed and is possible to make faster function specially for this. But still this wouldn't reach the "top" of each keyframe.
    Last edited by User137; 11-04-2011 at 03:29 PM.

  2. #2

    Bezier curves

    And now thing i call half bezier:



    This should be awesome for animating 3d models at least in theory.

    [pascal]function nxHalfBezier(const a,b,c: PVector3f; const delta: single): TVector3f;
    var i,j,b1,b2: TVector3f;
    begin
    i.x:=(a.x+b.x)/2; i.y:=(a.y+b.y)/2; i.z:=(a.z+b.z)/2;
    j.x:=(b.x+c.x)/2; j.y:=(b.y+c.y)/2; j.z:=(b.z+c.z)/2;
    b1:=nxBezier(@i,b,delta); b2:=nxBezier(b,@j,delta);
    result:=nxBezier(@b1,@b2,delta);
    end;[/pascal]

  3. #3

    Bezier curves

    nice code i will try to use it in opengl to produre nice effects
    From brazil (:

    Pascal pownz!

  4. #4

    Bezier curves

    Very nice... will take a look (and probably use) the code :razz:

    ATM i'm writing and gathering good math-related source. e.g I coded a function that rotates 2D vectors around a center (finished it just a few minutes ago) and a Line-Circle intersection algorithm.

    These are really nice snippets. How about setting up Pascal-GameDev snippet library?? :razz:
    (I'll add my snippets too, then)
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  5. #5

    Bezier curves

    Quote Originally Posted by chronozphere
    Very nice... will take a look (and probably use) the code :razz:

    ATM i'm writing and gathering good math-related source. e.g I coded a function that rotates 2D vectors around a center (finished it just a few minutes ago) and a Line-Circle intersection algorithm.

    These are really nice snippets. How about setting up Pascal-GameDev snippet library?? :razz:
    (I'll add my snippets too, then)
    a wiki?
    From brazil (:

    Pascal pownz!

  6. #6

    Re: Bezier curves

    Quote Originally Posted by User137
    http://en.wikipedia.org/wiki/B%C3%A9zier_curve

    Screenshot: http://img216.imageshack.us/img216/1517/bezierxq3.jpg

    To my surprise, code became very very simple and seems very fast too! No sqrt or power functions needed. Here's code that i now have on my Next3D engine:
    Your routine is smaller than the one at Wikipedia, how did you do that? :shock:

  7. #7

    Bezier curves

    Quote Originally Posted by arthurprs
    nice code i will try to use it in opengl to produre nice effects
    upload a demo with the source code
    From brazil (:

    Pascal pownz!

  8. #8

    Bezier curves

    Bezier curves have one disadvantage. If you interpolate a set of points with such curve, it will not pass through all the points. So, if you want to interpolate a movement through a set of waypoints you must also specify a set of control points outside of the path.

    It is much better to use Catmull-Rom splines which can be also used for keyframe animations.

  9. #9

    Bezier curves

    Quote Originally Posted by grudzio
    It is much better to use Catmull-Rom splines which can be also used for keyframe animations.
    Thanks.

    http://www.mvps.org/directx/articles/catmull/
    Showed simple formula of:
    q(t) = 0.5 *( (2 * P1) +
    (-P0 + P2) * t +
    (2*P0 - 5*P1 + 4*P2 - P3) * t2 +
    (-P0 + 3*P1- 3*P2 + P3) * t3)

    But i see small disadvantage to this. In figure 2 of that formula i see curve taking an extra leap upwards before heading to next point from P0 to P1.. same with P2 to P3. Wouldn't this look clumsy on animation? Or perhaps something similar to my half bezier can be used on those 2 middle ones that looks working good...

  10. #10

    Bezier curves

    Hmm.. i dont think that's a problem. I see that this method uses vectors to control the line direction.
    (Much like the pen tool in photoshop). That extra leap is there because the vector of P0 points up while P1 is located at the bottom right, from P0's point of view.

    So that extra leap is OK!
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Comodo SSL