Mouse Dragging.

Discuss issues specific to the Flash/AS3 port of Box2D
kestorr
Posts: 32
Joined: Tue May 22, 2012 2:37 am

Mouse Dragging.

Postby kestorr » Sat Jun 02, 2012 11:44 pm

Hello there.
So im not sure how to drag objects after the mouse on click.
Coudl you guys post me an exemple after my code so i better understand the method.

This is in box2d 2.02 version

Code: Select all


var metalShapeDef:b2PolygonDef=new b2PolygonDef();
         metalShapeDef.vertexCount=3;
         b2Vec2(metalShapeDef.vertices[0]).Set(0/Vals.RATIO, 0/Vals.RATIO);
           b2Vec2(metalShapeDef.vertices[1]).Set(0/Vals.RATIO, -100/Vals.RATIO);
           b2Vec2(metalShapeDef.vertices[2]).Set(100/Vals.RATIO, 0/Vals.RATIO);
         metalShapeDef.density=1;
         metalShapeDef.friction=0.9;
         metalShapeDef.restitution=0.1;
         
         var metalBodyDef:b2BodyDef=new b2BodyDef();
         metalBodyDef.position.Set(location.x/Vals.RATIO, location.y/Vals.RATIO);
         
         var metalBody:b2Body;
         metalBody=Vals.world.CreateBody(metalBodyDef);
         metalBody.CreateShape(metalShapeDef);
         metalBody.SetMassFromShapes();



Thank you.

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Mouse Dragging.

Postby jayther » Sun Jun 03, 2012 12:12 am

There should be a mouse drag code in all of the tests in the PhysTest.fla that comes with the Box2DAS3 2.0. They should be in the TestBed/Test.as file.

kestorr
Posts: 32
Joined: Tue May 22, 2012 2:37 am

Re: Mouse Dragging.

Postby kestorr » Sun Jun 03, 2012 1:38 am

I think i learned how to create Mouse Joints.
This is the code exemple:

Code: Select all


var mouseJointDef:b2MouseJointDef=new b2MouseJointDef();
         mouseJointDef.body1 = Vals.world.GetGroundBody();
         mouseJointDef.body2 = metalBody;
         var bodypos:b2Vec2 = metalBody.GetWorldCenter();
         mouseJointDef.target.Set(bodypos.x, bodypos.y);
         mouseJointDef.collideConnected=true;
         mouseJointDef.maxForce = 300 * metalBody.GetMass();
                        var mouseJoint:b2MouseJoint;
              mouseJoint=Vals.world.CreateJoint(mouseJointDef) as b2MouseJoint;
         

                        // And in the update i add this:
                        var currentmousepos:b2Vec2 = new b2Vec2(mouseX / Vals.RATIO, mouseY / Vals.RATIO);
         mouseJoint.SetTarget(currentmousepos);





But im not sure where to add it. To learn box2d i have been following some tutorials from this person here: http://www.youtube.com/watch?v=MGX8GOmz ... plpp_video

So its like this, I spawn objects in the world from this objectActor class which is liek this:

Code: Select all


public class MetalActor extends Actor
   {

      public function MetalActor(parent:DisplayObjectContainer, location:Point, initVel:Point)
      {
         var metalSprite=new Metal();
         parent.addChild(metalSprite);
         
         var metalShapeDef:b2PolygonDef=new b2PolygonDef();
         metalShapeDef.vertexCount=3;
         b2Vec2(metalShapeDef.vertices[0]).Set(0/Vals.RATIO, 0/Vals.RATIO);
           b2Vec2(metalShapeDef.vertices[1]).Set(0/Vals.RATIO, -100/Vals.RATIO);
           b2Vec2(metalShapeDef.vertices[2]).Set(100/Vals.RATIO, 0/Vals.RATIO);
         metalShapeDef.density=1.7;
         metalShapeDef.friction=0.9;
         metalShapeDef.restitution=0.1;
         
         var metalBodyDef:b2BodyDef=new b2BodyDef();
         metalBodyDef.position.Set(location.x/Vals.RATIO, location.y/Vals.RATIO);
         metalBodyDef.massData.mass=30;
         
         var metalBody:b2Body;
         metalBody=Vals.world.CreateBody(metalBodyDef);
         metalBody.CreateShape(metalShapeDef);
         metalBody.SetMassFromShapes();
         
         
         var velocityVector:b2Vec2=new b2Vec2(initVel.x/Vals.RATIO, initVel.y/Vals.RATIO);
         metalBody.SetLinearVelocity(velocityVector);
         
         
         
         super(metalBody, metalSprite);
      }

   }





And the the Actor class is this:

Code: Select all


public class Actor extends EventDispatcher
   {
      protected var _body:b2Body;
      protected var _costume:DisplayObject;
      

      public function Actor(myBody:b2Body, myCostume:DisplayObject)
      {
         _body=myBody;
         _costume=myCostume;
         
         updateMyLook();
      }
      public function updateNow():void
      {
         updateMyLook();
         childSpecificUpdating();
      }
      
      protected function childSpecificUpdating():void
      {
         if(_costume.y>_costume.stage.stageHeight)
         {
            dispatchEvent(new ObjectEvent(ObjectEvent.OBJECT_OFF_SCREEN));
         }
         
      }
      public function destroy():void
      {
         
         cleanUpBeforeRemoving();
         
         
         _costume.parent.removeChild(_costume);
         
         
         Vals.world.DestroyBody(_body);
      }
      protected function cleanUpBeforeRemoving():void
      {
         
      }
      private function updateMyLook():void
      {
         _costume.x=_body.GetPosition().x * 30;
         _costume.y=_body.GetPosition().y * 30;
         _costume.rotation=_body.GetAngle() * 180/Math.PI;
      }

   }



And then on my main class the objects are spawned with the following code:

Code: Select all

var metalActor=new MetalActor(this, new Point(300, 80), new Point(-30, -3));


which is then inserted into an array and the array is updated in the ENTER_FRAME function.


SO im not sure where i could get that mouse joint code so it would work.
Could you help me?

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Mouse Dragging.

Postby jayther » Sun Jun 03, 2012 2:00 am

You have to keep a variable/reference of the mouse joint so you can destroy the mouse joint when you release the mouse button. Then on an MOUSE_DOWN function, you check if there's a body there (needs to be MOUSE_DOWN and not CLICK, since a CLICK event only fires when the button is pressed then released). If there's a body there, create the mouse joint. If not, keep the mouse joint null.

Here's the code that involves mouse dragging from the Box2DFlashAS3 2.0.0 Test.as file:

Code: Select all

      
      
      
      //======================
      // Mouse Drag
      //======================
      public function MouseDrag():void{
         // mouse press
         if (Input.mouseDown && !m_mouseJoint){
            
            var body:b2Body = GetBodyAtMouse();
            
            if (body)
            {
               var md:b2MouseJointDef = new b2MouseJointDef();
               md.body1 = m_world.m_groundBody;
               md.body2 = body;
               md.target.Set(mouseXWorldPhys, mouseYWorldPhys);
               md.maxForce = 300.0 * body.m_mass;
               md.timeStep = m_timeStep;
               m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
               body.WakeUp();
            }
         }
         
         
         // mouse release
         if (!Input.mouseDown){
            if (m_mouseJoint)
            {
               m_world.DestroyJoint(m_mouseJoint);
               m_mouseJoint = null;
            }
         }
         
         
         // mouse move
         if (m_mouseJoint)
         {
            var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
            m_mouseJoint.SetTarget(p2);
         }
      }
      
      
      
      //======================
      // Mouse Destroy
      //======================
      public function MouseDestroy():void{
         // mouse press
         if (!Input.mouseDown && Input.isKeyPressed(68/*D*/)){
            
            var body:b2Body = GetBodyAtMouse(true);
            
            if (body)
            {
               m_world.DestroyBody(body);
               return;
            }
         }
      }
      
      
      
      //======================
      // GetBodyAtMouse
      //======================
      private var mousePVec:b2Vec2 = new b2Vec2();
      public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body{
         // Make a small box.
         mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
         var aabb:b2AABB = new b2AABB();
         aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
         aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
         
         // Query the world for overlapping shapes.
         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;
      }

billyNugz
Posts: 4
Joined: Wed Sep 05, 2012 7:49 am

Re: Mouse Dragging.

Postby billyNugz » Tue Oct 02, 2012 7:08 am

Hey guys can you point me in the right direction of how to do this with 2.0.2 as Query.world() doesn't exist. I have tryed setting up a call back function but using QueryAABB I will show you what Im doing below. Some things are commented out as Im just trying to trace things out and get this thing working. Thanks for any and all help.

Mouse click listener:

Code: Select all

public function on_mouse_down(evt:MouseEvent):void {
         var body:b2Body = GetBodyAtMouse();
         trace("on_mouse_down");
         if (body) {
            trace("on_mouse_down_IF");//wont fire
            var mouse_joint:b2MouseJointDef = new b2MouseJointDef;
            mouse_joint.bodyA = world.GetGroundBody();
            mouse_joint.bodyB = body;
            
            mouse_joint.target.Set(mouseX/pixels_in_a_meter, mouseY/pixels_in_a_meter);
            mouse_joint.maxForce = 10000;
            mouseJoint = world.CreateJoint(mouse_joint) as b2MouseJoint;
         }
      }

Create the AABB

Code: Select all

public function GetBodyAtMouse(includeStatic:Boolean=false)//:b2Body
      {
         trace("GetBodyAtMouse");
         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 body:b2Body = null;
            //var fixture:b2Fixture;
         
         world.QueryAABB(GetBodyCallback,aabb);
         //return body;
      }

The call back function:

Code: Select all

public function GetBodyCallback(fixture:b2Fixture)//:Boolean
        {
            trace("GetBodyCallback");
               var shape:b2Shape = fixture.GetShape();
            trace("type =" + fixture.GetBody().GetType())
               if (fixture.GetBody().GetType() != b2Body.b2_staticBody)
               {
               trace("GetBodyCallback_IF");
                     var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(), mousePVec);
                     if (inside)
                     {
                        var body = fixture.GetBody();
                        //return true;
                     }
               }
               //return true;
            }

And when my world updates:

Code: Select all

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);
         }
         /*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 * pixels_in_a_meter;
               bb.m_userData.y = bb.GetPosition().y * pixels_in_a_meter;
               bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
            }
         }*/


Return to “Flash”



Who is online

Users browsing this forum: Bing [Bot] and 3 guests