Poderiam me ajudar? Tentei aplicar sem o uso da biblioteca a colisão, na racket1 funcionou, quando apliquei a na 2 usando a aplicação na função do eixo (x,y) a bolinha trava no meio da tela e so se movimenta no eixo y.
Abaixo o código:
//variables ball
let xBall = 300;
let yBall = 200;
let diameter = 25;
let ligthning = diameter / 2;
//variables speed
let xBallspeed = 2;
let yBallspeed = 2;
let yRacket2speed = 2;
//variables racket1
let xRacket1 = 5;
let yRacket1 = 150;
//variables racket2
let xRacket2 = 585;
let yRacket2 = 150;
//variables racket size
let wRacket = 10;
let hRacket = 100;
function setup() {
createCanvas(600, 400);
}
function draw() {
background(0);
showBall();
rollBall();
limitBall();
showRacket1(xRacket1, yRacket1);
moveRacket1();
collision(xRacket1, yRacket1);
showRacket2(xRacket2, yRacket2);
moveRacket2();
collision(xRacket2, yRacket2);
}
//function ball
function showBall() {
circle(xBall, yBall, diameter);
}
function rollBall() {
xBall += xBallspeed;
yBall += yBallspeed;
}
function limitBall() {
if (xBall + ligthning > width || xBall - ligthning < 0) {
xBallspeed *= -1;
}
if (yBall + ligthning > height || yBall - ligthning <0) {
yBallspeed *= -1;
}
}
//function racket
function showRacket1 (x,y) {
rect(x, y, wRacket, hRacket, 10, 10);
}
function showRacket2 (x,y) {
rect(x, y, wRacket, hRacket, 10, 10);
}
function moveRacket1() {
if (keyIsDown(UP_ARROW)) {
yRacket1 -= 10;
}
if (keyIsDown(DOWN_ARROW)) {
yRacket1 += 10;
}
}
function moveRacket2() {
yRacket2speed = yBall - yRacket2 - wRacket / 2 - 50;
yRacket2 += yRacket2speed;
}
function collision(x,y) {
if (xBall - ligthning < x + wRacket && yBall - ligthning < y + hRacket && yBall + ligthning > y) {
xBallspeed *= -1;
}
}
function collision(x,y) {
if (xBall - ligthning < x + wRacket && yBall - ligthning < y + hRacket && yBall + ligthning > y) {
xBallspeed *= -1;
}
}