2

Can somebody walk me through how this madness works: http://www.youtube.com/watch?v=KL8QLLmUvbg

Specifically I'm interested in equally distributing a given number of squares along a path. I'm also wondering if this would work with multiple line segments-- this is one curved segment and I need a solution to distribute objects across one big line with multiple curves in it.

Basically I'm trying to make a tail that realistically follows a character.

Thanks

FlashJordan
  • 173
  • 1
  • 9

3 Answers3

7

First a Bezier spline is a curve parametrized by t. However t is not arc-length along the curve. So the procedure is this.

  1. Calculate the length of the bezier curve.
  2. Find the t values that divide the curve into N equal length segments.

However these two steps are tricky.

The first has a closed form solution only for quadratic Beziers. (You can find the solution here ) Otherwise you use a subdivide and approximate approach, or a numerical integration approach (and in some sense these are equivalent - I'd go the numerical integration approach as this has better provable behavior at the cost of slightly trickier implementation, but you may or may not care about that.)

The second is basically a guess a t value, and improve approach (using the same style of calculation at each step as step 1). I'd implement this using a secant style search, as I suspect the derivatives required to use a Newton's method search would be too expensive to calculate.

Once you've got the positions of the objects, you need to use the curve tangent and cotangent to create a local reference frame for the object. This allows the objects to sit nicely in the path of the curve, rather than all having the same orientation. Note that this only works nicely in 2D - in 3D you can still get some weird behavior with object orientation.

Community
  • 1
  • 1
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • "the derivative required to use a Newton's method search would be too expensive to calculate": I would say it's fast enough, you only have to compute the hodograph once -- the derivative of a bezier curve is also a bezier curve of one degree lower -- which is 4 subtractions. And evaluate at your t value, for example, `x'(t) = t*a + (1-t)*b`, as the derivative of a quadratic bezier curve is a lerp. – Matthias Jun 08 '14 at 10:47
  • @Matthias You're right that the derivative of the curve is simple, however it's not the derivative of position that you need, but the derivative of the arc length. And I was for some reason thinking that was more complex, but of course there is a simple relation between arc-length derivative and position derivative. - So you're correct Newton's method is in-fact a very sensible option for finding the t values. – Michael Anderson Jun 08 '14 at 12:48
0

You can start by looking into how a bezier curve is calculated. Wikipedia has some nice animations with the explanation and this link has some as3 code.

but if you're trying to create a tail, there are simpler ways of doing that, like using following behaviour or a physics library

Daniel
  • 34,125
  • 17
  • 102
  • 150
0

I ended up creating a following behavior system like Daniel recommended for simplicities sake. But to elaborate on Michael's awesome answer I stumbled onto this tutorial which details the the spline technique.

http://gamedev.tutsplus.com/tutorials/implementation/create-a-glowing-flowing-lava-river-using-bezier-curves-and-shaders/

FlashJordan
  • 173
  • 1
  • 9