samedi 22 mai 2021

Processing - I'm experiencing problems whilst making conditions for a ball bouncing within a screen

The ball starts from the middle of the screen and is supposed to bounce up and down inside the screen continuously. However, when the ball reaches the top, it gets stuck and the value for 'y' keeps reversing between negative and positive. Any help would be appreciated from a beginner.


float y = 200; 
float vy;
float x;

void setup() {
  size(400, 400);
  x = width/2;
}

void draw() {
  background(230);
  
  vy = 1.5;

  fill(230, 50, 0);
  ellipseMode(CENTER); 
  ellipse(x, y, 30, 30);
  
  if( y > height || y < 0 ){
    vy = vy * -1;
  }
  
  y = y - vy;
}

Aucun commentaire:

Enregistrer un commentaire