Это как ловить предмет с корзиной.(Нижняя часть первого объекта и Верхняя часть второго объекта).Это пример моего кода, но он обнаруживает все стороны.
this.hitPocket = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) ||
(mytop > otherbottom) ||
(myright < otherleft) ||
(myleft > otherright)) {
crash = false;
}
return crash;
}
ОБНОВЛЕНО: Привет, Кеннет Митчелл Де Леон, я попробовал ваше решение и изменил радиус на высоту и ширину.и это сработало.Моя проблема сейчас в том, что корзина подвижная или перетаскиваемая, чтобы поймать мяч.(Извините, что я упомянул об этом поздно) Поэтому, когда я перетаскиваю корзину в шар, она все равно ловит мяч, даже если он не прошел через верх корзины.
function hasContact(basket, ball){
const hoop = {
topLeft: {x:basket.x+10, y: basket.y},
topRight: {x: basket.x+basket.width-10,
y: basket.y}
//not necessary if your only concern is the top panel
// botLeft: {x: basket.position.x,
// y: basket.position.y+basket.height},
// botRight: {x:basket.position.x+basket.width,
// y:basket.position.y+basket.height}
}
//determine if ball is in between top left or top right of the basket in x axis
if(ball.x > hoop.topLeft.x && ball.x+ball.width < hoop.topRight.x
//determine if the ball is in contact with top panel of basket in y axis
&& ball.y-basket.y < ball.height/2 && basket.y - ball.y < ball.height/2){
return true;
}
return false;
}