Box2DWeb_04之Joint
Joint ! 是用来链接body和body直接的对象!
总共有8种joint,分别使用在不同的目的的链接中!
Mouse joint
var canvas = document.getElementById("stage");
canvas.addEventListener("mousedown",mouseDown,true);
canvas.addEventListener("mouseup",mouseUp,true);
context = canvas.getContext("2d");
createWorld();
createWall();
createDistance();
// createRevolute();
// createPrismatic();
// createPulley();
// createGear();
// createLine();
// createWeld();
};
其实有一个我们一直在用!MouseJoint! So..........Next!
Distance joint一个距离链接,你可以把他看着在确定距离的一个无形的杆!用来约束两个body之间的距离!
在createDistance方法中我们创建两个ball并为其创建设置DistanceJoint效果如下:
代码:
var ball_A = createBall(10,50,50);
var ball_B = createBall(10,150,150);
var distanceJointDef = new b2DistanceJointDef();
distanceJointDef.Initialize(ball_A, ball_B, ball_A.GetWorldCenter(), ball_B.GetWorldCenter());
world.CreateJoint(distanceJointDef);};
Revolute joint
旋转Joint是指两个body共享一个锚点!并有拥有自由的旋转度 Like this!
var ball_A = createBall(30,100,100);
var ball_B = createBall(5,90,90,true);//把位置为中心偏上方并设置为静态
var revoluteJointDef = new b2RevoluteJointDef();
revoluteJointDef.Initialize(ball_A, ball_B, ball_B.GetWorldCenter());//并以中间的ball的中心作为锚点
world.CreateJoint(revoluteJointDef);
};
我们可以为其添加马达(motor)和角度限制,让其自己去运动!或者限制最大角度
var ball_A = createBall(30,100,100);
var ball_B = createBall(5,90,90,true);//把位置为中心偏上方并设置为静态
var revoluteJointDef = new b2RevoluteJointDef();
revoluteJointDef.Initialize(ball_A, ball_B, ball_B.GetWorldCenter());//并以中间的ball的中心作为锚点
//设置最大角度和最小角度!并enableLimit,如果enableLimit为false则最大角度和最小角度没有意义
revoluteJointDef.lowerAngle = -5.0; // -90 degrees
revoluteJointDef.upperAngle = 2.5;// 45 degrees
revoluteJointDef.enableLimit = false;//
revoluteJointDef.maxMotorTorque = 20.0;//就当是力量的象征吧!
revoluteJointDef.motorSpeed = 10.0;//速度你懂得!
revoluteJointDef.enableMotor = true;
world.CreateJoint(revoluteJointDef);
};
PrismaticJoint----只允许一个自由度,为了防止两个body自由旋转.
额................相当于一个活塞运动般! Like this!
var ball_A = createBall(30,100,150);
var ball_B = createBall(5,100,50,true);//把位置为中心偏上方并设置为静态
var worldAxis = new b2Vec2(0.0, 1.0);//确定那一个自由度用的
var prismaticJointDef = new b2PrismaticJointDef();
prismaticJointDef.Initialize(ball_B, ball_A, ball_B.GetWorldCenter(), worldAxis);
prismaticJointDef.lowerTranslation = -5.0;
prismaticJointDef.upperTranslation = 2.5;
prismaticJointDef.enableLimit = true;
prismaticJointDef.maxMotorForce = 1.0;
prismaticJointDef.motorSpeed = 0.0;
prismaticJointDef.enableMotor = true;
world.CreateJoint(prismaticJointDef);
};
PulleyJoint----------------滑轮链接
var ball_A = createBall(10,50,150);
var ball_B = createBall(10,150,150);//把位置为中心偏上方并设置为静态
var anchor1 = ball_A.GetWorldCenter();
var anchor2 = ball_B.GetWorldCenter();
var groundAnchor1 = new b2Vec2(anchor1.x, anchor1.y - (100 / drawScale));
var groundAnchor2 = new b2Vec2(anchor2.x, anchor2.y - (100 / drawScale));
var ratio = 1.0;
var pulleyJointDef = new b2PulleyJointDef();
pulleyJointDef.Initialize(ball_A, ball_B, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio);
pulleyJointDef.maxLengthA = 300 / drawScale;
pulleyJointDef.maxLengthB = 300 / drawScale;
world.CreateJoint(pulleyJointDef);
};
GearJoint齿轮链接!是相对比较复杂的一个joint,我们模仿齿轮的工作的状态!
创建一个revoluteJoint做齿轮!并且再创建一个活塞joint,(prismaticJoint)做功!
分别使用齿轮(左边的小球)和一个中心点做旋转,再使用中心点和小球(右边的)做功 !
我们可以推动小球让齿轮运转,也可以使用齿轮让小球做功!
var _groundBody = createBall(5,50,100,true);
var gear = createBall(20,50,100);
var ball = createBall(20,150,100);
var revoluteJointDef = new b2RevoluteJointDef();
revoluteJointDef.Initialize(_groundBody, gear, gear.GetWorldCenter()); //ground must be the first body
revoluteJointDef.lowerAngle = -0.5 * b2Settings.b2_pi; // -90 degrees
revoluteJointDef.upperAngle = 0.25 * b2Settings.b2_pi; // 45 degrees
revoluteJointDef.enableLimit = true;
revoluteJointDef.maxMotorTorque = 10.0;
revoluteJointDef.motorSpeed = 0.0;
revoluteJointDef.enableMotor = true;
var revoluteJoint = world.CreateJoint(revoluteJointDef);
var worldAxis = new b2Vec2(1.0, 0.0);
var prismaticJointDef = new b2PrismaticJointDef();
prismaticJointDef.Initialize(_groundBody, ball, ball.GetWorldCenter(), worldAxis); //ground must be the first body
prismaticJointDef.lowerTranslation = -5.0;
prismaticJointDef.upperTranslation = 2.5;
prismaticJointDef.enableLimit = true;
prismaticJointDef.maxMotorForce = 1.0;
prismaticJointDef.motorSpeed = 0.0;
prismaticJointDef.enableMotor = true;
var prismaticJoint = world.CreateJoint(prismaticJointDef);
var gearJointDef = new b2GearJointDef();
gearJointDef.joint1 = revoluteJoint;
gearJointDef.joint2 = prismaticJoint;
gearJointDef.bodyA = gear;
gearJointDef.bodyB = ball;
gearJointDef.ratio = 2.0 * b2Settings.b2_pi / (300 / drawScale);
world.CreateJoint(gearJointDef);
};
Linejoint---相当于悬挂
var groundBody = createBall(5,50,50,true);
var ball = createBall(20,50,200);
var worldAxis = new b2Vec2(0.0, 1.0);
var lineJointDef = new b2LineJointDef();
lineJointDef.Initialize(groundBody, ball, ball.GetWorldCenter(), worldAxis);
lineJointDef.lowerTranslation = -2.0;
lineJointDef.upperTranslation = 2.0;
lineJointDef.enableLimit = true;
lineJointDef.maxMotorForce = 200.0;
lineJointDef.motorSpeed = 10.0;
lineJointDef.enableMotor = true;
world.CreateJoint(lineJointDef);
};
Weldjoint--焊接
var ballA = createBall(20,100,100);
var ballB = createBall(20,120,100);
var weldJointDef = new b2WeldJointDef();
weldJointDef.Initialize(ballA, ballB, ballA.GetWorldCenter());
world.CreateJoint(weldJointDef);
};
一个稍微复杂点的综合!其实也只用到了两个Joint!但是可以看到joint的混合使用!这个有点像极限摩托的感觉!
其中只用到了两个 ,连接轴之间的链接是Prismatic,轴和车轮直接是Revolute,轴和身体直接也是Revolute,不过要限制可以旋转的角度!So...that!
额····给它加个外表可能会好看些!