Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
¿Cómo verificar la colisión en el lienzo para objetos creados a partir de la misma clase?

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!