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

191
Vistas
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
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