博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Box2d引擎之元素
阅读量:4971 次
发布时间:2019-06-12

本文共 4732 字,大约阅读时间需要 15 分钟。

主要包括:

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

一、简单形状的物体

  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);
}

 

转载于:https://www.cnblogs.com/kuangliu/p/3442314.html

你可能感兴趣的文章
IFrame 框架的用法简介
查看>>
单词扭转,字符逆转
查看>>
RESTful规范
查看>>
struts 常用的标签
查看>>
Activity生命周期
查看>>
Python3基本数据类型
查看>>
hashlib模块 加密
查看>>
nodejs安装后在git bash 中输入node -v验证是否安装成功,但是提示:sh.exe": node :command not found,怎么解决?...
查看>>
jdbc之statement 与 preparedment
查看>>
Django(六)Session、CSRF、中间件
查看>>
【转】每天一个linux命令(11):nl命令
查看>>
第七周学习进度条
查看>>
抽象类与接口
查看>>
poj 1236 强联通分量
查看>>
C阅读与学习
查看>>
python运维之saltstack方式
查看>>
[转]关于Web前端开发,附:(百度web前端笔试面试题目)
查看>>
【转】WPF 从FlowDocument中找到Hyperlink
查看>>
python AES CBC模式加密
查看>>
Spring web flow的意义
查看>>