Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania bez odpowiedzi
Viewing all articles
Browse latest Browse all 21942

PROCESSING Bład poczas dodawania planet

$
0
0

Witam, zrobiłem symulator grawitacji, cała mechanika działa...  Tylko po ostatnim "updatecie" obiekty nie chcą się dodawać. Myśle że problem siedzi w usuwaniu obiektów 

///główny program 

ArrayList<Planet> b;
float G=0.667;
PVector mouse= new PVector(0,0);
void setup(){
  size(displayWidth,displayHeight,OPENGL);
  b= new ArrayList<Planet>();
}
void draw(){
 background(255);
 for(int i = b.size()-1 ; i>=0;i--){
   for(int j = b.size()-1 ; j>=0;j--){
     if(j!=i){ 
       b.get(j).applyForce(b.get(i));
       if(b.get(j).collision(b.get(i)) ==true){
         b.remove(b.get(i));
       }
     }    
   }
 } 
for(int k = b.size()-1;k>=0 ; k--){
  b.get(k).update();
  b.get(k).show();
  b.get(k).ForceReset();
 } 
}
void mousePressed(){
  b.add(new Planet(random(10,30),mouseX,mouseY));
}


///KLASA 
class Planet{
  float radius;
  float mass;
  
  PVector loc,vel,acc,buf;

  Planet(float r, float x,float y){
    radius= r;
    loc = new PVector(x,y);
    vel = new PVector(random(-5,5),random(-5,5));
    acc = new PVector(0,0);
    buf = new PVector(0,0);
  }
  void update(){
    mass=radius*radius*3.23;
    vel.add(acc);
    loc.add(vel);
    acc.mult(0);
  }
  void show(){
    fill(0);
    ellipse(loc.x,loc.y,radius*2,radius*2);
  }
 void applyForce(Planet h){
   float f;
   f=(G*mass*h.mass)/pow(PVector.dist(loc,h.loc),2);
    buf.set(h.loc);
    buf.sub(loc);
    acc.add(buf);
    acc.setMag(f/mass);
    acc.limit(40); 
  }
  void ForceReset(){
    acc.setMag(0);;
  }
  boolean collision(Planet q){
    if(PVector.dist(loc,q.loc)<radius+q.radius){
      radius=sqrt(((radius*radius*PI)+(q.radius*q.radius*PI))/PI);
      return true;
    }
    else{
       return false;
    }
  }
}

 


Viewing all articles
Browse latest Browse all 21942

Trending Articles