Smoothly connecting points on a distance/time graph

Discussion in 'Physics & Math' started by jb_sulli, Sep 17, 2009.

  1. jb_sulli Registered Member

    Messages:
    2
    Situation: I'm a computer programmer currently working on an animation project. For this project I want to be able to create a distance/time graph by merely clicking and dragging to create new anchor points. So far you can click and drag to create anchor points and I can figure out the slope that I want for each point. However, I can't seem to figure out how to find smoothly connect each point (i.e. find any distance given a time). I can visualize a smooth connection between each point quite easily, so I think I'm close. My thought was to create a velocity/time graph in the background with a straight line connecting each known slope (velocity) at a given time. However, this does not always work the way I need it to. It's possible I'm doing my math wrong but my guess is that sometimes a curved line is required to connect two slopes (velocities)? I already am prepared for the special cases of slope of zero going to slope of zero and handling an infinite slope.

    Question: Can a smooth line be created between two points with known slopes? Will a constant velocity change between each point work? Or is some acceleration required? If so, what's the anti-derivitive of 1/2bh?

    Thank you!

    Interactive demo of slopes/points (flash 9 required): managemyseats.com/animation_project/
    (just click and drag to create new points)
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. temur man of no words Registered Senior Member

    Messages:
    1,330
    Splines are something that is created for this purpose! You can try polynomial interpolation as an intermediate step since it is simpler to implement, but splines are local in the sense that changes do not propagate very far.

    Your idea with piecewise linear velocity graph will work too, but it will not be local; you add one point and half the distance/time graph has to change (count the degrees of freedom and you will see that the situation is very rigid). So you have to make the velocity/time graph piecewise quadratic. In this case you will be able to fix velocities of every point, and adding a point does not affect those velocities so that the change in the distance/time graph is confined to the interval you are adding point to.
     
    Last edited: Sep 17, 2009
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. rpenner Fully Wired Valued Senior Member

    Messages:
    4,833
    Assume \(t_A < t_B\) -- if not you can just swap A and B.
    You have Point A \((t_A, x_A)\) with associated slope, \(m_A\).
    Likewise, you have Point B \((t_B, x_B)\) with associated slope, \(m_B\).

    problem, what is a smooth function: \(f(t)\) such that
    \(f(t_A)=x_A, f'(t_A)=m_A, f(t_B)=x_B, f'( t_B ) =m_B \).

    One such choice is:

    \(f(t) = \left{ { x_A + m_A(t - t_A) \, \, \textrm{if}\, t \le t_A \\ x_A + \frac{x_B - x-A}{t_B - t_A} (t - t_A) \, \, \textrm{if} \, t_A < t \le t_B \\ x_B + m_B(t - t_B) \, \, \textrm{if}\, t > t_B } \right.\)

    Which is contiguous but only piece-wise smooth, like a polygon.

    A cubic spline would be smooth enough for many applications. It is literally a cubic polynomial in place of the linear connection. And there is only one cubic polynomial which satisfies:
    \(f(t_A) = x_A , \, f'(t_A) = m_A , \, f(t_B) = x_B , \, f'(t_B) = m_B\)


    \(f(t) = \left{ { x_A + m_A(t - t_A) \, \, \textrm{if}\, t \le t_A \\ x_A + (t - t_A) ( m_A + (t - t_A) ( 3\frac{x_B-x_A}{(t_B-t_A)^2}-\frac{m_B+2m_A}{(t_B-t_A)} + \left(2\frac{x_A-x_B}{(t_B-t_A)^3}+\frac{m_B+m_A}{(t_B-t_A)^2}\right) (t - t_A) ) ) \, \, \textrm{if} \, t_A < t \le t_B \\ x_B + m_B(t - t_B) \, \, \textrm{if}\, t > t_B } \right.\)

    or

    \(f(t) = \left{ { x_A + m_A T \, \, \textrm{if}\, t \le t_A \\ x_A + m_A T+ \left( 3\frac{\Delta x}{(\Delta t)^2}-\frac{m_B+2m_A}{\Delta t} \right) T^2 + \left(-2\frac{\Delta x}{(\Delta t)^3}+\frac{m_B+m_A}{(\Delta t)^2}\right) T^3 \, \, \textrm{if} \, t_A < t \le t_B \\ x_B + m_B T \, \, \textrm{if}\, t > t_B } \right.\)

    Where \(T = t - t_A\), \(\Delta t = t_B - t_A\), \(\Delta x = x_B - x_A\).
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. John Connellan Valued Senior Member

    Messages:
    3,636
    Forgive me for my potential misunderstanding, but how can a point have a slope

    Please Register or Log in to view the hidden image!

     
  8. rpenner Fully Wired Valued Senior Member

    Messages:
    4,833
    In computer science, one creates associations between the two, or data structures which hold both.

    In math, one may construct a mapping, either explicit m(x) or implicit M(x(i)) =m(i) as here I have done with the index set of {A, B}.
     
  9. rpenner Fully Wired Valued Senior Member

    Messages:
    4,833
    Now cubic polynomials might look like scary math for a beginning animation programmer. And advanced programmers might think that the method runs the risk of losing too many bits of information with the squaring and cubing of values.

    But if you have a language which works well with lists and vectors and recursion, there is another way to get the same result, and one that allows one to follow the calculation in geometric terms.


    \(f(t) = \left{ { x_A + m_A T \, \, \textrm{if}\, t \le t_A \\ g\left(\frac{T}{\Delta t}, \left[ x_A , \, x_A + m_A \Delta t , \, x_B - m_B \Delta t , \, x_B \right] \right) \, \, \textrm{if} \, t_A < t \le t_B \\ x_B + m_B T \, \, \textrm{if}\, t > t_B } \right.\)

    Where \(T = t - t_A\), \(\Delta t = t_B - t_A\), \(\Delta x = x_B - x_A\).

    \(g(u, L) = \left{ { L_1 \, \, \textrm{if} \, \textrm{size}(L) = 1 \\ g \left(u, \left[ u L_{i} + (1 - u) L_{i+1} \right]_1^{n-1} \right) \, \, \textrm{if} \, \textrm{size}(L) = n > 1 } \right.\)

    Each recursion of g reduces the size of the list, replacing the values with the weighted average of each pair of values. And this basic code works if L is a list of scalars (numbers) or vectors. It works with PostScript/PDF Bezier splines where 4 numbers go in, and with TrueType quadratic splines where only 3 numbers go in each list. And \(g(T/Δt,[x_A,x_B])\) is just the straight line interpolation between x_A and x_B.

    And because of the multiple averaging, everytime u is between 0 and 1, then g(u,L) is somewhere within the convex hull of the values of L.
     
    Last edited: Sep 19, 2009
  10. rpenner Fully Wired Valued Senior Member

    Messages:
    4,833
    That should have been: \(g(T/ \Delta t,[x_A,x_B])\)
     
  11. jb_sulli Registered Member

    Messages:
    2
    Got it!

    I finally got it to work! Thank you so much rpenner! I used your third equation for the most part; however, the coefficient of T^2 ended up needing to be multiplied by 2. I finally was able to do a lot of the math myself but got the wrong coefficient of T^3... so combining both I was able to figure it out.

    Please Register or Log in to view the hidden image!



    Thanks again! I can't wait to start applying it. To see a demo of it you can go to managemyseats.com/animation_project/
     
  12. temur man of no words Registered Senior Member

    Messages:
    1,330
    Nice job!
     

Share This Page