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

190
Visualizações
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 à 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