# How to create rope?

I'd like to re-create this game, but I have no idea how to create the rope, or its influences on the ball.

Basically, there is a rope, "attached", at one end, to the mouse pointer, and at the other end attached to a massive ball. As the user moves their mouse, the rope and ball follow, as if swinging a flail or such in real life.

How would I go about creating a rope as in the above game?

I need to know the basic concepts and physics behind the creation of such a rope, not how to use a pre-existing library to create one. I'd like to be able to implement a rope from scratch, so need to know how it can be done.

Thanks!

The rope in your example is probably just a simple physics-joint that limits the distance of the ball to the mouse-cursor. These joints are typically called "Rope joint" or "Spring joint".

It seems like the ripples in the string are not based on an actual physics-simulation. Most likely this is a bezier-curve where the control points get further pushed away from the central axis when the ball is closer to the mouse-position.

While the accepted answer explains how to create a realistic rope within the physics-engine, it's sometimes preferable to have a much simpler solution (as outlined above). The simpler solution (with just one joint) greatly improves the stability of the simulation. Multiple connected joints that have large forces applied tend to jitter and can result in broken and erratically moving chains of joints.

Of course you can also combine both methods, by creating your rope as such:

1. A chain of nodes, connected by joints
2. A rope/spring joint from the first to the last node of the chain that ensures that you can't pull the chain further apart than a given max-distance (one of the main reasons these chains become unstable).

This will result in a more realistically moving chain (that can even react to other objects in the physics-engine) but will require more processing-power than just a spring-joint with "fake" chain-behavior.

• Thanks for pointing that out! How would the control points of the bezier be manipulated though, because the example does seem very realistic, not simply like a bezier where it is straightened and "squashed". Aug 26 '15 at 20:55
• I don't think it looks very realistic at all. It just adds some ripples to the rope, it doesn't bend or anything... to me it looks like it's just being straightened and squashed. Aug 27 '15 at 8:32
• But I've noticed that the curves can be at different angles to one another; how is this being achieved? Aug 27 '15 at 10:44
• Add some random offsets to make it look different every time? Aug 27 '15 at 13:05
• Okay, yes; it does seem like that's how it was done in the game, but actually (and this will seem bizarre to everyone) the programming language I'm using makes it easier to create a physics engine like Kyranstar's answer pointed out than drawing bezier curves. Aug 27 '15 at 13:08

Basically, you'll need to create a physics engine. A rope is conceptually a bunch of "nodes" connected together with springs.

Basically, every tick, each nodes will exert a force on the nodes above and below it proportional to their distance apart (or how far from their comfortable stretch length they are). If you want to attach a ball, just connect it with a spring joint to the bottom node of the rope.

This is a great tutorial for creating a physics engine, but it doesn't really go into constraints or joints.

Here is a tutorial covering basic springs, which should be all you need for a rope like that.

This is a rope created in my (very basic) physics engine. It obviously has some quirks (the weird vibration) but it's generally what you're looking for. When you draw the rope, just don't draw the nodes.

Edit:

To make a good looking rope, a "string joint" is actually much nicer looking and closer to your example. A string joint is basically a spring, but it only corrects the objects when they are too far apart (not when they're too close).

This is my rope with a string joint instead:

• "When you draw the rope, just don't draw the nodes." - made me laugh :) Thanks for taking my extreme noob-ishness into consideration. Would the ball just be another node then? Of a greater mass? (Does each node have a mass?) Aug 23 '15 at 21:04
• Yes they do! Generally the best way to intuitively calculate masses is to give each ball a density, and just multiply that by its area. You can always tweak this to get better results in your actual game. Aug 23 '15 at 21:07
• Normally, I'd implement this in an object-oriented language, with a class for a node, with properties density, mass etc., but I'm forced to use a language where OOP is nigh impossible; how would you set up nodes, etc., under these constraints? Aug 23 '15 at 21:14
• To be honest, I have very little experience with non -OOP languages. You could look at Chipmunk2D, which is a physics engine written in C. Obviously, you're going to need some way to store this data in a cohesive unit. Aug 23 '15 at 21:19
• Okay, thank you; last question, is the grey node a node in the sense of the others? What is special about it to cause it to remain still? Aug 23 '15 at 21:24