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 › instances of classes problem
Page Index Toggle Pages: 1
instances of classes problem (Read 410 times)
instances of classes problem
Dec 12th, 2007, 7:58pm
 
I have a class called spunLine, it contains a set of x,y,z coordinates, held in a class called point3D.

I'm creating new instances of this class by adding new instances to an array held in an emitter class.

My problem is that whenever I change variables (namely the xyz position) it happens to all instances of the spunLine class, so they all end up bunched together.

Under my understanding these variables should be within the scope of an instance, not the whole class - which is what seems to be happening.

How can i ensure that each instance has it's own set of xyz values, instead of just referring to the whole class?

thanks in advance.

Martin
Re: instances of classes problem
Reply #1 - Dec 12th, 2007, 8:23pm
 
hard to say without any code ...

i guess somewhere along the way you are making shallow copies instead of real ones.

MyClass a, b;

a = new MyClass();
b = a;

// now a and b reference the exact same object!
// therefore changes to b will "happen in" a "too"

F
Re: instances of classes problem
Reply #2 - Dec 12th, 2007, 9:21pm
 
Here's the code, in all it's messy glory.

Basically, the emitter checks to see if it's created less lines than it's maximum value, and if it hasn't uses it's position/rotation information to pass onto the new line which is added to the emitter's lines array.

I've tried using the "this" keyword, but this doesn't seem to have any effect.


Code:

*****************spunLine****************************

class spunLine{

float force;
float sX,sY,sZ,eX,eY,eZ;
point3D pos,dim,rot;
color[] cols = new color[0];
warp[] warps = new warp[0];

//constants

final static byte DEBUG = 0;
final static byte BASIC = 1;
final static byte FULL = 2;

spunLine(point3D inPos, point3D inRot, point3D inDim, float inF){
pos=inPos;
rot=inRot;
dim=inDim;
force= inF;

}

void display(byte style){
//work out new position
calcPos();
//work out start and end points
calcPoints();
switch(style){
case 0:
//it's DEBUG Mode
println("sX:"+sX + ", sY:" + sY + ", sZ:"+sZ);
println("eX:"+eX + ", eY:" + eY + ", eZ:"+eZ);
line(sX,sY,sZ,eX,eY,eZ);
break;
case 1:
//it's BASIC mode
line(sX,sY,sZ,eX,eY,eZ);
break;
case 2:
//it's full mode
line(sX,sY,sZ,eX,eY,eZ);
break;
}
}

void calcPos(){
this.pos.x=this.pos.x+(sin(this.rot.x)*force);
this.pos.y=this.pos.y+(cos(this.rot.y)*force);
this.pos.z=this.pos.z+(sin(this.rot.z)*force);
}

void calcPoints(){
sX=this.pos.x+(sin(rot.x)*(dim.x/2));
sY=this.pos.y+(cos(rot.y)*(dim.x/2));
sZ=pos.z+(cos(rot.z)*(dim.x/2));
eX=pos.x-(sin(rot.x)*(dim.x/2));
eY=pos.y-(cos(rot.y)*(dim.x/2));
eZ=pos.z-(cos(rot.z)*(dim.x/2));
}

}

*****************Point3D***********************
class point3D{

float x,y,z;

point3D(float iX, float iY, float iZ){
x=iX;
y=iY;
z=iZ;
}
}

*******************Emitter Class***********************


class lineEmit {
float power,rate,randomness;
point3D pos,rot,dim;
int maxLines, lastUsedFrame;

// these params are used to automatically pass data to new lines created
color[] cols = new color[0];
spunLine[] lines = new spunLine[0];
warp[] warps = new warp[0];



lineEmit(point3D inPos, point3D inRot, point3D inDim, float iPow,int inMax){
pos=inPos;
rot=inRot;
dim=inDim;
power=iPow;
//rate in millis
rate=120;
//how much variation is allowed in creating new lines
randomness = 10;
maxLines = inMax;
}

void update(){
//this function checks whether it's ok to produce another line compared to the max allowed and
//whether it's been enough time since the last go
if ((frameNo-lastUsedFrame) > ((rate-(randomness/2) + random(randomness)))){
//it's ok to go
lastUsedFrame=frameNo;
if (lines.length < maxLines){

*************Here's where it creates a new line***********

//we can produce a new line using point3D information attached to the emitter
spunLine myNewLine = new spunLine(pos,rot,dim,power);

lines = (spunLine[]) append(lines,myNewLine);
myNewLine=null;
}
}
for(int i=0; i < lines.length; i++){
lines[i].display(spunLine.BASIC);
}
if (lines.length>0){
println("0:" + lines[0].pos.y);
if (lines.length>1){
println("1: " + lines[1].pos.y);
}
}
}

void addColor(color inCol){
//adds a colour to the cycle
cols = append(cols,inCol);
}

void setDir(float idX,float idY,float idZ){
rot=new point3D(idX,idY,idZ);
}

point3D getDir(){

return rot;
}
}

*****************HOW the emitter is set up **************

point3D inPos = new point3D(0,0,0);
point3D inRot = new point3D(0,0,0);
point3D inDim = new point3D(50,5,5);
myEmit = new lineEmit(inPos, inRot, inDim, 0.1,3);


*************Called each draw cycle ****************

myEmit.update();


thanks for the feedback

Martin

EDIT:Just cleaned a couple of things up
Re: instances of classes problem
Reply #3 - Dec 12th, 2007, 9:31pm
 
I think (and this is just a guess at the moment) that all your objects are just getting a reference to your rot pos and dim Point3Ds, and so when any spunLine updates the pos/rot/dim it changes the actual object, which all spunLnes have a reference to.
Re: instances of classes problem
Reply #4 - Dec 12th, 2007, 9:35pm
 
(wow quick reply!)

Yeah, that's what seems to be happening, like each instance is referencing the pos/rot/dim data as if it's a static variable, or referencing an outside data source.

Any ideas how i can make it so it only effect's the line's own pos/rot/dim data and not the classes?
Re: instances of classes problem
Reply #5 - Dec 12th, 2007, 10:00pm
 
One way is to add a copy() method to your Point3D class:

Code:
Point3D copy()
{
return new Point3D(x,y,z);
}


This will work since ints floats and other primitives are always absolutely copied, not just have a reference passed.

Re: instances of classes problem
Reply #6 - Dec 12th, 2007, 10:20pm
 
EDIT:Great that worked! i added the copy function to the Point3D class, and then used it as it copied the vectors in from the emitter class.

like so;
Code:

pos=inPos.copy();
rot=inRot.copy();
dim=inDim.copy();


Thanks for the help, it's a bit weird that it doesn't copy them normally.

Either way, it's fixed now, so i can move on!

Cheers!
Martin
Page Index Toggle Pages: 1