Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
float ballPosY[] = new float[3];
float ballPosX[] = new float[3];
float velocities[] = new float[3];
float lastPositions[] = new float[3];

PVector barPosition;

float velocityY = 0, gravity = 0.2;


void setup ()
{
  size(500, 500);
  barPosition = new PVector(width/2, 400);
  ballPosX[0] = 100;
  ballPosX[1] = 200;
  ballPosX[2] = 300;
  for (int i = 0; i < ballPosY.length; i++)
  {
    ballPosY[i] = random(0, 150);
  }
}

void draw()
{
  background(0);

  fill(255);

  for (int i = 0; i < ballPosY.length; i++)
  {
    ellipse(ballPosX[i], ballPosY[i], 50, 50);
  }

  rectMode(CENTER);
  rect(mouseX, barPosition.y, 70, 20);

  moveEllipses();
  checkCollision();
}

void checkCollision()
{
  for (int i = 0; i < ballPosX.length; i++)
  {
    if (abs(mouseX - ballPosX[i]) <= 30 )
    {
      if (ballPosY[i] > 500)
      {
      } else if (ballPosY[i] >= 400)
      {
        ballPosY[i] = 400;
        velocities[i] *= -1;
      }
    }
  }
}


void moveEllipses()
{
  for (int i = 0; i< ballPosY.length; i++)
  {  
    velocities[i] += gravity;

    ballPosY[i] += velocities[i];
  }
}