We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › random Color from palette
Page Index Toggle Pages: 1
random Color from palette (Read 606 times)
random Color from palette
Nov 13th, 2008, 4:49am
 
I've been playing with the bouncing bubbles example on processing, done a few diffeerent things successfully but now I am stuck. I want to make each ball to be assigned one of 3 colours randomly. I've tried to do this by making a palette of my 3 colours, then trying to add in a random(int) as a value of balls[i]. I was hoping that then each ball would have an int(index) attached to it to be read by fill(palette[index]); at the end.

Thus doesn't work (obviously, hence the post), the message a receive is "The constructor bubbles.Ball(float,float,floatflot,int,bubbles.Ball[]) is undefined". where bubbles is the name of the file.


any help with this would be much appreciated? Also if there would have been an easier way to do it?
code is below,

cheers,
Paddy



int numBalls = 20;

float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
color[] palette=new color[3];


float radius = 45, rotAngle = -90;
float accelX, accelY;
float springing = .005, damping = .95;



void setup()
{
 size(640, 600);
 noStroke();
 smooth();
 

palette[0]=color(0,255,255,200);
palette[1]=color(255,0,255,200);
palette[2]=color(255,255,0,200);

 for (int i = 0; i < numBalls; i++) {
   balls[i] = new Ball(random(width), random(height), random(20, 40),random(int(3)), i, balls);
 }

 
}

void draw()
{
 background(0);
 for (int i = 0; i < numBalls; i++) {
   balls[i].collide();
   balls[i].move();
   balls[i].display();  
 
 }
 ellipse(mouseX,mouseY,30,30);
}

class Ball {
 float x, y;
 float diameter;
 float vx = 0;
 float vy = 0;
 int index;
 int id;
 Ball[] others;
 
 

 Ball(float xin, float yin, float din, int indexin, int idin, Ball[] oin)  {
   x = xin;
   y = yin;
   diameter = din;
   index = indexin;
   id = idin;
   others = oin;
 
   
 }
 
 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;
   }
float deltaX = mouseX-x;
float deltaY = mouseY-y;



// create springing effect
 deltaX *= springing;
 deltaY *= springing;
 accelX += deltaX;
 accelY += deltaY;

 // move predator's center
 x += accelX;
 y += accelY;

 // slow down springing
 accelX *= damping;
 accelY *= damping;  
}
 
 
 void display() {
 
   
   
fill(palette[index]);
 
   ellipse(x, y, diameter, diameter);
 }
}
Re: random Color from palette
Reply #1 - Nov 13th, 2008, 6:03pm
 
My guess:

random(int(3))

should be:

int(random(3))
Re: random Color from palette
Reply #2 - Nov 13th, 2008, 6:28pm
 
Done, amazing. Thanks a lot mate. I'm only just getting into this programming malarky. Things like this make me wonder if I should.

thanks again.
Page Index Toggle Pages: 1