JS寫的不咋地的碰撞檢測

最近在學習碰撞檢測相關的知識,但說實話,寫的不咋地。可是由於鄙人學識淺薄,因此以爲基本上還行,可是挺追求我完美的,因此想拿出來讓大神們批評批評。app

先來看一下效果dom


感受哇,真卡,真難受。由於真原本是正方形因此檢測的不是很準確。函數

下面來批評一下這個的代碼。性能

  • 碰撞檢測的代碼
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.總體圖示

只要在那個區域以內就行。this

  • 碰撞區域邊緣的代碼
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...幾乎一直在重排,因此性能不是最好的,但效果基本上實現了。spa

相關文章
相關標籤/搜索