# Basic Box2D rope-chain: 2.1a version

I was playing with the web version of Cut the Rope when it came to my mind the idea of showing you how to create a cut the rope game.

I knew about two years ago I wrote a tutorial about a basic Box2D rope and I wanted to start from here, but unfortunately it does not work with 2.1a version.

So at the moment I made a Box2D rope-chain version working with 2.1a, whose theory is based on the same concept of the original post with intensive use of revolute joints.

```package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
public class Main extends Sprite {
private var worldScale:Number=30;
private var world:b2World;
// this is the ball which I'll join at the end of the chain/rope
private var steelBall:b2Body;
public function Main() {
// number of links forming the rope
// according to the number of links, I am setting the length of a single chain piace
// creation of a new world
world=new b2World(new b2Vec2(0,10),true);
debugDraw();
// ceiling polygon shape
var polygonShape:b2PolygonShape = new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,10/worldScale);
// ceiling fixture;
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.density=1;
fixtureDef.friction=1;
fixtureDef.restitution=0.5;
fixtureDef.shape=polygonShape;
// ceiling body
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(320/worldScale,0);
// ceiling creation;
var wall:b2Body=world.CreateBody(bodyDef);
wall.CreateFixture(fixtureDef);
polygonShape.SetAsBox(5/worldScale,chainLength/worldScale);
fixtureDef.density=1;
fixtureDef.shape=polygonShape;
bodyDef.type=b2Body.b2_dynamicBody;
for (var i:Number=0; i<=links; i++) {
bodyDef.position.Set(320/worldScale,(chainLength+2*chainLength*i)/worldScale);
if (i==0) {
}
else {
}
}
// attaching the ball at the end of the rope
var circleShape:b2CircleShape=new b2CircleShape(1);
fixtureDef.shape=circleShape;
steelBall=world.CreateBody(bodyDef);
steelBall.CreateFixture(fixtureDef);
}
// this is the core of the script: despite the official docs which suggest to use Initialize,
private function revoluteJoint(bodyA:b2Body,bodyB:b2Body,anchorA:b2Vec2,anchorB:b2Vec2):void {
var revoluteJointDef:b2RevoluteJointDef=new b2RevoluteJointDef();
revoluteJointDef.localAnchorA.Set(anchorA.x,anchorA.y);
revoluteJointDef.localAnchorB.Set(anchorB.x,anchorB.y);
revoluteJointDef.bodyA=bodyA;
revoluteJointDef.bodyB=bodyB;
world.CreateJoint(revoluteJointDef);
}
private function debugDraw():void {
var debugDraw:b2DebugDraw = new b2DebugDraw();
var debugSprite:Sprite = new Sprite();
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(worldScale);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);
debugDraw.SetFillAlpha(0.5);
world.SetDebugDraw(debugDraw);
}
private function sphereImpulse(e:MouseEvent):void {
// just apply an impulse to the sphere
steelBall.ApplyImpulse(new b2Vec2(-50+Math.random()*100,-150),steelBall.GetWorldCenter());
}
private function update(e:Event):void {
world.Step(1/30,10,10);
world.ClearForces();
world.DrawDebugData();
}
}
}```

The key lies in the `revoluteJoint` function which acts directly on `b2RevoluteJointDef` properties rather than unsing Initialize method which is not that efficient.

This is the result:

Click on the stage to add a random impulse to the sphere. The number and length of links is random, so refresh the page to see a different rope.

Download the source code and see you soon with a Cut the Rope tutorial.

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 10000000
// 2 Cars
// 2048
// Avoider
// Ballz
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Breakout
// Bricks
// Columns
// CubesOut
// Dots
// DROP'd
// Dudeski
// Eskiv
// Filler
// Fling
// Globe
// HookPod
// Hundreds
// InkTd
// Iromeku
// Lumines
// Magick
// MagOrMin
// Maze
// Memdot
// Nano War
// Nodes
// o:anquan
// Ononmin
// Pacco
// Phyballs
// Platform
// Poker
// Pool
// Poux
// Pudi
// qomp
// Racing
// Renju
// SameGame
// Security
// Sling
// Slingy
// Sokoban
// Splitter
// Sproing
// Stack
// Stairs
// Stringy
// Sudoku
// Tetris
// Threes
// Toony
// Turn
// TwinSpin
// vvvvvv
// Wordle
// Worms
// Yanga
// Zhed
// zNumbers