2
\$\begingroup\$

What are the options and mechanics for creating a grappling hook in a 2D game?

In the prototype for this feature, the person runs around in a platformer world and can throw the hook at any wall, where it sticks and shows a line to the player sprite. So that part (the easy part) is solved. But what options are available now for getting a rather good looking (not necessarily "correct") Tarzan-style grappling hook going, e.g. one allowing some kind of neat-looking swings?

(I understand several different approaches of how the mechanics could work are available; I'm looking for a simple one that may make some compromises and not be entirely physical, e.g. perhaps something which attracts the player to the hook center for a while or so while keeping to a certain rope min distance or such.)

I'm using JavaScript/ ImpactJS, by the way. Thanks!

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

I have had some success with this problem, and my solution was certainly a "compromise" on the exact physics. That is almost always desirable, anyway.

My method was to maintain a fixed radius from the grapple point, and only update a speed scalar. I did not store a velocity vector; I will explain. Then each frame:

  1. Determine player direction as a normal vector perpendicular to the direction from center of rotation to position.
  2. Calculate total acceleration, (gravity, player input, etc.) in the direction of motion, and reduce that to a scalar value.
  3. Modify speed scalar by the acceleration scalar.
  4. Move the player in the direction of motion from step 1, with distance according to the speed scalar.
  5. Modify position such that the stored radius is maintained. (Find the new direction to the center, and reduce the distance to that point to what it should be.)
  6. Determine the new direction of motion and reduce speed by its dot product with the old direction (aka the cosine of the angle between them).

That last step will be a very high fraction of unity for small angle changes. It will slow the player down, but the deceleration will be almost imperceptible for slow speeds, and far more noticeable for higher speeds. It will appear as though you planned it that way, almost as if you were calculating a drag force. :)

It is conceivable to allow the swing radius to change, but I would recommend making that a separate system with completely independent, more arcade-like motion. In any case, it would also yield weird effects. For example, it would introduce physics abuses like those the ninja rope races in the recent versions of worms:

worms ninja rope elevator

You will need to mitigate these problems as you see fit. But then again, they made for fun gameplay, so try it and see what happens.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .