Estoy construyendo un simulador de física de pelotas en Javascript. Entiendo cómo detectar colisiones para objetos creados individualmente, pero tengo problemas cuando los objetos se construyen usando clases ES6. Aquí está el código que tengo hasta ahora.
Constructor de bolas
const Ball = class { constructor(x, y, dx, dy) { this.x = x; this.y = y; this.dx = dx; this.dy = dy; } checkForWall() { if (this.x + ballRadius < ballRadius) { this.dx = -this.dx; } if (this.y + ballRadius < ballRadius) { this.dy = -this.dy; } if (this.x + ballRadius > canvas.width) { this.dx = -this.dx; } if (this.y + ballRadius > canvas.height) { this.dy = -this.dy; } } moveBall() { this.x += this.dx; this.y += this.dy; } };Función de dibujo de bolas en lienzo
const drawBalls = function (x, y) { ctx.beginPath(); ctx.arc(x, y, ballRadius, 0, Math.PI * 2); ctx.fillStyle = `green`; ctx.fill(); ctx.closePath(); };Función de dibujo sobre lienzo con temporizador de intervalos
const draw = function () { ctx.clearRect(0, 0, canvas.width, canvas.clientHeight); drawBalls(ballOne.x, ballOne.y); drawBalls(ballTwo.x, ballTwo.y); ballTwo.moveBall(); ballTwo.checkForWall(); ballOne.moveBall(); ballOne.checkForWall(); } let interval = setInterval(draw, 10);¿Hay alguna manera de implementar el código en la clase anterior para verificar si las bolas construidas a partir de esta clase han chocado o no con otras bolas construidas a partir de la misma clase de la misma manera que puedo verificar la colisión con las paredes del lienzo? ¿Necesito crear una clase principal y una clase secundaria?