calling a constructor from an object function
in
Programming Questions
•
1 year ago
Hi. I have a sketch in wich I create an arraylist of balls that flow in a pipe. Now I want to create diferent pipes, so I created a pipe class. I had a line inside the draw function that created new balls and was this:
balls.add(new ball(int(random(leftborder,rightborder)), int(random(0,height))));
and woked correctly.
Now I´ve moved this line inside the pipe class, for the balls to be created inside their pipe. The line is inside a function called inicialize() inside the pipe class.
A message appears:
the constructor pipes.ball (int, int) is undefined.
Pipes is the name of the file.
This is the complete code:
class Pipe{
float leftborder;
float rightborder;
float pipediam;
float middle;
float reslenght = 100;
float respoint;
float vaverage = 4;
// pipe rendered
int pipesize = 60;
float rightside;
float leftside;
Pipe(float leftborder0, float rightborder0, float pipediam0){
leftborder = leftborder0;
rightborder = rightborder0;
pipediam = pipediam0;
rightside = middle + pipesize/2;
leftside = middle - pipesize / 2;
}
void inicialize(){
middle =(leftborder + rightborder)/2;
respoint = height/2;
for (int i = 0; i < population; i++){
balls.add(new ball(int(random(leftborder,rightborder)), int(random(0,height))));
}
}
void bounceAndRender(){
for(int i=0;i<balls.size();i++){
ball b = (ball) balls.get(i);
//separate from walls
if(b.pos.x>rightborder-b.r+c){b.pos.x=rightborder;}
if(b.pos.x<leftborder + b.r-c){b.pos.x=leftborder;}
if(b.pos.y>height-b.r+c){b.pos.y-=5;}
//repulsion between balls
for(int j=i+1;j<balls.size();j++){
ball b2 = (ball) balls.get(j);
PVector dx=PVector.sub(b2.pos,b.pos);
if (dx.mag() < suck){
dx.normalize();
dx.mult(repulsion);
b2.force.add( dx);
dx.mult(-1);
b.force.add( dx);
}
}
//repulsion to walls
if (b.pos.x < suck + leftborder){
b.force.x = b.force.x + repulsion;
}
if (b.pos.x > rightborder - suck){
b.force.x = b.force.x - repulsion;
}
b.update();
// corrections
if (b.v.y < 0){b.v.y = 0;}
b.render();
if (b.pos.y > height){
b.pos.y = 0;
}
}
}
float leftborder;
float rightborder;
float pipediam;
float middle;
float reslenght = 100;
float respoint;
float vaverage = 4;
// pipe rendered
int pipesize = 60;
float rightside;
float leftside;
Pipe(float leftborder0, float rightborder0, float pipediam0){
leftborder = leftborder0;
rightborder = rightborder0;
pipediam = pipediam0;
rightside = middle + pipesize/2;
leftside = middle - pipesize / 2;
}
void inicialize(){
middle =(leftborder + rightborder)/2;
respoint = height/2;
for (int i = 0; i < population; i++){
balls.add(new ball(int(random(leftborder,rightborder)), int(random(0,height))));
}
}
void bounceAndRender(){
for(int i=0;i<balls.size();i++){
ball b = (ball) balls.get(i);
//separate from walls
if(b.pos.x>rightborder-b.r+c){b.pos.x=rightborder;}
if(b.pos.x<leftborder + b.r-c){b.pos.x=leftborder;}
if(b.pos.y>height-b.r+c){b.pos.y-=5;}
//repulsion between balls
for(int j=i+1;j<balls.size();j++){
ball b2 = (ball) balls.get(j);
PVector dx=PVector.sub(b2.pos,b.pos);
if (dx.mag() < suck){
dx.normalize();
dx.mult(repulsion);
b2.force.add( dx);
dx.mult(-1);
b.force.add( dx);
}
}
//repulsion to walls
if (b.pos.x < suck + leftborder){
b.force.x = b.force.x + repulsion;
}
if (b.pos.x > rightborder - suck){
b.force.x = b.force.x - repulsion;
}
b.update();
// corrections
if (b.v.y < 0){b.v.y = 0;}
b.render();
if (b.pos.y > height){
b.pos.y = 0;
}
}
}
void render(){
fill(20,20,2000);
rect (leftside - 4, 0, 4 , (height - reslenght)/2);
rect (rightside, 0, 4 , (height - reslenght)/2);
rect (leftside - 4, (height + reslenght)/2, 4 , (height - reslenght)/2);
rect (rightside, (height + reslenght)/2, 4 , (height - reslenght)/2);
ellipse(leftside - 2 , respoint, 0.5* pipesize, reslenght);
ellipse (rightside + 2, respoint, 0.5 * pipesize, reslenght);
fill (255);
ellipse(leftside - 6 , respoint, 0.5* pipesize, reslenght);
ellipse (rightside + 6, respoint, 0.5 * pipesize, reslenght);
}
}
class ball{
PVector pos;
PVector v;
PVector force;
color c;
int r=3;
float f = 0.1;
float g = 0.1;
Pipe pipe;
ball(int x0, int y0 , Pipe pipe0){
pos=new PVector(x0,y0);
v=new PVector(0,4);
force = new PVector (0,0);
pipe = pipe0;
}
void update(){
v.y = pipe.vaverage * (0.5 +(1 - sq(pos.x - pipe.middle)/sq(pipe.pipediam/2)));
v.add (force);
//add some chaos
v.x = v.x + random(-0.3,0.3);
count++;
//v.y = v.y + g ;
pos.add(v);
v.x = v.x*0.5;
force.x = 0;
force.y = 0;
}
void render(){
float posx;
float posy;
if ((pos.y > pipe.respoint - pipe.reslenght/2)&& (pos.y < respoint + pipe.reslenght/2)){
float xini = pipe.leftside + 0.25 * pipe.pipesize* (1 - abs(pos.y - pipe.respoint) /pipe.reslenght);
posx = xini + (pipe.pipesize - 2 * (xini - pipe.leftside)) * (pos.x - pipe.leftborder)/pipe.pipediam;
}
else
{
posx = pipeleftside + pipe.pipesize * (pos.x - pipe.leftborder)/pipe.pipediam;
}
ellipse(int(posx),int(pos.y),2.5,2.5);
fill(20,20,2000);
rect (leftside - 4, 0, 4 , (height - reslenght)/2);
rect (rightside, 0, 4 , (height - reslenght)/2);
rect (leftside - 4, (height + reslenght)/2, 4 , (height - reslenght)/2);
rect (rightside, (height + reslenght)/2, 4 , (height - reslenght)/2);
ellipse(leftside - 2 , respoint, 0.5* pipesize, reslenght);
ellipse (rightside + 2, respoint, 0.5 * pipesize, reslenght);
fill (255);
ellipse(leftside - 6 , respoint, 0.5* pipesize, reslenght);
ellipse (rightside + 6, respoint, 0.5 * pipesize, reslenght);
}
}
class ball{
PVector pos;
PVector v;
PVector force;
color c;
int r=3;
float f = 0.1;
float g = 0.1;
Pipe pipe;
ball(int x0, int y0 , Pipe pipe0){
pos=new PVector(x0,y0);
v=new PVector(0,4);
force = new PVector (0,0);
pipe = pipe0;
}
void update(){
v.y = pipe.vaverage * (0.5 +(1 - sq(pos.x - pipe.middle)/sq(pipe.pipediam/2)));
v.add (force);
//add some chaos
v.x = v.x + random(-0.3,0.3);
count++;
//v.y = v.y + g ;
pos.add(v);
v.x = v.x*0.5;
force.x = 0;
force.y = 0;
}
void render(){
float posx;
float posy;
if ((pos.y > pipe.respoint - pipe.reslenght/2)&& (pos.y < respoint + pipe.reslenght/2)){
float xini = pipe.leftside + 0.25 * pipe.pipesize* (1 - abs(pos.y - pipe.respoint) /pipe.reslenght);
posx = xini + (pipe.pipesize - 2 * (xini - pipe.leftside)) * (pos.x - pipe.leftborder)/pipe.pipediam;
}
else
{
posx = pipeleftside + pipe.pipesize * (pos.x - pipe.leftborder)/pipe.pipediam;
}
ellipse(int(posx),int(pos.y),2.5,2.5);
//}
}
}
Pipe thepipe;
float count;
int width=800;
int height=600;
float g=2;
ArrayList balls= new ArrayList();
float k=1;//factor to increment distance between balls
float c= 4;//maximun penetrance in walls
//float vlim=10;//maximum speed
float population=1000;//population limit
float suck = 20;// distance of influence
}
}
Pipe thepipe;
float count;
int width=800;
int height=600;
float g=2;
ArrayList balls= new ArrayList();
float k=1;//factor to increment distance between balls
float c= 4;//maximun penetrance in walls
//float vlim=10;//maximum speed
float population=1000;//population limit
float suck = 20;// distance of influence
//float visc;
float repulsion = 0.1;
void setup(){
noStroke();
size(width,height);
thepipe = new Pipe( width/2-600/2, width/2 + 600/2, 600);
thepipe.inicialize();
}
void draw(){
fill (250,150);
thepipe.render();
rect(0,0,width, height);
thepipe.bounceAndRender();
}
1