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 & HelpSyntax Questions › better way to do this:
Page Index Toggle Pages: 1
better way to do this: (Read 415 times)
better way to do this:
Dec 29th, 2008, 9:17pm
 
hello I'm starting to use the processing and I would like to know if you have a better way to write this code, mainly a way to get manipulate the values of the array

int ballCount = 5;
int ballSpeed = 3;

float[]ballSize = new float[ballCount];
float[]xpos = new float[ballCount];
float[]ypos = new float[ballCount];
float[]xspeed = new float[ballCount];
float[]yspeed = new float[ballCount];

void setup() {

 size (500,500);
 background(0);

 smooth();

 for(int i=0; i<ballCount; i++){

   ballSize[i] = random(5,30);
   xpos[i] = random(ballSize[i]/2,width-ballSize[i]/2);
   ypos[i] = random(ballSize[i]/2,height-ballSize[i]/2);

   xspeed[i] = random(1, ballSpeed);
   yspeed[i] = random(-ballSpeed, ballSpeed);

   frameRate(24);
 }
}

void draw (){

 background(0);
 fill(255,50);

 noStroke();

 for(int i=0; i<ballCount; i++){
   
   
   ellipse(xpos[i], ypos[i], ballSize[i], ballSize[i]);

    xpos[i]+=xspeed[i];
   ypos[i]+=yspeed[i];

   if (xpos[i]+ballSize[i]/2>=width || xpos[i]<=ballSize[i]/2){
     xspeed[i]*=-1;
   }

   if (ypos[i]+ballSize[i]/2>=height || ypos[i]<=ballSize[i]/2){
     yspeed[i]*=-1;
   }
 }

 
 for (int i=0, j=i++; i<ballCount; i++, j++){

   stroke(255,50);
   line(xpos[i]-xspeed[i],ypos[i]-yspeed[i],xpos[j]-xspeed[j],ypos[j]-yspeed[j]);
 
 }
}

Re: better way to do this:
Reply #1 - Dec 29th, 2008, 9:40pm
 
I would better do it in object-oriented programming style. Same result but the code is clearer and you don't have to bother with so many arrays.

Quote:
int ballCount = 5;
int ballSpeed = 3;
Ball[] balls; // array of balls

void setup() {
 size(500, 500);
 smooth();
 balls = new Ball[ballCount]; // set up the array
 for(int i=0; i<ballCount; i++) {
   balls = new Ball(); // set up each item of the array
 }
 frameRate(24);
}

void draw (){

 background(0);
 fill(255,50);
 noStroke();

 for(int i=0; i<ballCount; i++) {
   balls[i].move(); // move each ball
   balls[i].display(); // draw each ball
 }

 stroke(255,50);
 for (int i=0; i < ballCount-1; i++) {
   // draw a line between 2 consecutive balls
   line(balls[i].xpos, balls[i].ypos, balls[i+1].xpos, balls[i+1].ypos);
 }

}

// custom class
class Ball {

 // parameters
 float xpos, ypos, xspeed, yspeed, ballSize;

 // constructor (called by 'new Ball()' instruction)
 Ball() {
   ballSize = random(5,30);
   xpos = random(ballSize/2,width-ballSize/2);
   ypos = random(ballSize/2,height-ballSize/2);
   xspeed = random(1, ballSpeed);
   yspeed = random(-ballSpeed, ballSpeed);
 }

 // method you'll call to move the ball
 void move() {
   xpos += xspeed;
   ypos += yspeed;
   if (xpos + ballSize/2 >= width || xpos <= ballSize/2) {
     xspeed *= -1;
   }
   if (ypos + ballSize/2 >= height || ypos <= ballSize/2){
     yspeed*=-1;
   }
 }

 // method you'll call to draw the ball onto the screen
 void display() {
   ellipse(xpos, ypos, ballSize, ballSize);
 }

}


Note : You can use [i][ code ] [ /code ]
tags (without spaces) to embed your code, or the Tools > Copy for Discourse menu item in Processing.
Re: better way to do this:
Reply #2 - Dec 30th, 2008, 2:28am
 
I understood the code, but I am still not accustomed to thinking
that way, but seems to be much better ...

appeared this error:

can not convert from sketchs.Ball to sketch.Ball []
Re: better way to do this:
Reply #3 - Dec 30th, 2008, 3:43am
 
Quote:
int ballCount = 10;
int ballSpeed = 3;
Ball[] balls; // array of balls

void setup() {
  size(500, 500);
  smooth();
  balls = new Ball[ballCount]; // set up the array
  for(int i=0; i<ballCount; i++) {
    balls[i] = new Ball(); // set up each item of the array
  } 
  frameRate(24);


void draw (){

  background(0);
  fill(255,50);  
  noStroke();

  for(int i=0; i<ballCount; i++) {
    balls[i].move(); // move each ball
    balls[i].display(); // draw each ball
  } 

  stroke(255,50);
  for (int i=0; i < ballCount-1; i++) {
    // draw a line between 2 consecutive balls
    line(balls[i].xpos, balls[i].ypos, balls[i+1].xpos, balls[i+1].ypos);
    //connect first ball with the last ball
    line(balls[0].xpos, balls[0].ypos, balls[ballCount-1].xpos, balls[ballCount-1].ypos);
  } 



// custom class 
class Ball {

  // parameters
  float xpos, ypos, xspeed, yspeed, ballSize;

  // constructor (called by 'new Ball()' instruction)
  Ball() { 
    ballSize = random(5,30);
    xpos = random(ballSize/2,width-ballSize/2);
    ypos = random(ballSize/2,height-ballSize/2);
    xspeed = random(1, ballSpeed);
    yspeed = random(-ballSpeed, ballSpeed);
  } 

  // method you'll call to move the ball
  void move() {
    xpos += xspeed; 
    ypos += yspeed; 
    if (xpos + ballSize/2 >= width || xpos <= ballSize/2) {
      xspeed *= -1; 
    } 
    if (ypos + ballSize/2 >= height || ypos <= ballSize/2){
      yspeed*=-1; 
    } 
  } 

  // method you'll call to draw the ball onto the screen
  void display() {
    ellipse(xpos, ypos, ballSize, ballSize);
  } 

}



Re: better way to do this:
Reply #4 - Dec 30th, 2008, 3:51am
 
how can I do to detect a collision between the balls?
Re: better way to do this:
Reply #5 - Dec 30th, 2008, 4:46pm
 
Hahahaha

ahem.

The function "proximity" in the following code can be used to detect if balls are touching.

Code:

float x = 200;
float y = 200;
float radius0 = 20;
float radius1 = 50;

void setup(){
size(400, 400);
}
void draw(){
background(0);
fill(255);
ellipse(x, y, radius0 * 2, radius0 * 2);
if(proximity(mouseX, mouseY, x, y, radius0 + radius1)){
fill(255, 0, 0);
} else {
fill(255);
}
ellipse(mouseX, mouseY, radius1 * 2, radius1 * 2);
}
boolean proximity(float x0, float y0, float x1, float y1, float len){
return (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) <= len * len;
}


To resolve a collision requires much more complicated code. But I believe it's been covered on this board before.
Page Index Toggle Pages: 1