Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda