前面两章我们已经研究了如何使用Box2d来模拟游戏世界,这一章就把所有的东西拼凑在一起,最终完成我们的游戏。

一、定义物体

典型的物体:

{type:'ground',name:'dirt',x:500,y:440,width:1000,height:20,isStatic:true},
{type:'ground',name:'wood',x:185,y:390,width:30,height:80,isStatic:true},

典型的物体类型:

'glass':{
fullHealth:100,
density:2.4,
friction:0.4,
restitution:0.15,
},
'wood':{
fullHealth:500,
density:0.7,
friction:0.4,
restitution:0.4,
},

二、添加Box2d

参考前两章

三、创建物体

现在Box2d设置完毕,我们将在entities对象内部实现之前声明的entities.create()方法。该方法接受entities对象为参数,创建物体并将其加入到世界中

create:function(entity){
var definition = entities.definitions[entity.name];
if(!definition){
console.log('无定义名字',entity.name);
return;
}
switch(entity.type){
case 'block'://简单的矩形
entity.health = definition.fullHealth;
entity.fullHealth = definition.fullHealth;
entity.shape = 'rectangle';
entity.sprite = loader.loadImage('images/entities/'+entity.name+'.png');
//entity.breakSound = game.breakSound[entity.name];
box2d.createRectangle(entity,definition);
break;
case 'ground'://简单的矩形
entity.shape = 'rectangle';
//不会被画出,所以不必具有图像
box2d.createRectangle(entity,definition);
break;
case 'hero'://简单的圆
case 'villain'://简单的圆、矩形
entity.health = definition.fullHealth;
entity.fullHealth = definition.fullHealth;
entity.sprite = loader.loadImage('images/entities/'+entity.name+'.png');
entity.shape = definition.shape;
entity.bounceSound = game.bounceSound;
if(definition.shape == 'circle'){
entity.radius = definition.radius;
box2d.createCircle(entity,definition);
}else if(definition.shape == 'rectangle'){
entity.width = definition.width;
entity.height = definition.height;
box2d.createRectangle(entity,definition);
}
break;
default:
console.log('没定义类型',entity.type);
break;
}
},

四、向关卡中加入物体

data:[
{
//level 1
foreground:'desert-foreground',
background:'clouds-background',
entities:[
{type:'ground',name:'dirt',x:500,y:440,width:1000,height:20,isStatic:true},
{type:'ground',name:'wood',x:185,y:390,width:30,height:80,isStatic:true},

{type:'block',name:'wood',x:520,y:380,angle:90,width:100,height:25},
{type:'block',name:'glass',x:520,y:280,angle:90,width:100,height:25},
{type:'villain',name:'burger',x:520,y:205,calories:590},

{type:'block',name:'wood',x:620,y:380,angle:90,width:100,height:25},
{type:'block',name:'glass',x:620,y:280,angle:90,width:100,height:25},
{type:'villain',name:'fries',x:620,y:205,calories:420},

{type:'hero',name:'orange',x:80,y:405},
{type:'hero',name:'apple',x:140,y:405},

]
},

五、设置Box2d调试绘图

首先,在html文件中创建另一个canvas元素

<canvas id="debugcanvas" width="1000" height="480" style="border:1px solid;">
</canvas>

我们在对Box2d进行初始化时,设置调制绘图模式

html5游戏之Box2d物理引擎集成的更多相关文章

  1. 【极客学院出品】Cocos2d-X系列课程之九-BOX2D物理引擎

    Cocos2d-x 是时下最热门的手游引擎,在国内和国外手机游戏开发使用的份额各自是70%和25%,在App Store的top10中,有7个是用它开发的. 本节课程为Cocos2d-x系列课程之九, ...

  2. cocos2d-x中的Box2D物理引擎

    在Cocos2d-x中集成了2个物理引擎,一个是Chipmunk,一个是Box2D.前者是用C语言编写的,文档和例子相对较少:Box2D是用C++写的,并且有比较完善的文档和资料.所以在需要使用物理引 ...

  3. python下的Box2d物理引擎的配置

    /******************************* I come back! 由于已经大四了,正在找工作 导致了至今以来第二长的时间内没有更新博客.向大家表示道歉 *********** ...

  4. 实例介绍Cocos2d-x中Box2D物理引擎:使用关节

    下面我们将使用Box2D物理引擎技术进行重构.使得关节能够掌握如何在Box2D使用关节约束.HelloWorldScene.cpp中与使用关节的相关代码如下: void HelloWorld::add ...

  5. 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测

    在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...

  6. 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D

    我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...

  7. 瘸腿蛤蟆笔记29-cocos2d-x-3.2 Box2d物理引擎dynamics模块介绍

    转载标明出处:http://blog.csdn.net/notbaron/article/details/38611335 上篇回想 本篇名言:奋斗.寻觅.发现,而不屈服.[诗人丁尼生] 上篇中,我们 ...

  8. libgdx学习记录18——Box2d物理引擎

    libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...

  9. 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检測

    在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...

随机推荐

  1. Golang atomic

    原子操作函数 分为下面系列函数,其中Xxx可以是Int32/Int64/Uint32/Uint64/Uintptr/Pointer其中一种. 1.SwapXxx系列:交换新旧值: // SwapInt ...

  2. [C#]读取不同版本的excel文件的方法

    --------------------------------2007及以上的版本-------------------------------- 测试如下: //DataInterface.Met ...

  3. python爬虫学习(三):使用re库爬取"淘宝商品",并把结果写进txt文件

    第二个例子是使用requests库+re库爬取淘宝搜索商品页面的商品信息 (1)分析网页源码 打开淘宝,输入关键字“python”,然后搜索,显示如下搜索结果 从url连接中可以得到搜索商品的关键字是 ...

  4. gradle ----> 安装和使用

    1.安装gradle 参考官网教程:https://gradle.org/install/ 安装的前提:要求安装jdk1.7或者以上 比较重要的一步:配置环境变量,把gradle的bin目录的全路径配 ...

  5. uva11609

    以三个人组队为例,3组合是:C(3,0)=1,3,3,1.还有队长的选择.有 1*0,3*1,3*2,1*3种. 组合数:            1      3        3        1 ...

  6. gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i'

    2017-12-13 10:44:19gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i' 1.3.100 driver/char/random.cst ...

  7. PHP 练习项目------歆语微博项目

    一个简单微博项目,php+mysql+apache开发,个人购买资料的项目练习,适合新手练习. 测试账号:zhangqie  密码:123456 功能列表: 数据库增删改查, 图片上传 表情,@好友 ...

  8. Tensorflow手写数字识别---MNIST

    MNIST数据集:包含数字0-9的灰度图, 图片size为28x28.训练样本:55000,测试样本:10000,验证集:5000

  9. AVL平衡二叉树的各种问题(Balanced Binary Tree)

    AVL树或者是一棵空树,或者是具有以下性质的非空二叉搜索树: 1. 任一结点的左.右子树均为AVL树: 2.根结点左.右子树高度差的绝对值不超过1. 1.声明 #include<iostream ...

  10. Non-UTF-8 code starting with '\xbb' in file

    一.错误问题 错误问题:Non-UTF-8 code starting with '\xbb' in file,如图所示: 二.分析问题 原因:程序文件夹中出现中文,运行的时候出现如下错误,导致出错的 ...