Need help with BOX2D from expert

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);