(Weiterentwicklung)
(Objekt Ball)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Weiterentwicklung ==
+
''Es läuft wie geschmiert, aber die Bälle sind in der Bewegung oval und stoßen sich nicht gegenseitig ab. Was habe ich bloß falsch gemacht?''
 +
 
 +
== Code A ==
  
 
<source lang="Java">Ball[] ball = new Ball[15];
 
<source lang="Java">Ball[] ball = new Ball[15];
Line 193: Line 195:
 
</source>
 
</source>
  
== Ball ==
+
== Objekt Ball ==
  
 
<source lang="Java">class Ball {
 
<source lang="Java">class Ball {

Latest revision as of 13:35, 26 March 2011

Es läuft wie geschmiert, aber die Bälle sind in der Bewegung oval und stoßen sich nicht gegenseitig ab. Was habe ich bloß falsch gemacht?

Code A

Ball[] ball = new Ball[15];
int a,b,c; //Abstände der Ebenen

void setup() {
  size(1280,1024);
  //size(1920,1200);
  smooth();
  noStroke();
  colorMode(HSB,360,100,100,100);
  
  a = 500; //700 Größe der ersten Ebene
  b = 400; //600 Größe der zweiten Ebene
  c = 40; //60 Lücke
  
  for (int i=0; i < 5; i++) {
    ball[i] = new Ball();
    //in erster Ebene
    ball[i].x = random(ball[i].g1,a-ball[i].g1);
    ball[i].y = random(ball[i].g2,height-ball[i].g2);

    ball[i].h = (int)random(120);
  }
  
  for (int i=5; i < 10; i++) {
    ball[i] = new Ball();
    //in zweiter Ebene
    ball[i].x = random(a+c+ball[i].g1,a+c+b-ball[i].g1);
    ball[i].y = random(ball[i].g2,height-ball[i].g2);

    ball[i].h = (int)random(120,240);
  }
  
  for (int i=10; i < ball.length; i++) {
    ball[i] = new Ball();
    //in dritter Ebene
    ball[i].x = random(a+c+b+c+ball[i].g1,width-ball[i].g1);
    ball[i].y = random(ball[i].g2,height-ball[i].g2);

    ball[i].h = (int)random(240,360);
  }
}

void draw() {
  background(0);
  fill(255);
  rect(a,0,c,height);
  rect(a+c+b,0,c,height);
  
  for (int i=0; i < 15; i++) {
    ball[i].position[0] = ball[i].x-ball[i].g1;
    ball[i].position[1] = ball[i].x+ball[i].g1;
    ball[i].position[2] = ball[i].y-ball[i].g2;
    ball[i].position[3] = ball[i].y+ball[i].g2;
  }
  
  for (int i=0; i < 5; i++) {
    //Bewegen
    if (ball[i].angle <= 360) {
      ball[i].angle -= 2;
    }
    if (ball[i].angle <= 0) {
      ball[i].angle = 360;
    }
    //im Bogen
    ball[i].vx = cos(radians(ball[i].angle))*ball[i].g1/6;
    ball[i].vy = sin(radians(ball[i].angle))*ball[i].g2/6;
    ball[i].x = ball[i].x + ball[i].vx;
    ball[i].y = ball[i].y + ball[i].vy;
    //Ballgröße zurücksetzen
    if (ball[i].g1 != 60) {
      ball[i].g1 = 60;
    }
    //Rand oben
    if (ball[i].x <= 0+ball[i].g1) {
      ball[i].angle = 0;
    }
    //Rand unten
    if (ball[i].x >= a-ball[i].g1/2) {
      ball[i].angle = 180;
      ball[i].g1 = ball[i].g1-10;
    }
    //links raus
    if (ball[i].y <= 0-ball[i].g2) {
      ball[i].y = height+ball[i].g2/2;
    }
    //rechts raus
    if (ball[i].y >= height+ball[i].g2) {
      ball[i].y = 0-ball[i].g2/2;
    }
    //Bälle stoßen aneinander
    for (int j=i+1; j < 5; j++) {
      if ((ball[i].position[0] >= ball[j].position[0]) && (ball[i].position[1] <= ball[j].position[1]) && 
      (ball[i].position[2] >= ball[j].position[2]) && (ball[i].position[3] <= ball[j].position[3])) {
        ball[i].vx = -ball[i].vx;
        ball[i].vy = -ball[i].vy;
      }
    }
    ball[i].maleBall();
  }
  
  for (int i=5; i < 10; i++) {
    //Bewegen
    if (ball[i].angle <= 360) {
      ball[i].angle -= 3;
    }
    if (ball[i].angle <= 0) {
      ball[i].angle = 360;
    }
    //im Bogen
    ball[i].vx = cos(radians(ball[i].angle))*ball[i].g1/6;
    ball[i].vy = sin(radians(ball[i].angle))*ball[i].g2/6;
    ball[i].x = ball[i].x + ball[i].vx;
    ball[i].y = ball[i].y + ball[i].vy;
    //Ballgröße zurücksetzen
    if (ball[i].g1 != 60) {
      ball[i].g1 = 60;
    }
    //Rand oben
    if (ball[i].x <= a+c+ball[i].g1) {
      ball[i].angle = 0;
    }
    //Rand unten
    if (ball[i].x >= a+c+b-ball[i].g1/2) {
      ball[i].angle = 180;
      ball[i].g1 = ball[i].g1-10;
    }
    //links raus
    if (ball[i].y <= 0-ball[i].g2) {
      ball[i].y = height+ball[i].g2/2;
    }
    //rechts raus
    if (ball[i].y >= height+ball[i].g2) {
      ball[i].y = 0-ball[i].g2/2;
    }
    //Bälle stoßen aneinander
    for (int j=i+1; j < 10; j++) {
      if ((ball[i].position[0] >= ball[j].position[0]) && (ball[i].position[1] <= ball[j].position[1]) && 
      (ball[i].position[2] >= ball[j].position[2]) && (ball[i].position[3] <= ball[j].position[3])) {
        ball[i].vx = -ball[i].vx;
        ball[i].vy = -ball[i].vy;
      }
    }
    ball[i].maleBall();
  }
  
  for (int i=10; i < 15; i++) {
    //Bewegen
    if (ball[i].angle <= 360) {
      ball[i].angle -= 4;
    }
    if (ball[i].angle <= 0) {
      ball[i].angle = 360;
    }
    //im Bogen
    ball[i].vx = cos(radians(ball[i].angle))*ball[i].g1/6;
    ball[i].vy = sin(radians(ball[i].angle))*ball[i].g2/6;
    ball[i].x = ball[i].x + ball[i].vx;
    ball[i].y = ball[i].y + ball[i].vy;
    //Ballgröße zurücksetzen
    if (ball[i].g1 != 60) {
      ball[i].g1 = 60;
    }
    //Rand oben
    if (ball[i].x <= a+c+b+c+ball[i].g1) {
      ball[i].angle = 0;
    }
    //Rand unten
    if (ball[i].x >= width-ball[i].g1/2) {
      ball[i].angle = 180;
      ball[i].g1 = ball[i].g1-10;
    }
    //links raus
    if (ball[i].y <= 0-ball[i].g2) {
      ball[i].y = height+ball[i].g2/2;
    }
    //rechts raus
    if (ball[i].y >= height+ball[i].g2) {
      ball[i].y = 0-ball[i].g2/2;
    }
    //Bälle stoßen aneinander
    for (int j=i+1; j < 15; j++) {
      if ((ball[i].position[0] >= ball[j].position[0]) && (ball[i].position[1] <= ball[j].position[1]) && 
      (ball[i].position[2] >= ball[j].position[2]) && (ball[i].position[3] <= ball[j].position[3])) {
        ball[i].vx = -ball[i].vx;
        ball[i].vy = -ball[i].vy;
      }
    }
    ball[i].maleBall();
  }
}

Objekt Ball

class Ball {
  
  int g1,g2; //Größe
  float x,y; //Position
  
  int h,s,b,a; //Farbe
  
  float vx,vy; //Geschwindigkeit
  float angle; //Gradzahl
  
  float[] position = new float[4]; // die 4 Ecken des Balls;
  
  Ball() {
    g1 = 60;
    g2 = g1;
    
    s = 100;
    b = 100-g1; 
    a = 100;
    
    angle = 90;
  }
  
  void maleBall() {
    for (int i=0; i<g1; i+=5) { //Abrundungen bzw. Schatten
       fill(h,s,b+i,a);
       ellipse(x,y,g1-i,g2-i);
    }
  }
}