SOURCE

console 命令行工具 X clear

                    
>
console
const rectList = [
    {
        x: 50,
        y: 50,
        width: 40,
        height: 50,
        angle: 54,
        color: 'red',
    },
    {
        x: 100,
        y: 70,
        width: 100,
        height: 40,
        angle: 40,
        color: 'green',
    }
]


for(let i = 0 ; i < rectList.length; i++) {
    const data = rectList[i]
    const div = document.createElement('div')
    Object.assign(div.style,{
        position:'absolute',
        width: data.width + 'px',
        height: data.height + 'px',
        left: data.x + 'px',
        top: data.y + 'px',
        transform: `rotate(${data.angle}deg)`,
        background: data.color
    })
    document.body.appendChild(div)
}


 function OBB (centerPoint, width, height, rotation) {
    this.centerPoint = centerPoint;
    this.extents = [width / 2, height / 2];
    this.axes = [new Vector2(Math.cos(rotation), Math.sin(rotation)), new Vector2(-1 * Math.sin(rotation), Math.cos(rotation))];

    this._width = width;
    this._height = height;
    this._rotation = rotation;
}
OBB.prototype = {
    getProjectionRadius: function (axis) {
        console.log(this.axes)
        return this.extents[0] * Math.abs(axis.dot(this.axes[0])) + this.extents[1] * Math.abs(axis.dot(this.axes[1]));
    }
}

function Vector2 (x, y) {
    this.x = x || 0;
    this.y = y || 0;
};

Vector2.prototype = {
    sub: function (v) {
        return new Vector2(this.x - v.x, this.y - v.y)
    },
    dot: function (v) {
        return this.x * v.x + this.y * v.y;
    }
};

const CollisionDetector = {
    detectorOBBvsOBB: function (OBB1, OBB2) {
        const nv = OBB1.centerPoint.sub(OBB2.centerPoint);
        const axisA1 = OBB1.axes[0];
        if (OBB1.getProjectionRadius(axisA1) + OBB2.getProjectionRadius(axisA1) <= Math.abs(nv.dot(axisA1))) return false;
        const axisA2 = OBB1.axes[1];
        if (OBB1.getProjectionRadius(axisA2) + OBB2.getProjectionRadius(axisA2) <= Math.abs(nv.dot(axisA2))) return false;
        const axisB1 = OBB2.axes[0];
        if (OBB1.getProjectionRadius(axisB1) + OBB2.getProjectionRadius(axisB1) <= Math.abs(nv.dot(axisB1))) return false;
        const axisB2 = OBB2.axes[1];
        if (OBB1.getProjectionRadius(axisB2) + OBB2.getProjectionRadius(axisB2) <= Math.abs(nv.dot(axisB2))) return false;
        return true;
    }
}


function check () {

const OBB1 = rectList[0]
const OBB2 = rectList[1]

const rect1 = new OBB(new Vector2(OBB1.x, OBB1.y), OBB1.width, OBB1.height, OBB1.angle * Math.PI / 180);
const rect2 = new OBB(new Vector2(OBB2.x, OBB2.y), OBB2.width, OBB2.height, OBB2.angle * Math.PI / 180);
const isCollision = CollisionDetector.detectorOBBvsOBB(rect1, rect2);

console.log('isCollision',isCollision)
}

<body></body>
body{
    position: relative
}