# Need help with BOX2D from expert

**URL:** https://forum.kirupa.com/t/need-help-with-box2d-from-expert/292498
**Category:** Uncategorized
**Created:** [July 14, 2009, 11:18pm UTC](https://forum.kirupa.com/t/need-help-with-box2d-from-expert/292498 "2009-07-14T23:18:40Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![muzn](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@muzn](https://forum.kirupa.com/u/muzn)
#### Post date: [July 14, 2009, 11:18pm UTC](https://forum.kirupa.com/t/need-help-with-box2d-from-expert/292498/1 "2009-07-14T23:18:40Z")

</div>

Hi guys,

im really new to box2d n i alot of stuffs that confusing me. Im trying to apply the dragging object but doesnt seems to work.

can anyone show me what the mistake in my code…

// Classes used in this example  
import Box2D.Dynamics._;  
import Box2D.Collision._;  
import Box2D.Collision.Shapes._;  
import Box2D.Common.Math._;  
import Box2D.Dynamics.Joints.\*;

// Define constant  
var m\_iterations:int = 5;// define the iteration of simulation  
var m\_timeStep:Number = 1.0/2.0;//define the time step of simulation  
var mousePVec:b2Vec2 = new b2Vec2();  
var mouseJoint:b2MouseJoint;  
var real\_x\_mouse:Number;  
var real\_y\_mouse:Number;  
var pixels\_in\_a\_meter = 30;  
var body:b2Body;  
var bodyDef:b2BodyDef;  
var boxDef:b2PolygonDef;

bodyDef = new b2BodyDef();  
bodyDef.position.Set(8.5, 13.5);  
boxDef = new b2PolygonDef();  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\*  
\*Step 1. Construct world object for simulation  
\*  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

// Creat world AABB  
var worldAABB:b2AABB = new b2AABB();  
worldAABB.lowerBound.Set(-100.0, -100.0);  
worldAABB.upperBound.Set(1000.0, 1000.0);

// Define the gravity vector  
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);

// Construct a world object  
var world:b2World = new b2World(worldAABB, gravity, true);

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\*  
\*Step 2. Setup the ground for simulation  
\*  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

// Construct a shape definition for the ground  
// It is use to store the physical properties of the ground  
var groundDefinition:b2PolygonDef = new b2PolygonDef();  
groundDefinition.SetAsBox(500.5, 24);  
groundDefinition.friction = 0.3;  
groundDefinition.density = 0;// we set the density to 0 means the ground is not movable

// Construct a body definition for the ground  
// It is use to store the position and rotation of the ground  
var groundBodyDefinition:b2BodyDef = new b2BodyDef();  
groundBodyDefinition.userData = ground\_mc;  
groundBodyDefinition.position.Set(ground\_mc.x, ground\_mc.y);  
groundBodyDefinition.angle = ground\_mc.rotation/180\*Math.PI;

// Construct a body object and add to the world object for simulation  
var groundBody:b2Body = world.CreateBody(groundBodyDefinition);  
groundBody.CreateShape(groundDefinition);  
groundBody.SetMassFromShapes();

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

var groundDefinitionL:b2PolygonDef = new b2PolygonDef();  
groundDefinitionL.SetAsBox(500.5, 24);  
groundDefinitionL.friction = 0.3;  
groundDefinitionL.density = 0;// we set the density to 0 means the ground is not movable

// Construct a body definition for the ground  
// It is use to store the position and rotation of the ground  
var groundBodyDefinitionL:b2BodyDef = new b2BodyDef();  
groundBodyDefinitionL.userData = ground\_mcL;  
groundBodyDefinitionL.position.Set(ground\_mcL.x, ground\_mcL.y);  
groundBodyDefinitionL.angle = ground\_mcL.rotation/180\*Math.PI;

// Construct a body object and add to the world object for simulation  
var groundBodyL:b2Body = world.CreateBody(groundBodyDefinitionL);  
groundBodyL.CreateShape(groundDefinitionL);  
groundBodyL.SetMassFromShapes();

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

var groundDefinitionR:b2PolygonDef = new b2PolygonDef();  
groundDefinitionR.SetAsBox(500.5, 24);  
groundDefinitionR.friction = 0.3;  
groundDefinitionR.density = 0;// we set the density to 0 means the ground is not movable

// Construct a body definition for the ground  
// It is use to store the position and rotation of the ground  
var groundBodyDefinitionR:b2BodyDef = new b2BodyDef();  
groundBodyDefinitionR.userData = ground\_mcR;  
groundBodyDefinitionR.position.Set(ground\_mcR.x, ground\_mcR.y);  
groundBodyDefinitionR.angle = ground\_mcR.rotation/180\*Math.PI;

// Construct a body object and add to the world object for simulation  
var groundBodyR:b2Body = world.CreateBody(groundBodyDefinitionR);  
groundBodyR.CreateShape(groundDefinitionR);  
groundBodyR.SetMassFromShapes();

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\*  
\*Step 3. Setup the ball for simulation  
\*  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

// Construct a shape definition for the ball  
// It is use to store the physical properties of the ball  
var ballDefinition = new b2CircleDef();  
ballDefinition.radius = 60;  
ballDefinition.density = 6;  
ballDefinition.friction = 0.3;  
ballDefinition.restitution = 0.3;// We set the restitution properties to control the recoil of the object. Higher means more elastic

// Construct a body definition for the ball  
// It is use to store the position and rotation of the ball  
var ballBodyDefinition:b2BodyDef = new b2BodyDef();  
ballBodyDefinition.userData = ball\_mc;  
ballBodyDefinition.position.Set(ball\_mc.x, ball\_mc.y);  
ballBodyDefinition.angle = ball\_mc.rotation/180\*Math.PI;

// Construct a body object and add to the world object for simulation  
var ballBody:b2Body = world.CreateBody(ballBodyDefinition);  
ballBody.CreateShape(ballDefinition);  
ballBody.SetMassFromShapes();  
addChild(ballBodyDefinition.userData)

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

var ballDefinition2 = new b2CircleDef();  
ballDefinition2.radius = 60;  
ballDefinition2.density = 3.0;  
ballDefinition2.friction = 0.21;  
ballDefinition2.restitution = 0.5;// We set the restitution properties to control the recoil of the object. Higher means more elastic

// Construct a body definition for the ball  
// It is use to store the position and rotation of the ball  
var ballBodyDefinition2:b2BodyDef = new b2BodyDef();  
ballBodyDefinition2.userData = ball\_mc2;  
ballBodyDefinition2.position.Set(ball\_mc2.x, ball\_mc2.y);  
ballBodyDefinition2.angle = ball\_mc2.rotation/180\*Math.PI;

// Construct a body object and add to the world object for simulation  
var ballBody2:b2Body = world.CreateBody(ballBodyDefinition2);  
ballBody2.CreateShape(ballDefinition2);  
ballBody2.SetMassFromShapes();  
addChild(ballBodyDefinition2.userData)

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\*  
\*Step 4. Setup the box for simulation  
\*  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

// Construct a shape definition for the box  
// It is use to store the physical properties of the box  
var boxDefinition = new b2PolygonDef();  
boxDefinition.SetAsBox(20, 20);  
boxDefinition.density = 1.0;  
boxDefinition.friction = 0.5;  
boxDefinition.restitution = .5;

// Construct a body definition for the box  
// It is use to store the position and rotation of the box  
var boxBodyDefinition:b2BodyDef = new b2BodyDef();  
boxBodyDefinition.userData = box\_mc;  
boxBodyDefinition.position.Set(box\_mc.x, box\_mc.y);  
boxBodyDefinition.angle = box\_mc.rotation/180\*Math.PI;

// Construct a body object and add to the world object for simulation  
var boxBody:b2Body = world.CreateBody(boxBodyDefinition);  
boxBody.CreateShape(boxDefinition);  
boxBody.SetMassFromShapes();

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\*  
\*Step 4. Setup the render loop  
\*  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

function on\_mouse\_down(e:MouseEvent):void {  
var body:b2Body = GetBodyAtMouse();  
if (body) {  
var mouse\_joint:b2MouseJointDef = new b2MouseJointDef;  
mouse\_joint.body1 = world.GetGroundBody();  
mouse\_joint.body2 = body;  
mouse\_joint.target.Set(mouseX/pixels\_in\_a\_meter, mouseY/pixels\_in\_a\_meter);  
mouse\_joint.maxForce = 10000;  
mouse\_joint.timeStep = m\_timeStep;  
mouseJoint = world.CreateJoint(mouse\_joint) as b2MouseJoint;  
}  
}

function on\_mouse\_up(evt:MouseEvent):void {  
if (mouseJoint) {  
world.DestroyJoint(mouseJoint);  
mouseJoint = null;  
}  
}

function GetBodyAtMouse(includeStatic:Boolean=false):b2Body {  
real\_x\_mouse = (stage.mouseX)/pixels\_in\_a\_meter;  
real\_y\_mouse = (stage.mouseY)/pixels\_in\_a\_meter;  
mousePVec.Set(real\_x\_mouse, real\_y\_mouse);  
var aabb:b2AABB = new b2AABB();  
aabb.lowerBound.Set(real\_x\_mouse - 0.001, real\_y\_mouse - 0.001);  
aabb.upperBound.Set(real\_x\_mouse + 0.001, real\_y\_mouse + 0.001);  
var k\_maxCount:int = 10;  
var shapes:Array = new Array();  
var count:int = world.Query(aabb, shapes, k\_maxCount);  
var body:b2Body = null;  
for (var i:int = 0; i \<count; ++i) {  
if (shapes\*.m\_body.IsStatic() == false || includeStatic) {  
var tShape:b2Shape = shapes\* as b2Shape;  
var inside:Boolean = tShape.TestPoint(tShape.m\_body.GetXForm(), mousePVec);  
if (inside) {  
body = tShape.m\_body;  
break;  
}  
}  
}  
return body;  
}

function render(e:Event):void {

```
// calulate the simulation
world.Step(m_timeStep, m_iterations);

if (mouseJoint) {
            var mouseXWorldPhys = mouseX/pixels_in_a_meter;
            var mouseYWorldPhys = mouseY/pixels_in_a_meter;
            var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
            mouseJoint.SetTarget(p2);
        }

// Go through body list and update sprite positions/rotations
for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next) {
	if (bb.m_userData is Sprite) {
		bb.m_userData.x = bb.GetPosition().x;
		bb.m_userData.y = bb.GetPosition().y;
		bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
	}
}

```

}

// Add the Event.ENTER\_FRAME listener  
addEventListener(Event.ENTER\_FRAME,render);  
stage.addEventListener(MouseEvent.MOUSE\_DOWN, on\_mouse\_down);  
stage.addEventListener(MouseEvent.MOUSE\_UP, on\_mouse\_up);
