9
\$\begingroup\$

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!

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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". \$\endgroup\$
    – user70223
    Commented Aug 26, 2015 at 20:55
  • \$\begingroup\$ 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. \$\endgroup\$
    – bummzack
    Commented Aug 27, 2015 at 8:32
  • \$\begingroup\$ But I've noticed that the curves can be at different angles to one another; how is this being achieved? \$\endgroup\$
    – user70223
    Commented Aug 27, 2015 at 10:44
  • \$\begingroup\$ Add some random offsets to make it look different every time? \$\endgroup\$
    – bummzack
    Commented Aug 27, 2015 at 13:05
  • \$\begingroup\$ 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) \$\endgroup\$
    – bummzack
    Commented Aug 27, 2015 at 13:48
10
\$\begingroup\$

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

\$\endgroup\$
11
  • \$\begingroup\$ "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?) \$\endgroup\$
    – user70223
    Commented Aug 23, 2015 at 21:04
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kyranstar
    Commented Aug 23, 2015 at 21:07
  • \$\begingroup\$ 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? \$\endgroup\$
    – user70223
    Commented Aug 23, 2015 at 21:14
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kyranstar
    Commented Aug 23, 2015 at 21:19
  • \$\begingroup\$ 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? \$\endgroup\$
    – user70223
    Commented Aug 23, 2015 at 21:24

You must log in to answer this question.