Mouse Joint
Out of the many joints that box2d has, one is mouse joint. It is not used in physics simulations, but helps to make the physics world interactive by making a body move towards a specific point, like the mouse coordinates for example. So objects can be picked up, dragged etc by the user.
Lets take an example
Pickup an object by clicking it and drag it. It will follow the mouse. The above is inside an iframe. Right click in it and click Reload Frame to refresh it.
Creating mouse joint
The mouse joint is created between 2 objects like this
//if joint exists then create
var def = new b2MouseJointDef();
def.bodyA = ground;
def.bodyB = body;
def.target = p;
def.collideConnected = true;
def.maxForce = 1000 * body.GetMass();
def.dampingRatio = 0;
mouse_joint = world.CreateJoint(def);
body.SetAwake(true);
The mouse joint needs 2 bodies. The first one is not used but must be there. In this example we use the ground as the first body. The second body is the body that moves towards the mouse point. The target point is the point towards which the body shall move.
The maxForce specifies the amount of force that is applied on the body to pull it towards the target point. Try playing with the values of these parameters and...
Read full post here
Playing with the mouse joint in box2d in javascript
Out of the many joints that box2d has, one is mouse joint. It is not used in physics simulations, but helps to make the physics world interactive by making a body move towards a specific point, like the mouse coordinates for example. So objects can be picked up, dragged etc by the user.
Lets take an example
Pickup an object by clicking it and drag it. It will follow the mouse. The above is inside an iframe. Right click in it and click Reload Frame to refresh it.
Creating mouse joint
The mouse joint is created between 2 objects like this
//if joint exists then create
var def = new b2MouseJointDef();
def.bodyA = ground;
def.bodyB = body;
def.target = p;
def.collideConnected = true;
def.maxForce = 1000 * body.GetMass();
def.dampingRatio = 0;
mouse_joint = world.CreateJoint(def);
body.SetAwake(true);
The mouse joint needs 2 bodies. The first one is not used but must be there. In this example we use the ground as the first body. The second body is the body that moves towards the mouse point. The target point is the point towards which the body shall move.
The maxForce specifies the amount of force that is applied on the body to pull it towards the target point. Try playing with the values of these parameters and...
Read full post here
Playing with the mouse joint in box2d in javascript