一般来讲,创建一个刚体有五个步骤,具体为:
贴源码:
var bodyDef:b2BodyDef = new b2BodyDef();
var boxDef:b2PolygonDef = new b2PolygonDef();
var circleDef:b2CircleDef = new b2CircleDef();
var body:b2Body;
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
circleDef.radius = (Math.random() * 10 + 10) / RATIO;
circleDef.density = 1.0;
circleDef.friction = 0.3;
circleDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(circleDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.SetAsBox((Math.random() * 15 + 20) / 2 / RATIO, (Math.random() * 15 + 10) / 2 / RATIO);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.vertexCount = 3;
boxDef.vertices[0].Set(0, (Math.random() * 10 + 15) / RATIO);
boxDef.vertices[1].Set((Math.random() * -10 - 10) / RATIO, (Math.random() * -10 - 15) / RATIO);
boxDef.vertices[2].Set((Math.random() * 10 + 10) / RATIO, (Math.random() * -10 - 15) / RATIO);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.vertexCount = 4;
boxDef.vertices[0].Set((Math.random() * -10 - 10 ) / RATIO, (Math.random() * 10 + 10) / RATIO);
boxDef.vertices[1].Set((Math.random() * -10 - 5) / RATIO, (Math.random() * -10 - 10) / RATIO);
boxDef.vertices[2].Set((Math.random() * 10 + 5) / RATIO, (Math.random() * -10 - 10) / RATIO);
boxDef.vertices[3].Set((Math.random() * 10 + 10) / RATIO, (Math.random() * 10 + 10) / RATIO);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.vertexCount = 5;
boxDef.vertices[0].Set(0, (Math.random() * 10 + 10) / RATIO);
boxDef.vertices[2].Set((Math.random() * -10 - 15) / RATIO, (Math.random() * -10 - 10) / RATIO);
boxDef.vertices[3].Set((Math.random() * 10 + 5) / RATIO, (Math.random() * 10 - 10) / RATIO);
boxDef.vertices[1].Set((boxDef.vertices[0].x + boxDef.vertices[2].x), (boxDef.vertices[0].y + boxDef.vertices[2].y));
boxDef.vertices[1].Multiply(Math.random() / 2 + 0.8);
boxDef.vertices[4].Set((boxDef.vertices[3].x + boxDef.vertices[0].x), (boxDef.vertices[3].y + boxDef.vertices[0].y));
boxDef.vertices[4].Multiply(Math.random() / 2 + 0.8);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
运行结果:
刚体定义,设置位置,角度,阻尼,贴图等相关属性
形状定义,高宽,密度,摩擦力,弹力等相关属性
创建刚体
创建形状
计算质量
注:
Box2D有三种基本图形,分别为圆形,矩形,多边形
刚体默认原点是在中心
SetAsBox函数接收的是半个宽度和半个高度,圆接收的是半径
创建多边形首先要指出顶点数(最多8个),在指点顶点位置即可,指定顶点必须以顺时针方向指定,否则会变成静态物体,因为其面积计算为负,导致质量=密度*面积为负
Box2D只支持凸多边形,因此要创建凹多边形需将其拆分为多个凸多边形
静态物体的密度为0
角度为弧度制
贴源码:
var bodyDef:b2BodyDef = new b2BodyDef();
var boxDef:b2PolygonDef = new b2PolygonDef();
var circleDef:b2CircleDef = new b2CircleDef();
var body:b2Body;
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
circleDef.radius = (Math.random() * 10 + 10) / RATIO;
circleDef.density = 1.0;
circleDef.friction = 0.3;
circleDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(circleDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.SetAsBox((Math.random() * 15 + 20) / 2 / RATIO, (Math.random() * 15 + 10) / 2 / RATIO);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.vertexCount = 3;
boxDef.vertices[0].Set(0, (Math.random() * 10 + 15) / RATIO);
boxDef.vertices[1].Set((Math.random() * -10 - 10) / RATIO, (Math.random() * -10 - 15) / RATIO);
boxDef.vertices[2].Set((Math.random() * 10 + 10) / RATIO, (Math.random() * -10 - 15) / RATIO);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.vertexCount = 4;
boxDef.vertices[0].Set((Math.random() * -10 - 10 ) / RATIO, (Math.random() * 10 + 10) / RATIO);
boxDef.vertices[1].Set((Math.random() * -10 - 5) / RATIO, (Math.random() * -10 - 10) / RATIO);
boxDef.vertices[2].Set((Math.random() * 10 + 5) / RATIO, (Math.random() * -10 - 10) / RATIO);
boxDef.vertices[3].Set((Math.random() * 10 + 10) / RATIO, (Math.random() * 10 + 10) / RATIO);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
bodyDef.position.Set((Math.random() * 200 - 100 + 275) / RATIO, (Math.random() * -100 - 100) / RATIO);
boxDef.vertexCount = 5;
boxDef.vertices[0].Set(0, (Math.random() * 10 + 10) / RATIO);
boxDef.vertices[2].Set((Math.random() * -10 - 15) / RATIO, (Math.random() * -10 - 10) / RATIO);
boxDef.vertices[3].Set((Math.random() * 10 + 5) / RATIO, (Math.random() * 10 - 10) / RATIO);
boxDef.vertices[1].Set((boxDef.vertices[0].x + boxDef.vertices[2].x), (boxDef.vertices[0].y + boxDef.vertices[2].y));
boxDef.vertices[1].Multiply(Math.random() / 2 + 0.8);
boxDef.vertices[4].Set((boxDef.vertices[3].x + boxDef.vertices[0].x), (boxDef.vertices[3].y + boxDef.vertices[0].y));
boxDef.vertices[4].Multiply(Math.random() / 2 + 0.8);
boxDef.density = 1.0;
boxDef.friction = 0.3;
boxDef.restitution = 0.3;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
运行结果:
![Box2dの任意多边形[Ispooky] Box2dの任意多边形[Ispooky]](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)