9

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

enter image description here

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!

minseong
  • 411
  • 1
  • 4
  • 14

2 Answers2

11

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: enter image description here

Kyranstar
  • 334
  • 2
  • 12
  • "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?) – minseong 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. – Kyranstar 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? – minseong 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. – Kyranstar 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? – minseong Aug 23 '15 at 21:24
  • The reason that it's grey is that it's "sleeping" (sleep is just an optimization where you don't update an object when it's been at about zero velocity for a while), which is because I set it to having "infinite mass." Basically, I store mass by not storing mass, but the inverse of the mass. This way, I can store 0 as the inverse mass, and the object will never move. I store inverse mass because you may want to represent objects with infinite mass (impossible if you just store mass), but you never really want to represent objects with 0 mass. – Kyranstar Aug 23 '15 at 21:28
  • This concept is covered in that physics engine tutorial I linked in my answer. It's a really good read and I definitely recommend it even if you're not planning on implementing a full engine! – Kyranstar Aug 23 '15 at 21:29
  • Does each spring connection need its own object? Or is there a way for the nodes to directly influence one another? – minseong Aug 26 '15 at 16:26
  • In my engine, I have joint objects (springs are a type of joint) that get updated every tick and exert forces on their two connections. You don't necessarily need this type of architecture, but it might be easier when you have multiple types of joints. – Kyranstar Aug 26 '15 at 18:46
  • Do you have a link for implementing string joints? That does seem like what I was after... – minseong Aug 26 '15 at 20:56
  • You could look at SpringJoint in my game library that I've been making. I linked it in my answer. That string joint is actually not built on a spring, but a distance joint. However, they aren't that different if you don't want a stretchy rope. – Kyranstar Aug 27 '15 at 00:28
3

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.

bummzack
  • 22,646
  • 5
  • 61
  • 86
  • 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". – minseong 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. – bummzack Aug 27 '15 at 08:32
  • But I've noticed that the curves can be at different angles to one another; how is this being achieved? – minseong Aug 27 '15 at 10:44
  • Add some random offsets to make it look different every time? – bummzack Aug 27 '15 at 13:05
  • Well, you'll probably need some sort of physics-simulation anyway for the movement-calculation. As I said: Modelling the rope completely inside the physics simulation (eg. a chain of connected nodes) can cause instabilities very fast. Less chain segments will increase stability, but then you need to smooth out your rope (eg. use a spline/bezier) or fake it completely. But give it a try, and you'll see. The easiest approach of course is to use an existing physics-engine... (eg. Box2D) – bummzack Aug 27 '15 at 13:48