• html5游戏之Box2d物理引擎集成


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

    一、定义物体

    典型的物体:

    {type:'ground',name:'dirt',x:500,y:440,1000,height:20,isStatic:true},
    {type:'ground',name:'wood',x:185,y:390,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,1000,height:20,isStatic:true},
    {type:'ground',name:'wood',x:185,y:390,30,height:80,isStatic:true},

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

    {type:'block',name:'wood',x:620,y:380,angle:90,100,height:25},
    {type:'block',name:'glass',x:620,y:280,angle:90,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进行初始化时,设置调制绘图模式


    作者:狂流
    出处:http://www.cnblogs.com/kuangliu/
    欢迎转载,分享快乐! 如果觉得这篇文章对你有用,请抖抖小手,推荐一下!

  • 相关阅读:
    01 React快速入门(一)——使用循环时对于‘key’报错处理
    01 div实现浮动效果
    17 制作热力图
    16 ArcGIS Server 10.6.1发布影像服务
    虚拟机上有关于Apache服务基于主机名@4域名访问网页
    虚拟机上有关于Apache服务基于IP地址@3IP访问网站
    Apache有关个人用户主页以及强制访问安全系统功能介绍@2
    Apache服务的安装以及服务文件参数内容的配置 @1
    WVware虚拟机linux环境下使用ssh服务以安全密钥的形式远程控制服务(本地客户端登录远程服务端)
    WVware虚拟机linux环境下RAID5 五块磁盘操作管理实例
  • 原文地址:https://www.cnblogs.com/kuangliu/p/3449241.html
Copyright © 2020-2023  润新知