Box2D Forums

It is currently Fri Oct 24, 2014 7:37 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon Feb 11, 2008 3:20 am 
Offline

Joined: Sun Feb 10, 2008 4:25 pm
Posts: 9
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.


Top
 Profile  
 
PostPosted: Mon Feb 11, 2008 8:43 am 
Offline

Joined: Mon Dec 17, 2007 10:11 pm
Posts: 185
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.


Top
 Profile  
 
PostPosted: Mon Feb 11, 2008 9:23 am 
Offline

Joined: Sun Feb 10, 2008 4:25 pm
Posts: 9
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.


Top
 Profile  
 
PostPosted: Mon Feb 11, 2008 11:44 am 
Offline

Joined: Tue Jan 22, 2008 8:57 am
Posts: 17
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:
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);
            }
         }
      }
   }
}


Top
 Profile  
 
PostPosted: Mon Feb 11, 2008 5:45 pm 
Offline

Joined: Sun Feb 10, 2008 4:25 pm
Posts: 9
Thanks, it helps !

But they are two things i can't understand : the first is
Code:
 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 ??


Top
 Profile  
 
PostPosted: Mon Feb 11, 2008 6:01 pm 
Offline

Joined: Mon Dec 17, 2007 10:11 pm
Posts: 185
It is a type cast, so that the b2Joint type returned by the function is treated as a b2MouseJoint specifically.


Top
 Profile  
 
PostPosted: Wed Sep 30, 2009 10:01 am 
Offline

Joined: Sun Aug 30, 2009 10:26 am
Posts: 24
Quote:
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??


Top
 Profile  
 
PostPosted: Wed Sep 30, 2009 5:34 pm 
Offline

Joined: Tue Feb 03, 2009 2:05 pm
Posts: 356
Location: Canada
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group