js碰撞检测

将数据初始化成rect矩形类型
  let rectSize = {
        width:16,
        height:16
    };
  let rect = new Rect({
                x:startX,
                y:startY
            },rectSize);
   let rect1 = new Rect({
                x:startX,
                y:startY
            },rectSize);
            
 //第一个参数是矩形的起始点,第二个参数是矩形的长宽对象
Rect类
class Rect {
    constructor(start, size) {
        this.start = start;
        this.size = size;
        return [
            start,
            {
                x: start.x + size.width,
                y: start.y + size.height
            }
        ]
    }

    collision(rect1) {
        let rect = this;
        let lt = rect[0];
        let rt = {
            x: rect[1].x,
            y: rect[0].y
        };
        let rb = rect[1];
        let lb = {
            x: rect[0].x,
            y: rect[1].y
        };

        //第二个
        let lt1 = rect1[0];
        let rt1 = {
            x: rect1[1].x,
            y: rect1[0].y
        };
        let rb1 = rect1[1];
        let lb1 = {
            x: rect1[0].x,
            y: rect1[1].y
        };

        let rectInOther = false;
        if (rect1.contains(lt) || rect1.contains(rt) || rect1.contains(rb) || rect1.contains(lb)) {
            rectInOther = true;
        }

        let rect1InOther = false;
        if (rect.contains(lt1) || rect.contains(rt1) || rect.contains(rb1) || rect.contains(lb1)) {
            rect1InOther = true;
        }
        return rect1InOther || rectInOther;
    }

    contains(point) {
        let rect = this;
        let isContains = false;
        let minX = rect[0].x;
        let minY = rect[0].y;
        let maxX = rect[1].x;
        let maxY = rect[1].y;

        if (point.x >= minX && point.x <= maxX && point.y >= minY && point.y <= maxY) {
            isContains = true;
        }

        return isContains
    }

}

判断矩形是否相交的方法collision属于rect矩形类
使用的时候可以这样用

//碰撞判断

 rect.collision(rect1)    //false 

你可能感兴趣的:(JavaScript)