How can add random color to my project of bouncing balls

Hello everybody, I have spent an insane amount of hours trying to add random color to my project, and I am lost. Can anybody help me please. I would really appreciate it.

Bellow is the coding of the colliding balls project.

//Alejandro Baena
//Exercise 4 - Using objects to model relationships

int numBalls = 60;
Ball[] balls = new Ball[numBalls];
float spring = 0.07;
float gravity = 0.02;
float friction = -1.0;

void setup() {
  size(1200, 700);
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(30, 120), i, balls);
    float r = random(255);
    float g = random(255);
    float b = random(255);

    color myColor = color(r, g, b);
  }


}

void draw() {
  background(0);
  stroke(#bbeeff);
  strokeWeight(3);
  for (int i = 0; i < numBalls; i++) {
    balls[i].collide();
    balls[i].move();
    balls[i].display();  
  }
}

class Ball {

  // some variables that define
  // the x and y position
  // the velocity x and y
  // the ball height/width
  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;
  color ballColor;
  Ball[] others;

  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    // assign incoming values to internal class variables
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
    ballColor = #ffffff;
  } 
  Ball(float xin, float yin, float din, int idin, Ball[] oin, color iballColor) {
    // assign incoming values to internal class variables
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
    ballColor = iballColor;
  }
  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].x - x;
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) { 
        float angle = atan2(dy, dx);
        float targetX = x + cos(angle) * minDist;
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax;
        others[i].vy += ay;
      }
    }   
  }

  void move() {
    vy += gravity;
    x += vx;
    y += vy;
    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction; 
    }
    else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction; 
    } 
    else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
  }

  void display() {
    ellipse(x, y, diameter, diameter);
    fill(ballColor);
  }
}
Tagged:

Answers

  • edited February 2014

    I would set the fill color before drawing - lines 110 and 111 - but that's just me.

  • thank you TfGuy44, I tried it but it didn' t work :((

  • edited February 2014

    Try this as your for loop in the setup:

    for (int i = 0; i < numBalls; i++) {
            float r = random(255);
            float g = random(255);
            float b = random(255);
    
            color myColor = color(r, g, b);
            balls[i] = new Ball(random(width), random(height), random(30, 120), i, balls, myColor);
    
          }
    

    Just wondering, why are you saving all the other balls within each ball (in Ball[] others)? This will take up a lot of memory space and this actually doesn't give you any benefit, since this is a copy of the balls Array at the moment you created each ball, ie. only the initial positions are stored!

  • edited February 2014

    you have two constructors in the class, one with col, the other without

    you used the one without col.

    I changed that by changing your setup()

    also in setup you need to set myColor before adding a new ball with that color

    also as TFGuy44 said, use fill before ellipse in the very last lines

    Looks cool !

    //Alejandro Baena
    //Exercise 4 - Using objects to model relationships
    
    int numBalls = 60;
    Ball[] balls = new Ball[numBalls];
    float spring = 0.07;
    float gravity = 0.02;
    float friction = -1.0;
    
    void setup() {
      size(1200, 700);
      for (int i = 0; i < numBalls; i++) {
        float r = random(255);
        float g = random(255);
        float b = random(255);
    
        color myColor = color(r, g, b);
    
        balls[i] = new Ball(random(width), random(height), random(30, 120), i, balls, myColor);
      }
    }
    
    void draw() {
      background(0);
      stroke(#bbeeff);
      strokeWeight(3);
      for (int i = 0; i < numBalls; i++) {
        balls[i].collide();
        balls[i].move();
        balls[i].display();
      }
    }
    
    class Ball {
    
      // some variables that define
      // the x and y position
      // the velocity x and y
      // the ball height/width
      float x, y;
      float diameter;
      float vx = 0;
      float vy = 0;
      int id;
      color ballColor;
      Ball[] others;
    
      Ball(float xin, float yin, float din, int idin, Ball[] oin) {
        // assign incoming values to internal class variables
        x = xin;
        y = yin;
        diameter = din;
        id = idin;
        others = oin;
        ballColor = #ffffff;
      } 
      Ball(float xin, float yin, float din, int idin, Ball[] oin, color iballColor) {
        // assign incoming values to internal class variables
        x = xin;
        y = yin;
        diameter = din;
        id = idin;
        others = oin;
        ballColor = iballColor;
      }
      void collide() {
        for (int i = id + 1; i < numBalls; i++) {
          float dx = others[i].x - x;
          float dy = others[i].y - y;
          float distance = sqrt(dx*dx + dy*dy);
          float minDist = others[i].diameter/2 + diameter/2;
          if (distance < minDist) { 
            float angle = atan2(dy, dx);
            float targetX = x + cos(angle) * minDist;
            float targetY = y + sin(angle) * minDist;
            float ax = (targetX - others[i].x) * spring;
            float ay = (targetY - others[i].y) * spring;
            vx -= ax;
            vy -= ay;
            others[i].vx += ax;
            others[i].vy += ay;
          }
        }
      }
    
      void move() {
        vy += gravity;
        x += vx;
        y += vy;
        if (x + diameter/2 > width) {
          x = width - diameter/2;
          vx *= friction;
        }
        else if (x - diameter/2 < 0) {
          x = diameter/2;
          vx *= friction;
        }
        if (y + diameter/2 > height) {
          y = height - diameter/2;
          vy *= friction;
        } 
        else if (y - diameter/2 < 0) {
          y = diameter/2;
          vy *= friction;
        }
      }
    
      void display() {
        fill(ballColor);
        ellipse(x, y, diameter, diameter);
      }
    }
    
    
    //
    
  • Wow,

    I am the newby guy here and I have to say. , I am amazed by the fast response from everybody. BIG thanks to all the people that that try to helped me out in this project. I am happy again.

    :)>-

Sign In or Register to comment.