矩形和矩形圓的碰撞檢測
// 檢測圓和矩形的碰撞 矩形的寬,高,中心點x,y 圓的半徑,中心點血,y
IsComputeCollision(jw, jh,jx,jy, rr, rx, ry) {
var _rx = rx - jx;
var _ry = ry - jy;
var dx = Math.min(_rx, jw * 0.5);
var dx1 = Math.max(dx, -jw * 0.5);
var dy = Math.min(_ry, jh * 0.5);
var dy1 = Math.max(dy, -jh * 0.5);
return (dx1 - _rx) * (dx1 - _rx) + (dy1 - _ry) * (dy1 - _ry) <= rr * rr;
},
// 檢測矩形,矩形碰撞
isCollsionWithRect(rect1,rect2){
var x1 = rect1.x
var y1 = rect1.y
var w1 = rect1.width
var h1 = rect1.height
var x2 = rect2.x
var y2 = rect2.y
var w2 = rect2.width
var h2 = rect2.height
if (x1 > x2 && x1 > x2 + w2) {
return -1;
} else if (x1 < x2 && x1 + w1 < x2) {
return -1;
} else if (y1 > y2 && y1 > y2 + h2) {
return -1;
} else if (y1 < y2 && y1 + h1 < y2) {
return -1;
}
if (Math.abs(parseInt(x1+w1)-parseInt(x2)) <= 3) {
// 左側
return 2
}
if (Math.abs(parseInt(x2+w2)-parseInt(x1)) <= 3 ) {
// 右側
return 3
}
if(Math.abs(parseInt(y1+h1)-parseInt(y2)) <= 3 ){
// 下邊
return 1
}
if (Math.abs(parseInt(y2+h2) -parseInt(y1) <= 3)) {
// 上邊
return 0
}
return 5
},