Search code examples
arraysprocessing

Phrases when objects meet on processing


I have been trying to figure out how to add certain words when objects hit each other on my processing code. Would anyone know how I would get my screen to say 'You Have Won' when the red ball that is getting launched touches the black circle on the top half of the screen? I was wondering what needs to be done in order to achieve this.

This is my code:

Ball mainBall; // Horizontal ball
ArrayList<Ball> balls; //Red balls

PVector oldSpeed;

void setup () {  
  size (1280, 960);
  
  //Create main ball
  mainBall = new Ball(200, 700, 3, 0, 140, color(255, 200, 200));
  
  // Create list of balls
  balls = new ArrayList<Ball>();
}

void draw() {
  background(255);

  fill (0);
  rect (0, 550, width, height);
  ellipse(240,240,100,100);
  
  ellipseMode(CENTER);


  mainBall.collisionUpdate();  //reverse speed of ball if hits side window
  mainBall.drawBall();   //Draw the main ball
  
  //Create arraylist to store balls to get removed from the list
  ArrayList<Ball> toRemove = new ArrayList<Ball>();
  
  // For every ball
  for(Ball ball : balls){
    // Balls bounce within the screen
    ball.collisionUpdate();
    
    ball.slowSpeed(random(0.98,0.999)); //  Speed of the ball
    
    ball.drawBall();
    

    if(ball.isNotVisible()){
      toRemove.add(ball); //Removal of ball if off screen
    }
  }
  

  balls.removeAll(toRemove);   // Remove all the non-visible balls
}

void launch(){
  if(!mainBall.isStationary()){
    oldSpeed = mainBall.speed;
    mainBall.setSpeed(0, 0);
  }else{
    // Add a new ball to the list
    balls.add(new Ball(mainBall.position.x, mainBall.position.y, 0, random(-8, -5), 70, color(255, 0, 0)));
    //Make the main ball move again
    mainBall.setSpeed(oldSpeed);
  }
}

void mouseClicked(){
  launch();
}

void keyPressed(){
  launch();
}

// Ball class
class Ball{
  PVector position;
  PVector speed;
  float diameter;
  color c;
  
  Ball(float x, float y, float xSpeed, float ySpeed, float _diameter, color _c){
    position = new PVector(x, y);
    speed = new PVector(xSpeed, ySpeed);
    diameter = _diameter;
    c = _c;
  }
  
  void drawBall(){
    fill(c);
    circle(position.x, position.y, diameter);
    // Update position of ball
    position.add(speed);
  }
  
  void setSpeed(float xSpeed, float ySpeed){
    speed = new PVector(xSpeed, ySpeed);
  }
  
  void setSpeed(PVector _speed){
    speed = _speed;
  }
  
  boolean isStationary(){
    return speed.x == 0 && speed.y == 0;
  }

  void collisionUpdate(){
    if(position.x < diameter/2 || position.x > width-diameter/2 || position.y < diameter/2 || position.y > height-diameter/2 ){
      //Reverse speed
      speed.mult(-1);
    }
  }
  
  void slowSpeed(float deceleration){
      if(speed.mag() < 0.5){
          speed = new PVector(0, 0);
      }else{
          speed.mult(deceleration);
      }
  }
  
  // Return true or false if the ball is off the screen
  boolean isNotVisible(){
     return position.x > width+diameter || position.x < -diameter || position.y > height+diameter || position.y < -diameter;
  }
}

Solution

  • This might work:

    boolean winning = false;
    
    void draw() {
      for (Ball ball: balls) {
        if (sq(ball.position.x - 240) + sq(ball.position.y - 240) < sq(ball.diameter/2 + 50)) {
          winning = true;//test each ball to see if it's close enough to the black one
        }
      }
      if (winning) {
        fill(255, 0, 0);
        stroke(255, 0, 0);
        textSize(70);
        textAlign(CENTER, CENTER);
        text("You Have Won", width/2, height/2);
      }
    }