Mouse Drag & Mouse Joint for Dummies.

Discuss issues specific to the Flash/AS3 port of Box2D
gabin
Posts: 9
Joined: Sun Feb 10, 2008 4:25 pm

Mouse Drag & Mouse Joint for Dummies.

Postby gabin » Mon Feb 11, 2008 3:20 am

Hello, and thanks for all those amazing stuff that make my day... everyday (mostly night).
I'm quite new in it and i'm searching for a simple tutorial to add mouse drag on a body :

Used to look at the Test file function how it works on different examples (and other sources) , load a lot, read a lot, but they're must be something i don't understand
the basic : so I'm searching for a "mousejoint for dummies" simple little example of how to attach it to a body,
etc...(didn't found anything in the documentation as you must know...)

I'm french, and that could be an explanation...

Thanks a lot.

brainclog
Posts: 185
Joined: Mon Dec 17, 2007 10:11 pm
Contact:

Re: Mouse Drag & Mouse Joint for Dummies.

Postby brainclog » Mon Feb 11, 2008 8:43 am

Can you not get mouse dragging to work at all, or you are just having problems getting specific objects to drag?

I am not using this in a game, but I think all you have to do is call MouseDrag() in your game loop. It also depends on the GetBodyAtMouse function, and you have to be using the Input.as library.

If you want to only mouse drag certain bodies, you would need to modify MouseDrag so that it checks what the body is that is being clicked and does nothing if it's not "on the list". Also this might require some changes to GetBodyAtMouse, because I think it just returns the first body at the mouse that it finds. You might want to have it search for the first body on the drag list that it finds, in the case that there are two overlapping objects (such as a sensor if you are using them). That way it doesn't just return the sensor and not allow dragging.

gabin
Posts: 9
Joined: Sun Feb 10, 2008 4:25 pm

Re: Mouse Drag & Mouse Joint for Dummies.

Postby gabin » Mon Feb 11, 2008 9:23 am

thanks for your answer.
in fact i'm getting difficulties getting specific object to drag.

I'm copying all the function(drag...GetBodyAtMouse), get the input.as,
but it still doesn't run.
I'm searching for basic exemple to clear my mind, as a unique class file or something to have "new eyes" on it, leran from the beginning to find my mystake...
If someone could have it.

Beeryan
Posts: 17
Joined: Tue Jan 22, 2008 8:57 am

Re: Mouse Drag & Mouse Joint for Dummies.

Postby Beeryan » Mon Feb 11, 2008 11:44 am

Hi,

I'm not too sure if this will be helpful or not, but here's a mouseJoint test i was playing around with... basically drags the box with each mouse move using a mouseJoint... (need to have a movieClip with linkage "boxImage" for the userdata to work....)

Code: Select all

package {

   import flash.display.*;
   import flash.events.*;
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Dynamics.Joints.*;
   import Box2D.Dynamics.Contacts.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;

   public class mjTest extends Sprite {


      var bodyDef:b2BodyDef;
      var boxDef:b2BoxDef;
      var body:b2Body;
      var mj:b2MouseJoint;
      var md:b2MouseJointDef;


      public var m_world:b2World;
      public var m_timeStep:Number = 1/30;
      public var m_iterations:int = 10;


      public function mjTest() {

         var worldAABB:b2AABB = new b2AABB();
         worldAABB.minVertex.Set(0.0, 0.0);
         worldAABB.maxVertex.Set(1000.0, 800.0);
         var gravity:b2Vec2 = new b2Vec2(0, 300.0);
         var doSleep:Boolean = true;
         m_world = new b2World(worldAABB, gravity, doSleep);


         bodyDef = new b2BodyDef();
         boxDef = new b2BoxDef();


         boxDef.extents.Set(20,30);
         boxDef.friction = 1;
         boxDef.density = 1;
         boxDef.restitution = 0.3;
         bodyDef.isSleeping = true;
         bodyDef.position.Set(351,300);
         bodyDef.AddShape(boxDef);
         bodyDef.userData = new boxImage();
         bodyDef.userData.width = boxDef.extents.x * 2;
         bodyDef.userData.height = boxDef.extents.y * 2;
         addChild(bodyDef.userData);
         body = m_world.CreateBody(bodyDef);


         md = new b2MouseJointDef();
         md.body1 = m_world.m_groundBody;
         md.body2 = body;
         md.target.Set(350, 320);
         md.maxForce = 3000.0 * body.m_mass;
         md.timeStep = m_timeStep;
         mj = m_world.CreateJoint(md) as b2MouseJoint;
         body.WakeUp();



         addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
         stage.addEventListener(MouseEvent.MOUSE_MOVE, mJoint, false, 0, true);




      }
      function mJoint(e:MouseEvent):void {

         if (mj) {
            var p2:b2Vec2 = new b2Vec2(e.stageX, e.stageY);
            mj.SetTarget(p2);
         }
      }
      public function Update(e:Event):void {
         m_world.Step(m_timeStep, m_iterations);

         for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
            if (bb.m_userData is Sprite) {
               bb.m_userData.x = bb.m_position.x;
               bb.m_userData.y = bb.m_position.y;
               bb.m_userData.rotation = bb.m_rotation * (180/Math.PI);
            }
         }
      }
   }
}

gabin
Posts: 9
Joined: Sun Feb 10, 2008 4:25 pm

Re: Mouse Drag & Mouse Joint for Dummies.

Postby gabin » Mon Feb 11, 2008 5:45 pm

Thanks, it helps !

But they are two things i can't understand : the first is

Code: Select all

 if (bb.m_userData is Sprite) {
,

the second is that simple little word that i find in a lot of place wish is "as", >>>>
mj = m_world.CreateJoint(md) as b2MouseJoint;

What does it mean, it overload a instance/function ???
Nevers seen it in as2 ??

brainclog
Posts: 185
Joined: Mon Dec 17, 2007 10:11 pm
Contact:

Re: Mouse Drag & Mouse Joint for Dummies.

Postby brainclog » Mon Feb 11, 2008 6:01 pm

It is a type cast, so that the b2Joint type returned by the function is treated as a b2MouseJoint specifically.

shabasky
Posts: 24
Joined: Sun Aug 30, 2009 10:26 am

Re: Mouse Drag & Mouse Joint for Dummies.

Postby shabasky » Wed Sep 30, 2009 10:01 am

for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
if (bb.m_userData is Sprite) {
bb.m_userData.x = bb.m_position.x;
bb.m_userData.y = bb.m_position.y;
bb.m_userData.rotation = bb.m_rotation * (180/Math.PI);



Could you explain what is the purpose of this part? Is m_world.Step not enough for the mousejoint to refresh??

VengantMjolnir
Posts: 356
Joined: Tue Feb 03, 2009 2:05 pm
Location: Canada
Contact:

Re: Mouse Drag & Mouse Joint for Dummies.

Postby VengantMjolnir » Wed Sep 30, 2009 5:34 pm

The code you are referencing is somply to update the graphics with the physics object.

It is iterating over a linked list of b2Body(s) and checking if the userData is a sprite, if it is then it moves it to where the physics say it should go.


Return to “Flash”



Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 4 guests