You need to setup some offset variables to keep track of how far from 0,0 you move the container clip.
By keeping track of this information, you can create your mouseJoints in the proper location by taking your mouse position and subtracting the offset amount.
In this case, I simply used the debugdraw sprite to hold this information (with the added benefit of the debug drawing being in the correct location too).
The changes I made are:
Anytime you take the mouse position, first subtract the debugdraw sprite's location.
Ex:
Code:
var mouseXWorldPhys=(mouseX - stdo.x)/pixels_in_a_meter;
When you call container_mc.startDrag(), add a stage listener for MOUSE_MOVE.
Add a MOUSE_MOVE handler that set's the debugdraw's location to container's location.
Code:
function on_mouse_move(evt:MouseEvent):void {
stdo.x = container_mc.x;
stdo.y = container_mc.y;
}
When you call container_mc.stopDrag(), remove the stage listener for MOUSE_MOVE.
I think that's all I changed. You can see the full code below.
Code:
//Creating world and mousejoint
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.*;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2DebugDraw;
var mouseIsInUse:Boolean = new Boolean(false);
var m_world:b2World;
var m_iterations:int=10;
var m_timeStep:Number=1/30;
var mousePVec:b2Vec2 = new b2Vec2();
var real_x_mouse:Number;
var real_y_mouse:Number;
var pixels_in_a_meter:Number=new Number(30);
var worldAABB:b2AABB = new b2AABB();
var gravity:b2Vec2=new b2Vec2(0.0,0);
var mouseJoint:b2MouseJoint;
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN, on_mouse_down);
stage.addEventListener(MouseEvent.MOUSE_UP, on_mouse_up);
worldAABB.lowerBound.Set(-500.0, -500.0);
worldAABB.upperBound.Set(500.0, 500.0);
m_world=new b2World(worldAABB,gravity,true);
function Update(e:Event):void {
m_world.Step(m_timeStep, m_iterations);
if (mouseJoint) {
var mouseXWorldPhys=(mouseX - stdo.x)/pixels_in_a_meter;
var mouseYWorldPhys=(mouseY - stdo.y)/pixels_in_a_meter;
var p2:b2Vec2=new b2Vec2(mouseXWorldPhys,mouseYWorldPhys);
mouseJoint.SetTarget(p2);
}
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
if (bb.m_userData is Sprite) {
bb.m_userData.x=bb.GetPosition().x*pixels_in_a_meter;
bb.m_userData.y=bb.GetPosition().y*pixels_in_a_meter;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
function on_mouse_down(evt:MouseEvent):void {
var body:b2Body=GetBodyAtMouse();
if (body) {
var mouse_joint:b2MouseJointDef=new b2MouseJointDef;
mouse_joint.body1=m_world.GetGroundBody();
mouse_joint.body2=body;
mouse_joint.target.Set((mouseX - stdo.x)/pixels_in_a_meter, (mouseY - stdo.y)/pixels_in_a_meter);
mouse_joint.maxForce=10000;
mouse_joint.timeStep=m_timeStep;
mouseJoint=m_world.CreateJoint(mouse_joint) as b2MouseJoint;
}
else
{
container_mc.startDrag(false,null);
stage.addEventListener(MouseEvent.MOUSE_MOVE, on_mouse_move);
}
}
function on_mouse_move(evt:MouseEvent):void {
stdo.x = container_mc.x;
stdo.y = container_mc.y;
}
function on_mouse_up(evt:MouseEvent):void {
if (mouseJoint) {
m_world.DestroyJoint(mouseJoint);
mouseJoint=null;
}
else
{
container_mc.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_MOVE, on_mouse_move);
}
}
function GetBodyAtMouse(includeStatic:Boolean=false):b2Body {
real_x_mouse = (stage.mouseX - container_mc.x)/pixels_in_a_meter;
real_y_mouse = (stage.mouseY - container_mc.y)/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=m_world.Query(aabb,shapes,k_maxCount);
var body:b2Body=null;
for (var i:int = 0; i < count; ++i) {
if (shapes[i].m_body.IsStatic()==false||includeStatic) {
var tShape:b2Shape=shapes[i] as b2Shape;
var inside:Boolean=tShape.TestPoint(tShape.m_body.GetXForm(),mousePVec);
if (inside) {
body=tShape.m_body;
break;
}
}
}
return body;
}
//Custom random function
function randomF(lowVal:int, highVal:int):int {
var randomNumer:int = (Math.round(Math.random() * (highVal - lowVal)) + lowVal);
return randomNumer;
}
//Vars
var numberOfProjects:Number = new Number(15);
//B2DebugDraw
var stdo:Sprite = new Sprite();
addChild(stdo);
var dbd:b2DebugDraw = new b2DebugDraw();
dbd.m_sprite = stdo;
dbd.m_drawScale = pixels_in_a_meter;
dbd.SetFlags(b2DebugDraw.e_shapeBit);
dbd.m_lineThickness = 2.0;
dbd.m_fillAlpha = 0.6;
m_world.SetDebugDraw(dbd);
//Creater of projects
completeHandler();
function completeHandler() {
trace("There are " + numberOfProjects + " projects.");
var i:int=0;
for (i; i<numberOfProjects; i++) {
trace("currently working on project " + i);
var cardMC:MovieClip = new MovieClip();
var urlRq:URLRequest=new URLRequest("http://www.exoticsandluxury.com/wp-content/uploads/2009/02/maserati-mc-12-front-detail-picture-150x100.jpg");
//Just a test image
var l:Loader = new Loader();
l.load(urlRq);
cardMC.addChild(l);
l.x = -75; //Set the loader image so it is in the
l.y = -50; //center of the movieclip
//creating b2box
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
bodyDef = new b2BodyDef();
if(i==0) //If it is the first image, load it in the center
{
bodyDef.position.x=500/pixels_in_a_meter;
bodyDef.position.y=500/pixels_in_a_meter;
}
else //Everyone else, be random
{
bodyDef.position.x=randomF(0,1000)/pixels_in_a_meter;
bodyDef.position.y=randomF(0,1000)/pixels_in_a_meter;
}
bodyDef.linearDamping=2;
bodyDef.angularDamping=2;
boxDef = new b2PolygonDef();
boxDef.SetAsBox( -75 / pixels_in_a_meter, -50 / pixels_in_a_meter);
boxDef.density=1.0;
boxDef.friction=0.5;
boxDef.restitution=0.2;
bodyDef.userData = cardMC;
body=m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
container_mc.addChild(bodyDef.userData);
}
}