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

131
Vistas
Why is my ball sinking into the floor instead of bouncing back up?

I don't understand why it's just sinking into the floor. I'm guessing it's because it glitches below the floor, and then the velocity goes negative for an infinite time. I just don't know how to fix it

var gravity = 9.8
var velocity = 5;
var circleOne;

function setup() {
  createCanvas(400, 400);
  circleOne = new newCircle(200, 50, 50, "black");
}

function draw() {
  background("white");
  circleOne.display();
  circleOne.bounce();
}


class newCircle {
  constructor(x,y,radius,color) {
    this.x = x
    this.y = y
    this.radius = radius
    this.color = color
    this.velocity = 1;
  }
  
  display() {
    fill(this.color)
    ellipse(this.x,this.y,this.radius);
  }
  
  bounce() {
    if (this.y + this.radius > height) {
      this.velocity = -this.velocity * 0.5
    }
    else if (this.y < 0) {
      this.velocity = -this.velocity * 0.5
    }
    else  {
      this.velocity += 1;
    }
    this.y += this.velocity
    
  }
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have told the ball to flip the velocity every time it reaches below a certain point. But flipping the velocity does not necessarily mean that the ball will be above the threshold the next frame. So if the ball is below the threshold for two consecutive frames, it will flip twice, making the velocity exactly how it was two frames ago.

The ball will sink because the velocity keeps flipping every frame once it's too low.

You should set an extra boolean to true when you flip the velocity, then make sure to not flip it again as long as this boolean is true. You can set the boolean back to false once the ball has gone far enough up above the threshold.

However, if your goal is to make a somewhat more sophisticated physics engine, I highly recommend you to read this article. It has helped me a ton in the past while working on my own physics engine.

about 4 years ago · Juan Pablo Isaza Denunciar
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