The truth about the rope

Published on 2023-12-25 by TomatoSoup

I've been asked how I made the grapple for my Spiderman gamemode work so well. So, I present the truth about the rope.

There is no rope!

The grappling hook mechanic is a kinematics sim. When you grapple on to something, it sets three variables that are necessary to the sim: The entity you grappled, the grapple position, and the length of the "rope", which is just the distance between the player and the grapple position.

The entity you grappled and the grapple position are used to compute the world location of the grapple, which is what is used for all further calculations on the player's movement. The grapple is capable apply force on any physics objects you're attached to, which does the same logic as the force applied on the player and applies it to the local grappled position on the target.

On every frame, the physics sim runs. Follow along, if you'd like.

It takes the vector from the player to the world grapple position and terms this the tension. Truly, it's used to determine what the tension would be, and the direction of it, if the player were fully reeled in. It also computes the length of this vector and calls that the stretch.

If the stretch is too much greater than the length, by approximately 5 meters (given that the community seems to have centered on 52.5 source units per meter), the rope breaks. Holding shift will reel the rope in, shortening the length. If the rope is not under load, the rope reels in faster.

If the stretch is greater than the actual length of the rope we have to apply the tension to push the player back towards the rope.

Recall the basic formula for a spring: F = -xk where F is the force acting upon the load, x is the displacement from neutral, and k is the spring constant. It's negative because it needs to move back against the displacement. The rope is actually simulated as a perfectly elastic spring: The displacement is calculated by subtracting the rope length from the stretch to get the strength of the force. If the player is closer than the slack in the rope, this is a negative number, but we can't enter this code block in that case and so it is unconsidered. If the player is further than the length of the rope, it is a positive number.

A bounce is also added to the rope by considering the player's velocity's dot product with the direction of tension. If the player is deep in a swing, they are moving perpendicularly to the direction of tension which gives a dot product of zero and the rope acts as a perfectly linear spring to accelerate them through. But if the player falls, and the rope abruptly stops them, the strength of the spring is increased to bounce the player at the end of the rope and jerk them about.

Finally, the force is simply computed as the tension direction multiplied by the strength. This is applied to the player's speed and the opposite vector is applied to the object the player is roped on to.

As an aside, dot products are crazy useful. They're used to determine which wall a player is likely to intend to run on: If the player jumps towards a wall, say on their right, they would naturally look away from the wall, to the left. A simple dot product and sign check can determine if the player is looking to the right or left of their movement direction and determine which wall they intend to start wallrunning on.

Aesthetically, there's a watermelon so that there's a destroyable prop. If the melon is destroyed, the rope unhooks. The actual rope graphic is just a textured line that scales its pattern by the stretch, so that it visibly stretches as you load it or scruntches as you get closer to the connect point.


So what does this tell us, as far as techniques go?

It means that big long swings, where you reel in the rope as you go, will accelerate you the best.
It means that a well set up swing can go basically forever, because of the perfect elasticity.

It also means there's a funny interaction with the way the floor is sticky in Source games, in that you don't leave the floor unless you jump or take a significant amount of force: If you stand on the ground, rope up vertically, and start reeling in your rope, you'll actually get launched into the air once the force overcomes the stickyness of the ground, because you've stretched your secretly-springy-rope.

Lastly, you can airstrafe to influence your swing and rotate the axis of your swing to navigate tricky scenarios.


The code is not the best. Frankly, I wrote it while trying to recreate TitanFall in GMod and I got overwhelmed with recreating all of the parts. I solved the interesting problems, like cloaking, wall running, a few guns, and so on, and just ran out of steam. So I took the parts that I could salvage (the grapple and the wallrun, because they were super polished) and decided to make a gamemode out of them. The dead-simplest gamemode, after Deathmatch, is Capture the Flag. Once those mechanics and that gamemode came together, the theming was obvious.

I slammed it together in about 2 hours, with another 6 of tweaks and bug hunting. A few years later, some nerd named Carson stumbled on my gamemode and crashed the server. Others made trailers and kill montages for it.

After this I got off my ass and started the TitanFall gamemode again from scratch. All I knew was that it was possible: TitanFall is a Source engine game, after all. And I think I succeeded in making a good knockoff!


Enjoy the gallery: