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.
Page Index Toggle Pages: 1
class (Read 724 times)
class
Dec 22nd, 2009, 3:01am
 
hi,
i'm trying to figure out how to put this code in to a class, and then call it several times, based on random coordinates, where each class representation will act as it should.

Code:

float easing = 0.05;    // Numbers 0.0 to 1.0
float division = 0.4;   // line length in percentage
float x, y;

void setup() {
 size(400, 400);
 stroke(255);
}

void draw() {
 background(0);
 float targetX = (mouseX-(width/2))*division;
 float targetY = (mouseY-(height/2))*division;
 x += (targetX-x) * easing;
 y += (targetY-y) * easing;
 pushMatrix();
 translate(width/2, height/2);
 line(0, 0, x, y);
 ellipse(x, y, 2, 2);
 popMatrix();
}


now, i've been trying to do it like this, but it still doesn't seem to work. all the lines get attracted by the same coordinate - i want them to be attracted individually, based on their individual "0,0,0" ... don't know if it makes sense?

Code:

// GLOBAL -------------------------------------------------------
int boidsNumber = 50;      // number of boids
float easeVar = 0.05;      // Numbers 0.0 to 1.0
float divisionVar = 0.2;   // line length in percentage
float horizontal = 80;     // horizontal offset from screen (l/r)
float vertical = 100;      // vertical offset from screen (t/b)
float x, y;

float[] ptsX = new float[boidsNumber];    // array for start-pt X
float[] ptsY = new float[boidsNumber];    // array for start-pt Y

Boid boid1;

// SETUP --------------------------------------------------------
void setup() {
 size(500, 800);
 smooth();
 stroke(255);
 ellipseMode(CENTER);
 
 for (int i = 0; i < boidsNumber; i++) {
   ptsX[i] = random(horizontal, float(width)-horizontal);
   ptsY[i] = random(vertical, float(height)-vertical);
   x = ptsX[i];
   y = ptsY[i];
 }
 
 boid1 = new Boid(easeVar, divisionVar, x, y);
}

// DRAW ---------------------------------------------------------
void draw() {
 background(0);
 for (int i = 0; i < boidsNumber; i++) {
   pushMatrix();
   translate(ptsX[i], ptsY[i]);
   boid1.display();
   popMatrix();
 }
}

// CLASS --------------------------------------------------------
class Boid {
 float x_boid, y_boid, targetX, targetY, ease_boid, division_boid;
 
 Boid(float ease_constr, float division_constr, float x_constr, float y_constr) {
   ease_boid = ease_constr;
   division_boid = division_constr;
   x_boid = x_constr;
   y_boid = y_constr;
 }
 
 void display() {
   targetX = (mouseX-(x_boid/2))*division_boid;
   targetY = (mouseY-(y_boid/2))*division_boid;
   x += (targetX-x) * ease_boid;
   y += (targetY-y) * ease_boid;
   line(0, 0, x, y);
   ellipse(x, y, 2, 2);
 }
}


any ideas? thanks in advance.
/claus
Re: class
Reply #1 - Dec 22nd, 2009, 3:21am
 
Not sure if this produces the effect you want; but the principle is at least correct.  Rather than storing each Boid's origin in a points array (assuming I've interpreted your intention correctly) - just store the coordinates in the Boid object and move the pushMatrix stuff to the Boid display method:

Code:
// GLOBAL -------------------------------------------------------
int boidsNumber = 50; // number of boids
float easeVar = 0.05; // Numbers 0.0 to 1.0
float divisionVar = 0.2; // line length in percentage
float horizontal = 80; // horizontal offset from screen (l/r)
float vertical = 100; // vertical offset from screen (t/b)
float x, y;

Boid boid[];

// SETUP --------------------------------------------------------
void setup() {
size(500, 800);
smooth();
stroke(255);
ellipseMode(CENTER);
boid = new Boid[boidsNumber];

for (int i = 0; i < boidsNumber; i++) {
float originX = random(horizontal, float(width)-horizontal);
float originY = random(vertical, float(height)-vertical);
boid[i] = new Boid(easeVar, divisionVar, x, y,originX,originY);
}
}

// DRAW ---------------------------------------------------------
void draw() {
background(0);
for (int i = 0; i < boidsNumber; i++) {

boid[i].display();

}
}

// CLASS --------------------------------------------------------
class Boid {
float x_boid, y_boid, targetX, targetY, ease_boid, division_boid;
float originX,originY;

Boid(float ease_constr, float division_constr, float x_constr, float y_constr, float originX, float originY) {
ease_boid = ease_constr;
division_boid = division_constr;
x_boid = x_constr;
y_boid = y_constr;
this.originX = originX;
this.originY = originY;
}

void display() {
targetX = (mouseX-(x_boid/2))*division_boid;
targetY = (mouseY-(y_boid/2))*division_boid;
x += (targetX-x) * ease_boid;
y += (targetY-y) * ease_boid;
pushMatrix();
translate(originX, originY);
line(originX, originY, x, y);
ellipse(x, y, 2, 2);
popMatrix();
}
}

Re: class
Reply #2 - Dec 22nd, 2009, 3:29am
 
One other point:  If you're passing the same easeVar and divisionVar value to all Boids you might as well leave it as a global variable and remove it from the Boid object; unless of course this is going to change in a later incarnation Wink
Re: class
Reply #3 - Dec 22nd, 2009, 3:36am
 
thanks for quick reply, blindfish.

i think it begins to look something like i imagined. i just need to understand exactly what it is you changed.

oh, and just to be sure, regarding this.originX and this.originY. 'this' is to refer to the originX/Y in the class you're in, when you write it, right? so it doesn't refer to the global variable?

thanks!
Re: class
Reply #4 - Dec 22nd, 2009, 3:57am
 
More or less - it's just a habit I picked up.  I don't like to write different parameter names in the class Constructor, so the 'this' is required to reference the class variable as opposed to the parameter being passed.  Here's a demonstration:

Code:
// I've added these global variables for demonstration purposes:
float x = 1;
float y = 2;

MyClass myClassInstance;

void setup() {
 size(100,100);
 myClassInstance = new MyClass(50,30);
 myClassInstance.printMe();
}

class MyClass{
 // class properties
 float x;
 float y;

 // constructor
 MyClass(float x, float y) {
   // so 'this.x' is the class variable x and 'x' is the parameter passed in the constructor (NOT the global variable x)
   this.x = x;
   this.y = y;
 }
 
 // methods...
 void printMe() {
   // note that you don't need to use 'this.x' outside of the constructor to reference the class variable 'x'
   // (So global variable names that need to be used inside a class shouldn't clash with class variable names.)
   println("x: " + x + "  y: " + y);
 }
 
}



Re: class
Reply #5 - Dec 22nd, 2009, 4:07am
 
okay, thanks.

makes sense and provides consistency that you're able to call them the same. those different variable names makes it confusing.

but i guess that you have to be able to see and know that the global variables (x and y) has NOTHING to do with the declared variables (x and y) in the class.

great explanation!
cheers.
Re: class
Reply #6 - Dec 22nd, 2009, 4:26am
 
I'm still fairly new to all this class-based stuff, but I'm getting there.  TBH on a large project I don't know whether I'd rely on global variables (i.e. declared before setup).  Instead I might create a class to hold them and create an instance of this class and reference the variables in the instance, even possibly passing a reference to it to other classes if necessary.  That way you know exactly where the variables are coming from and I imagine that's the approach you have to take in pure Java...

I wouldn't say I've done everything 'the right way' but my Asteroids game was built to test these principles so might be useful for reference (though I'm sure there's far better reference material out there Wink ).
Page Index Toggle Pages: 1