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

189
Views
Hitbox bug in javascript

I'm making a videogame in javascript (without canvas because it's an assignement and teacher forbid us to us canvas). I'm making the hitbox right now and i run into some bug. the one i wanna talk about is this one:

Picture 1 picture 2
When i load the page the character spawns in the air and fall due to a gravity function:

//* GRAVITY

let force = setInterval(gravity, 10);

let fall = true;

function gravity(){
    if(fall){
        // loop that multiplie the velocity by 1.05 until it's at 40
        if (gforce < 20) gforce = gforce * 1.05;

        // Apply gravity to character
        persoY = persoY + gforce;
        persoId.style.top = persoY + 'px';

        //! debugging
        console.log(gforce);
    }
}

Then when it touches the ground it stops falling because of my hitbox function:

let touch = true;

function coll(){
    if(touch){
        let persoY = persoId.offsetTop,
            persoX = persoId.offsetLeft,
            persoW = persoId.offsetWidth,
            persoH = persoId.offsetHeight,
            islandY = islandHitbox.offsetTop,
            islandX = islandHitbox.offsetLeft,
            islandW = islandHitbox.offsetWidth,
            islandH = islandHitbox.offsetHeight;
        if (persoX < islandX + islandW &&
            persoX + persoW > islandX &&
            persoY < islandY + islandH &&
            persoY + persoH > islandY) {
            fall = false;
        } else {
            fall = true;
        }
    }
    requestAnimationFrame(coll);
}
coll();

Now the problem is that when the page load and the player fall there's a 50% chance he stop falling right when he touches the hitbox like in Picture 2 or another 50% chance that he clips a bit through the hitbox before he stops falling like in Picture 1 And i really can't understand why pls help me thx u already.

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!