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 › Classes and ArrayList
Page Index Toggle Pages: 1
Classes and ArrayList (Read 1368 times)
Classes and ArrayList
Jan 11th, 2010, 9:46am
 
Classes and ArrayList

As a beginner in OOP, I tried to make a little traffic simulation
with cars and lorries. They shall learn to stop at the crossing, wait for
other vehicles and so on. Up to now I have this:
- A class Vehicle with methods to update position, accelerate or brake,
turn to left or right and and basic display method.
- Two subclasses LKW and PKW with different display methods (and later on
 other additional methods), who inherit the vehicle methods.
That works well so far.
Now I added a class
Vehicle system to initialize an arraylist containing the LKW and PKW and later
on having methods to deal with the interaction of vehicles.

Now my Problem:
After starting the vehicle system with more than one vehicle, there are cars and lorries with different colors , but they are all displayed at the same position and their individual speed seems to add up.
What am I doing wrong?
Code:
class Vehicle_System{
 ArrayList vehicles;
 int num;

 Vehicle_System(int num){
   this.num=num;
   vehicles=new ArrayList();
   PVector xx=new PVector(0,0);
   PVector yy=new PVector(0,0);
   color c=color(0,0,0);
   for(int i=0;i<num;i++){
     //initializing code
     c=color(random(10,255),random(10,255),random(10,255));
     if (random(0,1) < 0.5) {
       xx.set(float(300+i*50),float(300),0);
       //position
       yy.set(random(1,2),0,0);
       //speed
       vehicles.add(new PKW(xx,yy,4.0,2.0,0.25*TWO_PI,c));
     }
     else {
       xx.set(float(300),float(300),0);
       yy.set(0,random(1,1.2),0);
       vehicles.add(new LKW(xx,yy,2.5,1.5,0.50*TWO_PI,c));
       //all vehicles get the same position-vector ort
       // and the speed seems to add up, same for all.
       //display and "abbiegen"="turn to ..." works separately.
       //but speed and position???
     }
   }
 }
 void update(){
   for(int i=0;i<vehicles.size();i++){
     Vehicle v = (Vehicle) vehicles.get(i);
     if(abs(v.ort.x-300)<1&&abs(v.ort.y-300)<1){
       v.ort.x=300;
       v.ort.y=300;
       //get rid of rounding errors
       v.abbiegen(ceil(random(0,2))*PI/2);
     }
     backgr();
     v.update();
     //update-method of class vehicle
     v.display();
     //display-method of subclass LKW or PKW
     //seems to use the appropriate display-methods
     //it displays LKW and PKW, not generic method
   }
 }
 void backgr(){
    background(100);
    fill(0);
    noStroke();
    rect(300,300,600,60);
    rect(300,300,60,600);
 }
}
Re: Classes and ArrayList
Reply #1 - Jan 11th, 2010, 10:18am
 
It might help if you post the code for the two vehicle classes LKW and PKW...

I'm slightly confused as to why you define 'xx' and 'yy' as PVectors.  Surely these are the x and y coordinates of your vehicle?  If so I would expect the argument to be either a single PVector or two separate floats; not two separate PVectors.  Alternatively I guess one is the position and the other a speed vector?  In which case you should name them more logically.

From a quick look over it I don't see any obvious errors - the complete listing might help if it's not too long Wink
Re: Classes and ArrayList
Reply #2 - Jan 11th, 2010, 10:55am
 
you are totally right: xx and yy are position and speed and indeed I should use more precise names.
Here are the classes:

Code:
class Vehicle
{
 //--------------------------------fields------------------------
 PVector ort,geschw;
 float maxspeed,travelspeed,angle_to_go;



 Vehicle( PVector ort, PVector geschw,  float maxspeed, float travelspeed , float angle_to_go  ){
   //------------------------------------constructor---------------
   this.ort=ort;
   this.geschw=geschw;
   this.maxspeed=maxspeed;
   this.travelspeed=travelspeed;
   this.angle_to_go=angle_to_go;
 
 }
 //methods
//  float angle(PVector geschw){
//    //----------------------------angle----------------------------------
//    float w;
//    if(geschw.mag()==0 ) {
//      println("v=Null, kein Winkel möglich");
//      return w=100;
//    }
//    PVector normy=new PVector(0,-1);
//    w=PVector.angleBetween(normy,geschw);
//    return w;
//  }
 void acc(float wanted_speed){
   //------------------------------acc--------------------------------
   //very simple for a start,not smooth!
   geschw.set(wanted_speed*sin(angle_to_go),wanted_speed*cos(angle_to_go),0);
 }

 void display(){
   // to override by subclasses----------------display
   stroke(0);
   fill(200);
   ellipse(ort.x,ort.y,5,5);
 }

 void abbiegen(float turn_angle){
   //-----------------------------abbiegen zu erweitern mit
   float vv=geschw.mag();
   angle_to_go=angle_to_go+turn_angle;
   if(angle_to_go>TWO_PI)angle_to_go=angle_to_go-TWO_PI;
   geschw.set(vv*sin(angle_to_go),vv*cos(angle_to_go),0);
 }

 void update(){
   //------------------------update
   ort.set(ort.x+geschw.x,ort.y+geschw.y,0);
   checkbounds(ort);
 }
}
void checkbounds(PVector ort){
 //------------------------------checkbounds
 if(ort.x>width+20) ort.set(-20,ort.y,0);
 if(ort.y<-20) ort.set(ort.x,height+20,0);
 if(ort.x<-20) ort.set(width+20,ort.y,0);
 if(ort.y>height+20) ort.set(ort.x,-20,0);
}






Code:
class PKW extends Vehicle
{
 color c;
 PKW(PVector   ort, PVector geschw,  float maxspeed, float travelspeed , float angle_to_go,color c){
    super(ort,geschw,maxspeed,travelspeed,angle_to_go);

   this.c=c;

   
 }
  void display(){
   //------------------display PKW---overrides Vehicles.display
 //  if(millis()%6000==0) this.abbiegen(angle_to_go+PI/2);//JUST FOR TESTING
  // println("PKW: "+geschw.x);
    float x,y;
   x=ort.x;
   y=ort.y;
 stroke(0);
 strokeWeight(1);
 fill(c);
 beginShape();
 vertex(x +20*sin( angle_to_go+radians(30)),y +20*cos( angle_to_go+radians(30)));
 vertex(x +20*sin( angle_to_go+radians(-30)),y +20*cos( angle_to_go+radians(-30)));
 vertex(x +20*sin( angle_to_go+radians(30+180)),y +20*cos( angle_to_go+radians(30+180)));
 vertex(x +20*sin(angle_to_go+radians(-30+180)),y +20*cos( angle_to_go+radians(-30+180)));
 endShape();
 stroke(0);
 strokeWeight(6);
 line(x +7*sin( angle_to_go+radians(60)),y +7*cos( angle_to_go+radians(60)),x +7*sin( angle_to_go+radians(-60)),y +7*cos( angle_to_go+radians(-60)));
 strokeWeight(4);
 line(x +8*sin( angle_to_go+radians(40+180)),y +8*cos( angle_to_go+radians(40+180)),x +8*sin( angle_to_go+radians(-40+180)),y +8*cos( angle_to_go+radians(-40+180)));
 stroke(255);
 strokeWeight(2);
 ellipse(x +18*sin( angle_to_go+radians(30)),y +18*cos( angle_to_go+radians(30)),2,2);
 ellipse(x +18*sin( angle_to_go+radians(-30)),y +18*cos( angle_to_go+radians(-30)),2,2);

}


}


The other class LKW only has a different display method.

Code:

Vehicle_System vsys;

void setup(){
 size(600,600);
 smooth();
 rectMode(CENTER);
 ellipseMode(CENTER);
 vsys=new Vehicle_System(1);
}

void draw(){

 vsys.update();

}


Re: Classes and ArrayList
Reply #3 - Jan 11th, 2010, 12:18pm
 
The mistake is an easy one for newbies to OOP.

You have ONLY created 2 PVector objects
Code:

   PVector xx=new PVector(0,0);
   PVector yy=new PVector(0,0);

and these are being shared by all the vehicles, what you need is for each vehicle to have their own PVectors for speed and position

Change your code in Vehicle_System to
Code:
      if (random(0,1) < 0.5) {
       xx = new PVector(float(300+i*50),float(300),0);
       //position
       yy  = new PVector(random(1,2),0,0);
       //speed
       vehicles.add(new PKW(xx,yy,4.0,2.0,0.25*TWO_PI,c));
     }
     else {
       xx = new PVector(float(300),float(300),0);
       yy = new PVector(0,random(1,1.2),0);
       vehicles.add(new LKW(xx,yy,2.5,1.5,0.50*TWO_PI,c));
       //all vehicles get the same position-vector ort
       // and the speed seems to add up, same for all.
       //display and "abbiegen"="turn to ..." works separately.
       //but speed and position???
     }


This should solve the problem.
Smiley
Re: Classes and ArrayList
Reply #4 - Jan 11th, 2010, 12:43pm
 
Thank you very much!
Re: Classes and ArrayList
Reply #5 - Jan 11th, 2010, 3:05pm
 
oh...I was so stupid! Now I see, I had a background-statement not in the void draw(), but in the display-method. So all vehicles but the last one were unvisible.
Thanks for the help with "new PVector"!
Re: Classes and ArrayList
Reply #6 - Jan 14th, 2010, 2:42pm
 
Now, it seems to work:
http://www.openprocessing.org/visuals/?visualID=6989
Page Index Toggle Pages: 1