Grappling hook slowly getting larger

I’m implementing a grappling hook into my game. I want it to keep its length constant once hooked, therefore creating a circular motion around the hook point. Here’s an awesome high-resolution scheme:

http://img405.imageshack.us/img405/231/esquemaj.png

To keep things simple, I decided to keep with linear motion calc instead of switching to a circular reference system when hooked. My approach to create the circular motion is to get the player speed (yellow in the scheme) and find its orthogonal projection (pink) to the hook vector (blue/green). Then I reverse the orthogonal projection and add it to the player speed, nullifying any speed parallel to the hook vector, so that only orthogonal movement to it is possible, effectively creating a circular motion.

It seems to work, but when I’m hooked, for some reason the hook gets slowly larger, like one pixel per second or so. What could be the cause for that? Maybe it’s a precision problem? I doubt so, I’m using doubles, and one pixel per second is like A LOT of precision loss.

EDIT: I got an answer in other forum where I asked the same question. The problem is that I’m taking the tangential velocity at the tangential point. Therefore the player moves away from the circle as the only point the tangent is on the
circle is the one where the player is currently at. Okay, I got the problem, but I can’t come with a solution!

It’s because of the finite step size - that linear trajectory is only valid for an infinitesimal, and over a finite step, you’re diverging very slightly from the “correct” path. In this case, the error is always in the same direction, growing the distance, so over time it really adds up.

I would suggest directly correcting the distance at each step.

Thanks for the answer :slight_smile: I thought about directly correcting it as you suggest, but it didn’t seem very elegant. I think it’s the only option, so… how could I know the correction factor? How much exactly I’m deviating from the correct position in each step?

How much exactly I’m deviating from the correct position in each step?

Doesn’t matter. Calculate the length at the beginning. Then, at the end of each step, set the length to the previously measured length.

Thanks, that worked :slight_smile:

It might not be elegant, but the only other solution is really to use a different velocity update scheme, and actually calculate the force that the chain is applying on the hook so that you get better accuracy past first order.

Not worth the trouble, IMO - almost every general game physics engine in common use would (under the hood, after you strip away all the irrelevant complexity) do pretty much what you’re doing, and correct the position after the fact.