html5游戏之Box2d物理引擎集成
前面两章我们已经研究了如何使用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物理引擎集成的更多相关文章
- 【极客学院出品】Cocos2d-X系列课程之九-BOX2D物理引擎
Cocos2d-x 是时下最热门的手游引擎,在国内和国外手机游戏开发使用的份额各自是70%和25%,在App Store的top10中,有7个是用它开发的. 本节课程为Cocos2d-x系列课程之九, ...
- cocos2d-x中的Box2D物理引擎
在Cocos2d-x中集成了2个物理引擎,一个是Chipmunk,一个是Box2D.前者是用C语言编写的,文档和例子相对较少:Box2D是用C++写的,并且有比较完善的文档和资料.所以在需要使用物理引 ...
- python下的Box2d物理引擎的配置
/******************************* I come back! 由于已经大四了,正在找工作 导致了至今以来第二长的时间内没有更新博客.向大家表示道歉 *********** ...
- 实例介绍Cocos2d-x中Box2D物理引擎:使用关节
下面我们将使用Box2D物理引擎技术进行重构.使得关节能够掌握如何在Box2D使用关节约束.HelloWorldScene.cpp中与使用关节的相关代码如下: void HelloWorld::add ...
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测
在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...
- 瘸腿蛤蟆笔记29-cocos2d-x-3.2 Box2d物理引擎dynamics模块介绍
转载标明出处:http://blog.csdn.net/notbaron/article/details/38611335 上篇回想 本篇名言:奋斗.寻觅.发现,而不屈服.[诗人丁尼生] 上篇中,我们 ...
- libgdx学习记录18——Box2d物理引擎
libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检測
在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...
随机推荐
- 大数据-Hive 常用命令
Hive 启动 ~$ hive 退出 hive>quit; --退出hive or hive> exit; --exit会影响之前的使用,所以需要下一句kill掉hadoop的进程 > ...
- python - selenium 2 升级到最新版本
python - selenium 2 升级到最新版本 之前一直用的是selenium 2.48 .firefox36 而实际用户的浏览器可能都有自动更新功能,所以版本基本上是最新的.所以这次专门做了 ...
- how-to-view-source-of-chrome-extension
https://gist.github.com/paulirish/78d6c1406c901be02c2d Option 1: Command-line download extension as ...
- JavaScript 第三章总结
Getting functional function的特点 function can be reused over and over much more readable function is p ...
- ssh repo ----> struts+hibernate+spring( jar包和源码)各版本下载链接
struts http://archive.apache.org/dist/struts/ hibernate http://hibernate.org/orm/releases/5.0/ sprin ...
- Vue Authentication And Route Handling Using Vue-router(详解)
英文原文 (本文原出处),本博在原文的基础上,进一步分析代码的结构和解释代码. git 代码 创建一个app: vue-router-auth 本文详解了如何使用vue-router建立路由记录对 ...
- JavaScript 入门笔记
JavaScript 1.JS和DOM的关系 浏览器有渲染html代码的功能,把html源码在内存里形成一个DOM对象,就是文档对象 浏览器内部有一个JS的解释器/执行/引擎,如chrome用v8 ...
- 一篇文章一张思维导图看懂Android学习最佳路线(转载)
Android学习路线从4个阶段来对Android的学习过程做一个全面的分析:Android初级.中级.高级以及资深工程师.只针对Android应用开发,不针对Rom开发和逆向工程等.方便起见虚拟“小 ...
- Entertainment Box(可多执行的区间问题)
本题出自: Nordic Collegiate Programming Contest 2015 Ada, Bertrand and Charles often argue over which T ...
- Django 的逆向解析url--reverse(转)
https://www.cnblogs.com/zhenfei/p/6368955.html Django中提供了一个关于URL的映射的解决方案,你可以做两个方向的使用: 1.有客户端的浏览器发起一个 ...