JS写的不咋地的碰撞检测

最近在学习碰撞检测相关的知识,但说实话,写的不咋地。但是因为鄙人学识浅薄,所以觉得基本上还行,但是挺追求我完美的,所以想拿出来让大神们批评批评。

先来看一下效果


感觉哇,真卡,真难受。因为真本来是正方形所以检测的不是很准确。

下面来批评一下这个的代码。

  • 碰撞检测的代码
if((mover.offsetLeft + mover.offsetWidth >= target.offsetLeft) 
    && (mover.offsetTop +  mover.offsetHeight >= target.offsetTop) 
    && (target.offsetLeft + target.offsetWidth >= mover.offsetLeft)  
    && (target.offsetTop +  target.offsetHeight >= mover.offsetTop)  
    )

因为碰撞可以分为这四个角度。

1.左上角

2.右上角

3.左下角

4.右下角

5.整体图示

只要在那个区域之内就行。

  • 碰撞区域边缘的代码
setInterval(function move(e) {
    boll.style.left = boll.offsetLeft + (this.N * 10) + 'px'; //改变小球的位置
    boll.style.top = boll.offsetTop + (this.T * 10)  + 'px';
    if(boll.offsetLeft >= sq.offsetWidth - boll.offsetWidth || boll.offsetLeft <= 0 ){ //碰撞左右两边
        this.N *= -1; //改变方向(依自己喜好定义)
    }
    if(boll.offsetTop >= sq.offsetHeight - boll.offsetHeight || boll.offsetTop <= 0 ){ //碰撞上下两边
        this.T *= -1; //改变方向(依自己喜好定义)
    }   
}.bind(this), 50);
  • 检测每一个小球与其他小球是否碰撞
bollArr = [], //存放小球DOM元素,改变方向用
boll = []; //存放小球,检测是否碰撞用
setInterval(function move(e) {
    for (var i = 0; i < bollArr.length; i ++) {
        for (var j = i + 1; j < bollArr.length; j ++) {
            collisionDetection(bollArr[i], bollArr[j], fes[i], fes[j]);
        }
    }
}, 50);
  • 小球构造函数
function Boll() {
    this.backgroundColor = 'rgba(35, 215, 65, .3)'; //小球颜色
    this.left = getRandom(400); //小球位置
    this.top = getRandom(400);
    this.N = 1;  //改变小球方向的数(自己可以精确定义,我就随便定义了)
    this.T = 1;
    boll.push(this);
}
  • 整体代码
let sq = document.getElementById('square'), //获取最外面的框
    bollArr = [], //小球DOM元素集合
    boll = []; //小球实例集合
function Boll() {  //构造函数
    this.backgroundColor = 'rgba(35, 215, 65, .3)';
    this.left = getRandom(400);
    this.top = getRandom(400);
    this.N = 1;
    this.T = 1;
    boll.push(this);
}
let boll0 = new Boll(),
    boll1 = new Boll(),
    boll2 = new Boll();
Boll.prototype.createBoll = function() {  //创建小球
    let boll = document.createElement('div');
    boll.style.display = 'none';
    boll.style.width =  '60px';
    boll.style.height = '60px';
    boll.style.backgroundColor = this.backgroundColor;
    boll.style.borderRadius = '50%';
    boll.style.position = 'absolute';
    boll.style.left = this.left + 'px';
    boll.style.top = this.top + 'px';
    boll.style.display = 'block';
    sq.appendChild(boll);
    bollArr.push(boll);
    setInterval(function move(e) {  //检测是否和外面的框碰撞
        boll.style.left = boll.offsetLeft + (this.N * 10) + 'px';
        boll.style.top = boll.offsetTop + (this.T * 10)  + 'px';
        if(boll.offsetLeft >= sq.offsetWidth - boll.offsetWidth || boll.offsetLeft <= 0 ){
            this.N *= -1;
        }
        if(boll.offsetTop >= sq.offsetHeight - boll.offsetHeight || boll.offsetTop <= 0 ){
            this.T *= -1;
        }   
    }.bind(this), 50);
}
// 碰撞检测
function collisionDetection(mover, target, boll, boll2) {
    if((mover.offsetLeft + mover.offsetWidth >= target.offsetLeft) 
        && (mover.offsetTop +  mover.offsetHeight >= target.offsetTop) 
        && (target.offsetLeft + target.offsetWidth >= mover.offsetLeft)  
        && (target.offsetTop +  target.offsetHeight >= mover.offsetTop)  
    ){
         boll.N *= -1;
         boll.T *= -1;
         boll2.N *= -1;
         boll2.T *= -1;
    }
}
boll0.createBoll();
boll1.createBoll();
boll2.createBoll();
// 为每两个小球检测是否碰撞
setInterval(function move(e) {
    for (var i = 0; i < bollArr.length; i ++) {
        for (var j = i + 1; j < bollArr.length; j ++) {
            collisionDetection(bollArr[i], bollArr[j], boll[i], boll[j]);
        }
    }
}, 50);

上面其实有很多不好和bug,希望看到的大神能批评几句。
因为使用了offset...几乎一直在重排,所以性能不是最好的,但效果基本上实现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值