Quantcast
Viewing all articles
Browse latest Browse all 10

Weld joint in box2d in javascript

Weld joint in box2d
The weld joint can be used to join to bodies tightly at a common point. Here is an example

A weld joint is created like this

//create distance joint between b and c
var joint_def = new b2WeldJointDef();
joint_def.bodyA = b;
joint_def.bodyB = c;

//connect the centers - center in local coordinate - relative to body is 0,0
joint_def.localAnchorA = new b2Vec2(-0.5, -0.5);
joint_def.localAnchorB = new b2Vec2(0.5, 0.5);

//difference in angle of each body
joint_def.referenceAngle = 0 * Math.PI / 3;

//add the joint to the world
world.CreateJoint(joint_def);

The joint definition needs to know the bodies to connect and the coordinates on each of them where the weld is to be done. It also has a referenceAngle parameter that defines the angle between the 2 bodies after being welded. Play with the referenceAngle to see the effect.
Source

/**
Weld Joint in box2d in javascript
Silver Moon (m00n.silv3r@gmail.com)
*/
var b2Vec2 = Box2D.Common.Math.b2Vec2
, b2AABB = Box2D.Collision.b2AABB
, b2BodyDef = Box2D.Dynamics.b2BodyDef
, b2Body = Box2D.Dynamics.b2Body
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
, b2Fixture = Box2D.Dynamics.b2Fixture
, b2World = Box2D.Dynamics.b2World
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
, b2DebugDraw = Box2D.Dynamics.b2DebugDraw
, b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef
, b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef
, b2Shape = Box2D.Collision.Shapes.b2Shape
;

var world;
var ctx;
var canvas_width;
var canvas_height;
var mouse_pressed = false;
var mouse_joint = false;
var mouse_x, mouse_y;

//box2d to...

Read full post here
Weld joint in box2d in javascript


Viewing all articles
Browse latest Browse all 10

Trending Articles