加工中的争吵问题

时间:2018-04-26 10:44:49

标签: processing

在这段代码中,如果球碰撞一个用其除数之一编号的球,则球会碰撞并呈红色。

但是当他们在绘图区域的边缘碰撞时,我会得到闪烁的球......我错过了什么吗?

实际碰撞部分(" rencontre"方法)来自官方网站。
(我评论字体部分和saveFrame部分,因为它可以很容易地测试)

    int rang = 1; // l'indexation des nombres commencera à 2
    int effectif = 39;
    int diametre = 50;
    color noir = color(0,0,0);
    color rouge = color(255,0,0);
    color blanc = color(255,255,255);

    class nombre {
      //int diametre = 30;
      float rayon = diametre*1./2;
      float vinit = .5;
      int vmax = 5;
      color c,cpremier,ccompose,ctexte;
      int nb;
      PVector position;
      PVector vitesse;
      String texte;
      nombre[] autres;

      nombre (nombre[] others) { 
        vitesse  = new PVector(random(-vinit,vinit),random(-vinit,vinit));
        position = new PVector(diametre+random(width-diametre),diametre+random(height-diametre));
        //position = new PVector(width/2,height/2);
        nb = rang + 1;
        texte = str(nb);
        autres = others;
        cpremier = noir;
        ccompose = rouge;
        ctexte = blanc;
        c = cpremier;
      }
      void bouge() {
        position.add(vitesse);
        if (position.x > width - rayon || position.x < rayon) {
          vitesse.x = - vitesse.x;
        }
        if (position.y >= height -rayon || position.y < rayon) {
          vitesse.y = - vitesse.y;
        }
      }
      void display () {
        ellipseMode(CENTER);
        stroke(c);
        fill(c);
        ellipse(position.x,position.y,diametre,diametre);
        fill(ctexte);
        text(texte,position.x,position.y);
      }
      void rencontre () {
        // pris sur le site de processing (dans les exemples)
        for (int i = 0; i < effectif; i++) {
          if (i+2 == nb) {
            continue;
          }
          float dx = autres[i].position.x - position.x;
          float dy = autres[i].position.y - position.y;
          //float distance = sqrt(dx*dx + dy*dy);
          float distance = dist( autres[i].position.x,autres[i].position.y,position.x,position.y); 
          float minDist = 2*rayon;
          if (distance < minDist) { 
            if (autres[i].nb % nb == 0) {
              autres[i].c = ccompose;
            } 
            else if (nb % autres[i].nb == 0) {
              c = ccompose;
            }
            float angle = atan2(dy, dx);
            float targetX = position.x + cos(angle) * minDist;
            float targetY = position.y + sin(angle) * minDist;
            float ax = (targetX - autres[i].position.x) * .5;
            float ay = (targetY - autres[i].position.y) * .5;
            vitesse.x -= ax;
            vitesse.y -= ay;
            autres[i].vitesse.x += ax;
            autres[i].vitesse.y += ay;
            autres[i].vitesse.limit(5);
            vitesse.limit(5);
          }
        }
      }
    }

    PFont f;
    nombre[] bulle = new nombre [effectif];

    void setup() {
      size(1000,500);
      background(255);
      for (int i=0;i<effectif;i++) {
          bulle[i] = new nombre(bulle);
          rang ++;
       }
      //f = createFont("LucidaSansRegular.ttf", 24);
      //textFont(f);
      //textAlign(CENTER, CENTER);
    }


    void draw () {
      fill(255);
      rect(0,0,width,height);
       for (int i=0;i<effectif;i++) {
         bulle[i].bouge();
         bulle[i].rencontre();
         bulle[i].display();  
      }
      // saveFrame("./gif/boite-######.png");
    }

0 个答案:

没有答案