Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Basic Box2D rope

It’s time to create a rope with Box2d.

To start defining a rope, we’ll start from something similar: a chain. A chain is a series of two or more connected links. The smaller and closer the links, the more flexible the chain. So we can define a rope as a “perfect chain”, with infinite links.

In a Box2D simulation, ropes as we know them aren’t possible. But we can construct chains, and according to the number of links they can approximate a rope quite well.

Obviously, the higher the number of links, the more accurate the simulation, the slowest and CPU intensive the simulation.

I found 10 links are a good compromise between simulation and performance.

Every link is (guess what?) linked to the previous one with a revolute joint. Refer to Box2D joints: Revolute Joint if you need more information about revolute joints.

The core of the whole script is the link variable. I simply “save” the reference of the last chain placed on the stage and create the joint between the last chain placed and the one I am going to place.

So I’m having the first link with a joint with the ceiling, the second one with a joint on the first one, the third with a joint on the second and so on.

And this is the result:

Every object is draggable, except the ceiling.

Download the source code, Box2D library included, and enjoy.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (36 votes, average: 4.33 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 27 comments

  1. Yarden Refaeli

    on October 6, 2009 at 12:35 am

    YOU ROCK!
    how about the sweet old school game with the cup and the ball?

    A ball is connected to the bottom of a cup, and you need to play with the cup till the ball get inside it… I dunno if you know what I mean, the game can be found in many variations.

    Thanks!
    -yarden

  2. dim

    on October 6, 2009 at 8:24 am

    hell yeah, i like this rope :)

  3. maw

    on October 6, 2009 at 1:21 pm

    ergh…. sorry… but its look like a chain, not a rope… imho elastic nodes can help in simulation of rope

  4. Ryan Williams

    on October 6, 2009 at 2:41 pm

    Surely it just needs more segments to be more convincing, maw? It certainly moves like rope.

  5. Jjet42

    on October 6, 2009 at 6:38 pm

    Hay why don’t you make a tutorial for storing user data on their machine? That way the game remebers where they left off or their personal settings. Maybe even remember how they play and become harder. I heard its easy they are called Flash Cookies or something like that.

  6. Monkios

    on October 6, 2009 at 7:43 pm

    This, with elasticity, would be awesome.

  7. makc

    on October 6, 2009 at 8:26 pm

    The problem with this rope is when the box weights too much or you pull it too hard, the rope breaks down. Therefore you need to add distance joints. This was discussed on box2d forums looong time ago, see http://www.box2d.org/forum/viewtopic.php?p=10691#p10691

  8. oliver_l1

    on October 6, 2009 at 9:45 pm

    Hi Emanuele,

    this has nothing to do with box2d.when i enter your site i get a html script virus warning via antivir.

    Check your site!

  9. Matt

    on October 6, 2009 at 11:57 pm

    Any suggestions for using movieclips as the rope segments. I’m trying to work them into the code but then nothing displays.

    Awesome class BTW.

  10. linto

    on October 12, 2009 at 8:44 am

    Hi Emanuele,

    I don’t know whether this is the right place to post this question.

    How will we implement an object whose one side is transparent ( objects hitting that side will pass through) and the other side is not (objects will bounce hitting that side). The scenario is implementation of a pipe through which a ball rolls… but objects hitting the outer side of the pipe should not bounce back….objects should move as if they are passing below/above that pipe..

    Thanks in advance
    LINTO

  11. zap

    on October 13, 2009 at 2:40 pm

    Its nice but this is better…

    example:
    http://www.letsmoondance.nl/

    source:
    http://www.box2d.org/forum/viewtopic.php?f=6&t=2094

  12. Anthony

    on October 19, 2009 at 9:55 pm

    Maw, maybe if you read the tut. It said lets start with a chain…

  13. Andy

    on October 20, 2009 at 4:43 am

    the rope acts as if there is no weight on the end of it.

  14. Jaleel

    on January 25, 2010 at 11:08 am

    you have created lots of useful programs great. i would suggest you to create an menu for these programs so that we can quickly reach the desired one.

  15. iPhone Dev » Rope Simulate with Cocos2d and Box2d

    on February 6, 2010 at 5:46 am

    [...] Rope Simulate with Cocos2d and Box2d February 5th, 2010 admin Leave a comment Go to comments This post presents a good idea on how to simulate a rope using the distance joint in Box2d . I re-implemented it using  Cocos2d.  If you have got problems, please refer to that post for more details or leave a comment. In addition, you can also find another way of simulating a rope using Box2d from here. [...]

  16. florian

    on July 6, 2010 at 12:44 pm

    try this one, I approximated the rope as a parabola. Looks like more natural but it’s also complicated.

    http://numanim.com/index.php?option=com_content&view=article&id=48&Itemid=57&lang=fr

  17. florian

    on July 6, 2010 at 12:46 pm

    I forgot to say, to see the rope, pull the ring out

  18. ScanPlayGames

    on December 28, 2010 at 1:25 am

    Hello,

    I am working on a game and was in need of Box2D rope. Your sample is great here but it seems to be using an older build of Box2D where several of the above functions have been deprecated.

    If anyone wants to see a full, working source for this example using the latest Box2D for AS3, I posted the updated code here:
    click for rope

  19. harilalkm

    on January 4, 2012 at 7:05 am

    Hi, it’s great tutorial.
    I have a doubt . i need to change the position of the ceiling instead of rope?

    i have tried differnt method.. Am new Box2d ..

  20. harilalkm

    on January 5, 2012 at 8:39 am

    hi,

    I have tried it using 2.1a.I manged to move the ceiling also. but the rope don’t stick together.
    This is my source
    http://www.kreataglobal.in/sample/escape/test.zip
    Can you help meto fing what went wrong

  21. Cocos2d

    on February 23, 2012 at 2:01 am

    Hi Emanuele, great work. I am trying to get a similar rope in Cocos2D Box2D. I don’t know what I am doing wrong. Can you please help me. Thanks in Advance!

  22. Anders

    on March 28, 2012 at 12:07 pm

    Hey, what exactly is making the rope just hang straight down? My rope sway back and forth.

  23. Anders

    on March 28, 2012 at 4:25 pm

    Disregard my question, i’m a retard :P

  24. godslave

    on May 9, 2012 at 9:52 am

    Hi, I’m new with box2d and libgdx and i’m trying to build a path like in the Stick stunt biker game.At first I tried to do this with a strong rope with polygon shape using prismaticJoint and distance joint but it’s not working as i expected. then i tried to do with chainShape and failed again to make the path curve smooth for my bike.for bike i follow the tutorial “creation-of-a-box2d-car-with-10-customizable-parameters”.
    Any suggestion on how could i build the path.

  25. Rope Simulation with Cocos2d and Box2d | iPhone Dev

    on October 31, 2012 at 4:44 am

    [...] This post presents a good idea on how to simulate a rope using the distance joint in Box2d . I re-implemented it using  Cocos2d.  If you have got problems, please refer to that post for more details or leave a comment. In addition, you can also find another way of simulating a rope using Box2d from here. [...]

  26. libGDX: Hanging Rope [duplicate] | BlogoSfera

    on March 16, 2013 at 12:04 pm

    [...] but no enough explanation given to use it.Can any body help me to make a rope like this one. Hanging Rope in Box2dI also tried to search for some good libGDX book for android but couldn’t find.your help is [...]

  27. danadrianmirea

    on May 31, 2013 at 9:08 pm

    Hi,

    Thank you for this.
    This is exactly what I was looking for, I am new with Box2D and couldn’t accomplish it on my own.

    Regards,
    Adrian

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×