1

I have a simulation where I have balloons bouncing around the screen with a string attached to each one. it functions like a constraint. The string should obey gravity, such that if the balloon isn't max length, there should be slack hanging down.

In order to solve this, I represent the string with line segments. I have the string start hanging straight down, and then iterate over each segment, pointing it to the balloons, and then positioning it so it is connected. This gets the behavior very close to what I want, however in order to make sure that it doesn't leave the center, i translate it back to the place it is rooted in. The problem is that the end no longer is going to be connected. If i didn't have to reset the string to the vertical position this would work, but i have to in order to make sure the slack is calculated.

I added a temporary solution of iterating over it again to reconnect it, and the behavior is decent, but it definitely isn't realistic. An example can be seen here: http://www.mikerkoval.com/simulation/balloons

click to create balloons. I am unsure how to simulate gravity. Is there a better approach to make this more realistic?

-----EDIT----

I am strugglign to get my value solved for a in the catenary function.

I defined my function as:

var k = function (a){
    return Math.pow((2 * a[0]*Math.sinh(h/(2 * a[0])) - sqrt(s*s-   v*v)), 2)
}

however, when I call

var sol = numeric.uncmin(k, [1]);

it runs for a little then throws a Nan error. I am trying to figure out where the problem even is, but I am having very little success since I am struggling to learn what is going on with uncmin

Kyll
  • 7,036
  • 7
  • 41
  • 64
Luple
  • 481
  • 3
  • 21
  • There's several issues about your question. Asking about how to design a physical simulation is _extremely broad_ and not really on-topic for a programming site. We tend to focus on concrete programming issues, not system design. Also, please don't make edits that invalidate existing answers - Gene's answer has nothing to do with the code you edited in. Please check the [help/on-topic] and [ask] for more information. – Kyll Jun 18 '17 at 14:57
  • Thank you for the feedback. I edited in the code because I was unsure if I was on the right track and he seemed to have a better idea and was wondering if i could get feedback. I didn't mean to invalidate his response, I was using his approach and was looking for insight. – Luple Jun 18 '17 at 20:42

1 Answers1

2

I think what you want is to find the parameters for the catenary curve through two points p0 and p1, the positions of the anchor and balloon, which has given arc length s, which is the length of the string.

The equation of the curve is

y = a cosh (x / a)

where a is a parameter you need to determine. Let v = y1 - y0 and h = x1 - h0. With a bit of algebra you can show the value of a must satisfy

sqrt(s*s - v*v) = 2 * a * sinh(h / (2a))

This article a nice discussion of how to solve this for a iteratively with Newton's method. The author makes a substitution of variables b = a/h that make makes the solution space close to linear, so Newton will converge very quickly to a good answer.

Newton's method is a simple iteration that requires the explicit derivative of the curve and a starting point. The article above gives the derivative. Because the curve is so close to linear, you can pick any starting point on that linear portion - b = 0.2 will do. By converging quickly, I mean the number of correct significant digits doubles with each iteration. So in practice you're likely to have all the digits of a double precision floating point number in 6 iterations or less.

Once you have the parameter, it will be a simple patter to plot the explicit curve you need.

If Newton doesn't converge in 6 iterations or so, then the instance of the problem is a very ill-conditioned one. Here that means a curve that's very nearly a straight line. So just draw such a line between p0 and p1!

All this should be easily fast enough: less expensive than your current approximation.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • Thanks for the feedback. This is really useful. I am trying to get my solution working. I am going to make an edit to expand my question with my new problems – Luple Jun 17 '17 at 16:48
  • I realized there's a piece missing. Once you have the parameter `a`, finding the right translation is not so simple. Looking at it. Will post something later if I can figure it out. Another solution that would work without the complication of transcendental functions is to use a parabola. Catenaries are close to parabolic in shape and would probably be fine for your application. @M.Koval – Gene Jun 17 '17 at 18:09
  • Hey Gene, thank you so much for everything. I thought about doing the parabola, but I think this is what I am looking for. just have to figure out how to get it to work. Would like to learn something; well I already have. So I think for the translation what I had to do was calculate the curve first and then map the left point of the curve to the anchor of the balloon. I think the problem I am having is that I am not using numeric.js function numeric.uncmin correctly because the a value I get doesn't seem like it is the minimum point. Also it says that it usually converges with 0 iterations. – Luple Jun 17 '17 at 20:40
  • Thank you so much Gene for putting me on the right track. I got everything working and it looks great! – Luple Jun 18 '17 at 20:42
  • @M.Koval Great! Looks very nice. How did you find the right translation to intersect the endpoints? If my answer was helpful, you might "accept" it. – Gene Jun 19 '17 at 02:53
  • So I found this discussion: http://mathhelpforum.com/calculus/96398-catenary-cable-different-heights.html It basically gave me a step by step walkthrough of the math in order to find the location between two points. I didn't realize that the big problem I was having before was that my numerical solver wasn't getting very good solutions 90% of the time, and so I found a new one that worked much better. The hardest part then was finding a good solution for handling when the distance between the two points is very small, since it should just be a lot of slack. – Luple Jun 19 '17 at 13:19