前面两章我们已经研究了如何使用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. ubuntu16.04 安装docker-ce,docker-compose

    2018.11.14更新 参考https://blog.csdn.net/qq_38199832/article/details/77803645 sudo curl -sSL https://dow ...

  2. nRF52832的SAADC

    SAADC部分思维导图 1ADC原理 1.1主要特点 1)8/10/12分辨率,使用过采样可达到14位分辨率 2)多达8个通道 单端输入时使用1个通道,2个通道可组成差分输入 单端和差分输入时均可配置 ...

  3. (转)Windows上搭建Kafka运行环境

    转自:<Windows上搭建Kafka运行环境> 完整解决方案请参考: Setting Up and Running Apache Kafka on Windows OS   在环境搭建过 ...

  4. 2018年浙江理工大学程序设计竞赛校赛 Problem I: 沙僧

    沙僧 思路: dfs序+差分数组 分层考虑,通过dfs序来查找修改的区间段,然后用差分数组修改 代码: #include<bits/stdc++.h> using namespace st ...

  5. layui 下拉框不显示解决方法

    添加以下代码 layui.use('form', function(){ var form = layui.form; form.render(); });

  6. 【转】 详解C中volatile关键字

    转自: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html volatile提醒编译器它后面所定义的变量随时都有可能 ...

  7. idea ----> 使用idea工具整合mybaiti时出现的问题总结

    使用idea测试mabtis实例时出现  java.lang.IllegalArgumentException: Mapped Statements collection does not conta ...

  8. WPF经典编程模式-MVVM示例讲解

    https://www.cnblogs.com/lvdongjie/p/5515962.html

  9. Django 权限管理(二)

    权限菜单展示 1.展示的效果 实现该效果,涉及到的核心点(突破点) 1. 权限分类: (1)菜单权限 (主要用来展示的权限(url)如查看权限 url,  如上图中的每个权限都归类为菜单权限,主要用来 ...

  10. 20165309 Linux安装及学习

    Linux安装及学习 安装虚拟机 结合娄老师的博客<基于VirtualBox虚拟机安装Ubuntu图文教程>和对一些小问题的百度,我也算是很顺利地完成了安装. 然后,按照步骤安装了虚拟机增 ...