Box2d引擎之元素

时间:2024-06-10 17:34:50

主要包括:

  • 简单形状的物体,如矩形、圆、多边形
  • 复杂的由多个形状组成的物体
  • 结合点,如连接多个物体的旋转结合点
  • 接触监听器

一、简单形状的物体

  1. 矩形
    1. function createRectangularBody(){
      var bodyDef = new b2BodyDef;
      bodyDef.type = b2Body.b2_dynamicBody;
      bodyDef.position.x = 40/scale;
      bodyDef.position.y = 100/scale;

      var fixtureDef = new b2FixtureDef;
      fixtureDef.density = 1.0;
      fixtureDef.friction = 0.5;
      fixtureDef.restitution = 0.3;

      fixtureDef.shape = new b2PolygonShape;
      fixtureDef.shape.SetAsBox(30/scale,50/scale);

      var body = world.CreateBody(bodyDef);
      var fixture = body.CreateFixture(fixtureDef);
      }

    1. function createCircularBody(){
      var bodyDef = new b2BodyDef;
      bodyDef.type = b2Body.b2_dynamicBody;
      bodyDef.position.x = 5;
      bodyDef.position.y = 3.3;

      var fixtureDef = new b2FixtureDef;
      fixtureDef.density = 1.0;
      fixtureDef.friction = 0.5;
      fixtureDef.restitution = 0.7;

      fixtureDef.shape = new b2CircleShape(1);

      var body = world.CreateBody(bodyDef);
      var fixture = body.CreateFixture(fixtureDef);

      }

  2. 多边形

function createSimplePolygonBody(){
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = 8;
bodyDef.position.y = 1.66;

var fixtureDef = new b2FixtureDef;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.2;

fixtureDef.shape = new b2PolygonShape;
//必须按顺时针方向创建一个顶点数组
var points = [
new b2Vec2(0,0),
new b2Vec2(40/scale,50/scale),
new b2Vec2(50/scale,100/scale),
new b2Vec2(-50/scale,100/scale),
new b2Vec2(-40/scale,50/scale),
]
//调用SetAsArray来使用顶点数组定义形状
fixtureDef.shape.SetAsArray(points,points.length);

var body= world.CreateBody(bodyDef);
var fixture = body.CreateFixture(fixtureDef);

}

二、复杂的由多个形状组成的物体

function createComplexBody(){
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = 11.5;
bodyDef.position.y = 2;
var body = world.CreateBody(bodyDef);

//创建第一个载具并为物体添加圆形状
var fixtureDef = new b2FixtureDef;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.7;
fixtureDef.shape = new b2CircleShape(40/scale);
body.CreateFixture(fixtureDef);
//创建第二个载具并为物体添加多边形
fixtureDef.shape = new b2PolygonShape;
var points = [
new b2Vec2(0,0),
new b2Vec2(40/scale,50/scale),
new b2Vec2(50/scale,100/scale),
new b2Vec2(-50/scale,100/scale),
new b2Vec2(-40/scale,50/scale),
];
fixtureDef.shape.SetAsArray(points,points.length);
body.CreateFixture(fixtureDef);
}

三、结合点

function createRevoluteJoint(){
//定义第一个物体
var bodyDef1 = new b2BodyDef;
bodyDef1.type = b2Body.b2_dynamicBody;
bodyDef1.position.x = 16;
bodyDef1.position.y = 1.6;
var body1 = world.CreateBody(bodyDef1);
//创建第一个载具并向物体添加矩形形状
var fixtureDef1 = new b2FixtureDef;
fixtureDef1.density = 1.0;
fixtureDef1.friction = 0.5;
fixtureDef1.restitution = 0.5;
fixtureDef1.shape = new b2PolygonShape;
fixtureDef1.shape.SetAsBox(1.6,0.3);
body1.CreateFixture(fixtureDef1);
//定义第二个物体
var bodyDef2 = new b2BodyDef;
bodyDef2.type = b2Body.b2_dynamicBody;
bodyDef2.position.x = 15.5;
bodyDef2.position.y = 1.6;
var body2 = world.CreateBody(bodyDef2);
//创建第二个载具并向物体添加多边形形状
var fixtureDef2 = new b2FixtureDef;
fixtureDef2.density = 1.0;
fixtureDef2.friction = 0.5;
fixtureDef2.restitution = 0.5;
fixtureDef2.shape = new b2PolygonShape;
var points = [
new b2Vec2(0,0),
new b2Vec2(40/scale,50/scale),
new b2Vec2(50/scale,100/scale),
new b2Vec2(-50/scale,100/scale),
new b2Vec2(-40/scale,50/scale),
];
fixtureDef2.shape.SetAsArray(points,points.length);
body2.CreateFixture(fixtureDef2);
//创建结合点连接body1 and body2
var jointDef = new b2RevoluteJointDef;
var jointCenter = new b2Vec2(15.5,1.6);

jointDef.Initialize(body1,body2,jointCenter);
world.CreateJoint(jointDef);
}

四、接触监听器

var specialBody;
function createSpecialBody(){
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = 15;
bodyDef.position.y = 0;

specialBody = world.CreateBody(bodyDef);
specialBody.SetUserData({name:'special',life:250});
//创建载具并添加圆形状
var fixtureDef = new b2FixtureDef;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.5;

fixtureDef.shape = new b2CircleShape(1);
var fixture = specialBody.CreateFixture(fixtureDef);
}
b2ContactListener有四个事件

  • BeginContact():物体开始接触时调用
  • EndContact():结束接触时调用
  • PostSolve():求解器完成后调用,进行碰撞检测时很有用
  • PreSolve():在求解器求解前调用

function listenForContact(){
var listener = new Box2D.Dynamics.b2ContactListener;
listener.PostSolve = function(contact,impulse){
var body1 = contact.GetFixtureA().GetBody();
var body2 = contact.GetFixtureB().GetBody();
//如果接触的两个物体都具有生命值,则减少生命值
if(body1==specialBody||body2==specialBody){
var impulseAlongNormal = impulse.normalImpulses[0];
specialBody.GetUserData().life -= impulseAlongNormal;
console.log(impulseAlongNormal,'碰撞后生命值:',specialBody.GetUserData().life);
}
};
world.SetContactListener(listener);
}