Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
JavaScript Image Collision Detection

I'm trying to find out when 2 images collide in a game I made, but I've been receiving conflicting advice and I'm not exactly sure how to get any of the solutions to work (as all of them result in errors, and the collision results as null)

here are the two options I've been trying to solve (any solution works really, I'm just stumped and can't seem to get it to work regardless of what I try).

// first option
function trackXY() {
  for (let i = 0; i < 2; i++) {
    let playerrr = document.getElementById('moveAva');
    let pikaTest = document.getElementById('pikaLocation' + i);

    let playHeight = window
      .getComputedStyle(playerrr)
      .getPropertyValue('height');
    let playWidth = window.getComputedStyle(playerrr).getPropertyValue('width');
    let playLeft = window.getComputedStyle(playerrr).getPropertyValue('left');
    let playTop = window.getComputedStyle(playerrr).getPropertyValue('top');

    let pokeHeight = window
      .getComputedStyle(pikaTest)
      .getPropertyValue('height');
    let pokeWidth = window.getComputedStyle(pikaTest).getPropertyValue('width');
    let pokeLeft = window.getComputedStyle(pikaTest).getPropertyValue('left');
    let pokeTop = window.getComputedStyle(pikaTest).getPropertyValue('top');
    let xPlayer = parseInt(playLeft + 0.5 * playWidth);
    let yPlayer = parseInt(playTop + 0.5 * playHeight);
    let xEnemy = parseInt(pokeLeft + 0.5 * pokeWidth);
    let yEnemy = parseInt(pokeTop + 0.5 * pokeHeight);
    let rPlayer = 0.5 * parseInt(playHeight);
    let rEnemy = 0.5 * parseInt(pokeHeight);
    let dX = xEnemy - xPlayer;
    let dY = yEnemy - yPlayer;
    let distance = Math.sqrt(dX * dX + dY * dY);
    if (distance <= rEnemy + rPlayer) {
      alert('collison!');
      playHeight = parseInt(playHeight) + 0.5 * parseInt(pokeHeight);
      document.getElementById('pikaInvent').style.display = 'block';
      document.getElementById('pikaWon').style.display = 'block';
    }
  }
}
//second option
function trackXY() {
  for (let i = 0; i < 1; i++) {
    let playerrr = document.getElementById('moveAva');
    let pikaTest = document.getElementById('pikaLocation' + i);

    let xPlayer = parseInt(playerrr.style.left + 0.5 * playerrr.style.width);
    let yPlayer = parseInt(playerrr.style.top + 0.5 * playerrr.style.height);
    let xEnemy = parseInt(pikaTest.style.left + 0.5 * pikaTest.style.width);
    let yEnemy = parseInt(pikaTest.style.top + 0.5 * pikaTest.style.height);
    let rPlayer = 0.5 * parseInt(playerrr.style.height);
    let rEnemy = 0.5 * parseInt(pikaTest.style.height);
    let dX = xEnemy - xPlayer;
    let dY = yEnemy - yPlayer;
    let distance = Math.sqrt(dX * dX + dY * dY);
    if (distance <= rEnemy + rPlayer) {
      alert('collison!' + enemy.id);
    }
  }
}
trackXY();

if anyone could help, I could really appreciate it. Or, if you have another solution for collision detection with 2 rectangular images. Thanks!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda