javascript - How to add collision detection boxes to a large number of sprites?
phpcn_u1582
phpcn_u1582 2017-05-31 10:39:16
0
1
492

I have a sprite sheet, and each frame in it has a different size, as shown below:

Now I want to add collision detection boxes for them, but there are many pictures, and the collision position is not limited to the picture border. In this case,
how to add detection boxes, do I need to add them as needed for each frame? How to get the position of the collision box after adding it? Is there any tool to achieve this? Thank you very much~

phpcn_u1582
phpcn_u1582

reply all(1)
滿天的星座

基本上游戏开发最难的部分之一就是碰撞检测了,你如何选择碰撞检测请务必根据你项目的需求来决定,一般对于多边形用得比较多的是分离轴定理(SAT),像你这种如果要求不是特别高的话用正常等大的矩形检测就好了,如果非要变化的话就为每个状态保存一个碰撞检测框用于碰撞检测,比如说:

var monster = {
    steps : {
        "walk" : {
            "width" : "", //碰撞检测时用于计算的宽度
            "height" : "", //高度
            "imgs" : [] //精灵,可能"walk"这个动作中包含了多帧,进行游戏循环的时候需要逐步变化
            "idx" : 0 //用于判断imgs当前到哪个步骤的下标索引
        },
        "jump" : {
            //同样的
        },
        "run" : {
            //等等
        }
        //........
    }, //保存不同状态下的显示效果和碰撞边框
    "currentStatus" : "walk", //当前的状态,方便相关功能的载入
    "setup" : function(){
        this.x += "";
        this.y += '';
        //用于计算和更新位置
    },
    "draw" : function(){
        //用于绘制
    }
    //......
};
//碰撞检测一般都在游戏循环里单独调用一个方法,通过循环获取活动对象的x,y,width,height来进行检测,这里你可以通过判断一个矩形是否存在一个顶点在另一个矩形内部来得知是否碰撞,当然用sat也是可以的,不过麻烦了点

当然我这里仅仅给你参考意见,我不是主攻游戏开发,以前有兴趣的时候自学过一段时间,如果有什么地方有问题欢迎指出来,就这样吧。

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!