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

181
Views
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
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!