Hello, in the code below I try to give a random value to textSize in the sphere class, but it copies

edited May 2016 in Library Questions

[see below for code]

Answers

  • edit post, highlight code, ctrl +o

  • Sorry couldn't finish my message and it was sent! I face an issue with textSize which copy the same word over and over in diffrenet size, aproblem of

  • I can't read your code and I don't understand the problem. If you click the icon in the upper right hand corner of the original post you will be able to edit it. Highlight your code and hit ctrl+o. Leave an empty line before and after the code for it to format properly. Then describe your problem and where it occurs.

  • edited May 2016
                    import peasy.test.*; 
                    import peasy.org.apache.commons.math.*;
                    import peasy.*; 
                    import peasy.org.apache.commons.math.geometry.*; 
                    import processing.opengl.*; 
                    PeasyCam cam; 
                    // spheres number float 
                    noiseValue = 0.001; 
                    Sphere [] sphere; // double array : 1) ID spheres 2) coordinates x,y,z
                    float range; 
                    float rangeCount = 465; 
                    float [][] pos; 
                    float x, y, z, d, a; 
                    PVector camPos;
                    String[] words = { "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop de lux" };
    
                    void setup() {
                      size (1200, 800, OPENGL); 
                      cam = new PeasyCam(this, width/2, height/2, height/2, width);
                      range = rangeCount; 
                      sphere = new Sphere [words.length]; 
                      pos = new float [sphere.length][3]; 
    
                      for (int i =0; i<sphere.length; i++) {
                        sphere[i] = new Sphere( random(width), random(height), random(height/2),words[i]);
                        println(rangeCount); 
                      } 
                    } 
    
                    void draw() { 
                      background (0); 
                      frameRate(25); 
                      lights();
                      for (int i=0; i<sphere.length; i++) {
                        pos[i][0] = x;
                        pos[i][1] = y; 
                        pos[i][2] = z; 
                        int j = 1; 
                      pushStyle();
                        for (j+=i; j<sphere.length; j++) { 
                          d= dist(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]);
                          if (d<=range) {
                            a = constrain((range - d)*3, 0, 255);
                            strokeWeight(1); 
                            stroke(255, a); 
                            line(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]); 
                          } 
                        } 
                       popStyle(); 
                        sphere[i].display();
                        sphere[i].move();
                      }
                    }
    
                    class Sphere { 
                      float p1, p2, p3; 
                      float radius; 
                      String nom; 
                      float textS;
    
                      Sphere (float pt1, float pt2, float pt3, String s) { 
                        p1 = pt1 ;
                        p2 = pt2; 
                        p3 = pt3; 
                        radius = random(10, 30); 
                        nom=s; 
                        textS = random(10, 100); 
                      }
    
                      void move() { 
                        x = (noise(p1)width); 
                        y = (noise(p2)height); 
                        z = (noise(p3)*width); 
                        p1+=noiseValue; 
                        p2+=noiseValue; 
                        p3+=noiseValue; 
                      }
    
                      void display () { 
                        for (int i =0; i< words.length; i++) { 
                          String word = words[i % words.length]; 
                          textSize(textS); textAlign(CENTER); 
                          text(word, pos[i][0], pos[i][1] - 35, pos[i][2]);
                          pushMatrix();
                          translate(x, y, z);
                             noStroke();
                              fill(255, (a + 30) * 3);
                              sphereDetail(30);
                              sphere(radius);
                         popMatrix();
                        }
                      }
    
  • I repost with a properly edited code. Thanks!

  • Thank you Eyeorlife! I have a problem to set a random size to my text (String words), it copies my texte several time on the top of the other...

  • I am still not sure what exactly the problem is, but I can point out that this is problematic:

    for (int i =0; i< words.length; i++) { 
        String[] words = { "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop de lux" }; 
    

    This should result in error because you are trying to access the array words before it has been declared. And furthermore you are declaring it in every iteration of the loop. Move the array to the top level of the class.

  • Thank you very much, it may be why the textSize copy the text in every iteration in a different size! I am so dummy. Thank you for your help.

  • Answer ✓

    Is the problem that the text "peur du noir" get's drawn instead of any of the other phrases?

    Or is it that the size doesn't change?

  • textSize() doesn't store text, it stores a number. textSize() determines the size of the text that is drawn after it has been called.

  • Answer ✓

    This should result in error because you are trying to access the array words before it has been declared.

    that's probably using the global definition of words on line 15. this repetition of that definiton looks wrong - why is it inside the loop? i'd delete line 81

    you also need to add a pushMatrix() / popMatrix() in the sphere.display() loop or the translates will accumulate.

  • Answer ✓

    and the popStyle should be at the same level as pushStyle, not inside the condition.

  • Thank you very much Koogs will do that later on. I'm at work now. I'm really a newbe to P5 and I really appreciate your help after I was looking for a solution for hours!

  •   This way?! 
    
    
        import peasy.test.*; 
        import peasy.org.apache.commons.math.*;
        import peasy.*; 
        import peasy.org.apache.commons.math.geometry.*; 
        import processing.opengl.*; 
        PeasyCam cam; 
        // spheres number float 
        noiseValue = 0.001; 
        Sphere [] sphere; // double array : 1) ID spheres 2) coordinates x,y,z
        float range; 
        float rangeCount = 465; 
        float [][] pos; 
        float x, y, z, d, a; 
        PVector camPos;
        String[] words = { "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop de lux" };
    
        void setup() {
          size (1200, 800, OPENGL); 
          cam = new PeasyCam(this, width/2, height/2, height/2, width);
          range = rangeCount; 
          sphere = new Sphere [words.length]; 
          pos = new float [sphere.length][3]; 
    
          for (int i =0; i<sphere.length; i++) {
            sphere[i] = new Sphere( random(width), random(height), random(height/2),words[i]);
            println(rangeCount); 
          } 
        } 
    
        void draw() { 
          background (0); 
          frameRate(25); 
          lights();
          for (int i=0; i<sphere.length; i++) {
            pos[i][0] = x;
            pos[i][1] = y; 
            pos[i][2] = z; 
            int j = 1; 
          pushStyle();
            for (j+=i; j<sphere.length; j++) { 
              d= dist(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]);
              if (d<=range) {
                a = constrain((range - d)*3, 0, 255);
                strokeWeight(1); 
                stroke(255, a); 
                line(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]); 
              } 
            } 
           popStyle(); 
            sphere[i].display();
            sphere[i].move();
          }
        }
    
        class Sphere { 
          float p1, p2, p3; 
          float radius; 
          String nom; 
          float textS;
    
          Sphere (float pt1, float pt2, float pt3, String s) { 
            p1 = pt1 ;
            p2 = pt2; 
            p3 = pt3; 
            radius = random(10, 30); 
            nom=s; 
            textS = random(10, 100); 
          }
    
          void move() { 
            x = (noise(p1)width); 
            y = (noise(p2)height); 
            z = (noise(p3)*width); 
            p1+=noiseValue; 
            p2+=noiseValue; 
            p3+=noiseValue; 
          }
    
          void display () { 
            for (int i =0; i< words.length; i++) { 
              String word = words[i % words.length]; 
              textSize(textS); textAlign(CENTER); 
              text(word, pos[i][0], pos[i][1] - 35, pos[i][2]);
              pushMatrix();
              translate(x, y, z);
                 noStroke();
                  fill(255, (a + 30) * 3);
                  sphereDetail(30);
                  sphere(radius);
             popMatrix();
            }
          }
    
  • Answer ✓

    well, does it work?

    pushStyle / popStyle should be inside the loop, i think, given that you want all the items to have a separate style.

    lines 74 and 75 have errors in them, missing a *?

    there are still problems - multiple jittery text for each sphere - but i can't debug it at the moment.

  • yes, that was the problem I was pointing out multiple jittery text for each sphere! thanks for your quick answer and will check out if it works in a few hours !

  • The prolem is that each word repeat itself a multiple of times for each sphere

  • Answer ✓
    for (j += i; j < sphere.length; j++) { 
       ...
       sphere[i].display();
    

    but sphere.display...

    for (int i = 0; i < words.length; i++) {
        ...
        text(word, pos[i][0], pos[i][1] - 35, pos[i][2]);
    

    you are printing all the text for each of the spheres.

    what i think you want is for each sphere to have one word associated with it. so assign that word in the constructor and remove the loop in sphere.display().

  • Answer ✓

    in fact, you already pass the word in, as nom. and p[i][0..2] are just x, y, z

    so:

    // replace
    // text(word, pos[i][0], pos[i][1] - 35, pos[i][2]);
    // with
    text(nom, x, y - 35, z);
    
  • Answer ✓

    what's the dist() doing? because it looks like everything is always connected to everything else. you might need to tune the values...

  • yes I need to put the push/pop Style somewhere else that only the lines fade in/out according to the distance between spheres. By the way, what do you mean by assign the word in the constructor?!

  • Thank you very much and sorry for my very basic skill in Processing ;)

  • Answer ✓
    Sphere (float pt1, float pt2, float pt3, String s) { 
    

    this is the constructor for Sphere. that last argument is copied to 'nom'

    when you create the Spheres you use

    sphere[i] = new Sphere( random(width), random(height), random(height/2),words[i]);
    

    which gives each sphere a random position and a word from the list. one word is all the Sphere needs. this is good. what you had before was unnecessary.

  • Hi here is my code, still doesn't work I messed it up I guess thank you very much for your help!

    import peasy.test.*;
    import peasy.org.apache.commons.math.*;
    import peasy.*;
    import peasy.org.apache.commons.math.geometry.*;
    import processing.opengl.*;
    PeasyCam cam;
    // nbre de Sphères
    int n;
    float noiseValue = 0.001;
    Sphere [] sphere;
    // double tableau : 1) ID sphères 2) coordonnées en x,y,z
    float range;
    float rangeCount = 465;
    float [][] pos;
    float x, y, z, d, a;
    PVector camPos;   
    float angleXY, d1;  
    PFont font;
    String[] words = {
      "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop delux"
    }; 
    
    void setup() {
    
      size (1200, 800, OPENGL);
      cam = new PeasyCam(this, width/2, height/2, height/2, width);
      range = rangeCount;
      sphere = new Sphere [words.length];
      pos = new float [sphere.length][3];
      for (int i =0; i<sphere.length; i++) {
        sphere[i] = new Sphere( random(width), random(height), random(height/2), words[i]);
        println(rangeCount);
      }
    }
    void draw() {
      background (0); 
      frameRate(25);
      lights();
    
      for (int i=0; i<sphere.length; i++) {
        pos[i][0] = x;
        pos[i][1] = y;
        pos[i][2] = z;
    
        int j = 1;
        for (j+=i; j<sphere.length; j++) {
          d= dist(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]);
          pushStyle();
          if (d<=range) {  
            a = constrain((range - d)*3, 0, 255);
            strokeWeight(1);
            stroke(255, a);
            line(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]);
          }
          popStyle();
        }
    
        sphere[i].display();  
        sphere[i].move();
      }
    }
    
    class Sphere {
      float p1, p2, p3;
      float radius;
      String nom;
      float textS;
    
      Sphere (float pt1, float pt2, float pt3, String s) {
        p1 = pt1 ;
        p2 = pt2;
        p3 = pt3;
        radius = random(10, 30);
        textS = random(10, 100); 
        String[] nom = {
      "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop delux"
    };
    
      }
      void move() {
        x = (noise(p1)*width);
        y = (noise(p2)*height);
        z = (noise(p3)*width);
        p1+=noiseValue;
        p2+=noiseValue;
        p3+=noiseValue;
      }
    
      void display () {
    String word = words  [i %words.length]; // Here is the problem?!
        textSize(textS);
        textAlign(CENTER);
        text(nom, x, y-35, z);
        pushMatrix();
        translate(x, y, z);
        noStroke();
        fill(255, (a+30)*3);
        sphereDetail(30);
        sphere(radius);
        popMatrix();
      }
    }
    
  • Answer ✓

    How about this? Is this what you want?

    import peasy.test.*; 
    import peasy.org.apache.commons.math.*;
    import peasy.*; 
    import peasy.org.apache.commons.math.geometry.*; 
    import processing.opengl.*; 
    PeasyCam cam; 
    // spheres number float 
    float noiseValue = 0.001; 
    Sphere [] sphere; // double array : 1) ID spheres 2) coordinates x,y,z
    float range; 
    float rangeCount = 465; 
    float [][] pos; 
    float x, y, z, d, a; 
    PVector camPos;
    String[] words = { "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop de lux" };
    
    void setup() {
      size (1000, 600, OPENGL); 
      cam = new PeasyCam(this, width/2, height/2, height/2, width);
      range = rangeCount; 
      sphere = new Sphere [words.length]; 
      pos = new float [sphere.length][3]; 
    
      for (int i =0; i<sphere.length; i++) {
        sphere[i] = new Sphere( random(width), random(height), random(height/2), words[i]);
        println(rangeCount);
      }
    } 
    
    void draw() { 
      background (0); 
      frameRate(25); 
      lights();
      for (int i=0; i<sphere.length; i++) {
        pos[i][0] = x;
        pos[i][1] = y; 
        pos[i][2] = z; 
        int j = 1; 
        pushStyle();
        for (j+=i; j<sphere.length; j++) { 
          d= dist(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]);
          if (d<=range) {
            a = constrain((range - d)*3, 0, 255);
            strokeWeight(1); 
            stroke(255, a); 
            line(pos[i][0], pos[i][1], pos[i][2], pos[j][0], pos[j][1], pos[j][2]);
          }
        }
        popStyle(); 
        sphere[i].display();
        sphere[i].move();
      }
    }
    
    class Sphere { 
      float p1, p2, p3; 
      float radius; 
      String nom; 
      float textS;
      int nr;
    
      Sphere (float pt1, float pt2, float pt3, String s) { 
        p1 = pt1 ;
        p2 = pt2; 
        p3 = pt3; 
        radius = random(10, 30); 
        nom=s; 
        textS = random(10, 100);
      }
    
      void move() { 
        x = (noise(p1)*width); 
        y = (noise(p2)*height); 
        z = (noise(p3)*width); 
        p1+=noiseValue; 
        p2+=noiseValue; 
        p3+=noiseValue;
      }
    
      void display () { 
          textSize(textS); 
          textAlign(CENTER); 
          text(nom, x, y - 35,z);
          pushMatrix();
          translate(x, y, z);
          noStroke();
          fill(255, (a + 30) * 3);
          sphereDetail(30);
          sphere(radius);
          popMatrix();
      }
    }
    
  • Thank you so much Eeyorelife! I was so stressed I couldn't find a way out. Thank you koogs too, you are both my saviors. It's my 1rst time on the Forum and I am so happy. Thank you for your generosity and the time you spent on my little problem. I hope in few years i'll be able to help someone else too just like you! cu

  • Answer ✓

    eeyore coming in there at the 11th hour and getting the 'answered' tick.

    I might have known...

  • Hi Koogs, sorry I didn't pay attention and certainly ticked the'answered' by 'mistake' when eeyore answered me. As I told you then its'my 1rst time on the forum and I may make some mistake. You definitely helped me a lot and I thank you once again for your great help. I ticked the 'answered' everywhere now! Sorry I'll pay more attention to this next time. Best wishes

  • edited July 2016

    Hello, I have an issue with selecting object (clickedMouse) and recursivity. I did a kind of solar system, a sun at the center, planets around and satellites around the planets. I manage to slect my planets and zoom towards them, but not to select the satellites!? If someone would take a look at my sketch, I'd be glad ! Thanks in advance.

        import peasy.test.*;
    import peasy.org.apache.commons.math.*;
    import peasy.*;
    import peasy.org.apache.commons.math.geometry.*;
    import processing.opengl.*;
    PeasyCam camera;
    // nbre de Sphères
    Sphere sun;
    Sphere currentSphereCenter;
    boolean pressed = false;
    boolean locked = false;
    float radiusSphere = 600;
    float radius;
    float range;
    float rangeCount = 600;
    PVector camPos;  
    float theta;
    float phi;
    
    
    
    String[] words = {
      "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop de lux"
    };
    String[][] satWords = {
      {
        "peur du noir", "couscous"
      }
      , {
        "suée nocturne", "bbbb", "ghhtt"
      }
      , {
        "troisième oeil", "bhbhiu", "jkjkjlkj", "ghhjkkklk"
      }
      , {
        "sombre matière"
      }
      , {
        "clair de nuit"
      }
      , {
        "les lucioles"
      }
      , {
        "trop de lux"
      }
    };
    void setup() {
    
      size (1200, 800, P3D);
      hint(DISABLE_DEPTH_TEST);
    
      camera = new PeasyCam(this, width/2, height/2, height/2, width);
      range = rangeCount;
    
      PFont Moonfont = createFont ("RNIB MOON", (52));
      sun = new Sphere(0, 90, TWO_PI, PI/2, "Clair Obscur", Moonfont, null);
      PFont [] fonts = new PFont [8];
      for (int j=0; j<8; j++) {
        println(j);
        fonts [j] = createFont("Helvetica 45 Light", (32+4*j));
      }
      Sphere []spheres = new Sphere [words.length];
      for (int i =0; i<spheres.length; i++) {
        PFont f = fonts[floor(random(fonts.length))] ;
        spheres[i] = new Sphere(radiusSphere, random(25, 50), random(0, TWO_PI), random(0, PI), words[i], f, sun);
        // satWords = liste de liste des noms pr chaque satellite et [i] correspond à la planète
    
        Sphere [] sats = new Sphere [satWords[i].length];
        for (int k =0; k<satWords[i].length; k++) {
          PFont g = fonts[floor(random(fonts.length))] ;
          sats [k] = new Sphere(radiusSphere/4, random(10, 20), random(0, TWO_PI), random(0, PI), satWords[i][k], g, spheres[i]);
        }
        spheres [i].addSatellites(sats);
        //String[] fontList = PFont.list();
        // printArray(fontList);
      }
      sun.addSatellites(spheres);
      println(rangeCount);
     //selectSphere(sun); 
      frameRate(25);
    }
    void draw() {
      background (0);
    
    /*float[] rotations = camera.getRotations();
      rotateX(rotations[0]);
        rotateY(rotations[1]);
        rotateZ(rotations[2]);*/
        //directionalLight(200, 255, 255, 0, 0, 0);
      sun.display();
      pushMatrix();
      translate (0, 0, 0);
      noStroke();
      stroke (75, 75, 75, 15);
      getWeird(150, 0.1, radiusSphere);
      popMatrix();
    }
    
    
    void getWeird(float sStep, float tStep, float radius) {
      float s = 0;
      float t = 0;
      float lastx = 0;
      float lasty = 0;
      float lastz = 0;
      while (t < 180) {
        s += sStep;
        t += tStep;
        float radianS = radians(s);
        float radianT = radians(t);
        float thisx = 0 + (radius * cos(radianS) * sin(radianT)) + noise(random(10)*25);
        float thisy = 0 + (radius * sin(radianS) * sin(radianT)) + noise(random(10)*25);
        float thisz = 0 + (radius * cos(radianT)) + noise(random(10))*25;
        if (lastx != 0) {
          line(thisx, thisy, thisz, lastx, lasty, lastz);
        }
        lastx = thisx;
        lasty = thisy;
        lastz = thisz;
      }
    }
    
    void mouseClicked () {
      //println("mouseClicked");
      //for (int i = 0; i < sun.spheres.length; i++) {
      Sphere clickedSphere = sun.spheres[0];
    
    
    
      float lastd = 10000;
      for (Sphere s : sun.spheres) {
        float x1 = screenX (s.x, s.y, s.z);
        float y1 = screenY (s.x, s.y, s.z);
        float d = dist(x1, y1, mouseX, mouseY);
    
        //println ("x1: "+x1+", y1: "+y1+", d: "+ d);
        if ( d < lastd) {
          lastd = d;
          clickedSphere = s;
        }
      }
      if (lastd <= clickedSphere.radius*1.2) {
          selectSphere (clickedSphere);
    
        }else {
          backLevel();
        }
    
    }
    
    void backLevel() {
      println("backLevel");
      if (currentSphereCenter != sun){
        selectSphere (currentSphereCenter.sphereCenter);
      }
    }
    void selectSphere (Sphere s) {
    int zoom = 5;
      println ("planete la plus proche: "+ s.nom);
       currentSphereCenter = s;
    if (currentSphereCenter != sun){
    camera.lookAt(s.x, s.y, s.z);
    camera.getDistance();
    float d = getDistance;
    camera.setDistance ((d/d)*2);
    
       if(mouseEvent.getClickCount()==2){
    
    }
    }
    
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    
    class Sphere {
      float theta;
      float phi;
      float vTheta;
      float vPhi;
      float  r, x, y, z;
      float radius;
      float radiusSphere;
      String nom;
      PFont font;
      color c;
      Sphere[] spheres;
    Sphere sphereCenter;
    
      Sphere (float radiusSphere, float rad, float theta, float phi, String s, PFont f, Sphere sC) {
        this.theta = theta ;
        this.phi = phi;
        this.r = radiusSphere;
        vTheta =0;
        vPhi =0;
        x = r * sin(phi) * cos(theta);
        y = r * sin(phi) * sin(theta);
        z = r * cos(phi);
        radius = rad;
        c = color(random(200, 255));
        nom = s;
        font = f;
        sphereCenter = sC;
      }
    
      String printPos () {
        return "x:"+x+ " y:"+y+" z:"+z;
      }
    
      void move() {
    
        vTheta = noise (-0.0001, 0.0001);
        theta+=vTheta/100;
        if (theta < 0||theta > TWO_PI) {
          vTheta*= -1;
        }
        vPhi = noise (-0.0001, 0.0001);
        phi+=vPhi/100;
        if (phi < 0 ||phi > PI) {
          vPhi*= -1;
        }
        vPhi*=2;
        vTheta*=2;
        x = r * sin(phi) * cos(theta);
        y = r * sin(phi) * sin(theta) ;
        z = r * cos(phi);
        if (spheres != null) {
          for (int i = 0; i < spheres.length; i++) {
            spheres[i].move();
          }
        }
      }
      void addSatellites (Sphere [] sats) {
        spheres= sats;
      }
    
      void display () {
        //println ("spheres["+i+"]:"+spheres[i].printPos());
        textFont(font);
        textAlign(CENTER);
        pushMatrix();
        fill(255);
        noStroke();
        float[] rotations = camera.getRotations();
        translate(x, y - (radius+10), z);
        rotateX(rotations[0]);
        rotateY(rotations[1]);
        rotateZ(rotations[2]);
        scale(0.5);
        text(nom, 0, 0, 0);
        popMatrix();
        pushMatrix();
        translate(x, y, z);
        fill(c);
        stroke(c, 100);
        getWeird(7, 0.1, radius);
    
        if (spheres != null) {
    
          // lignes qui pointent des spheres vers le sun
          for (int i = 0; i < spheres.length; i++) {
            float x1 = spheres[i].x;
            float y1 = spheres[i].y;
            float z1 = spheres[i].z;
            strokeWeight(1);
            stroke(random (180,255)); 
            line (0, 0, 0, x1, y1, z1);
            //line (radius * sin(phi) * cos(theta), radius * sin(phi) * sin(theta), radius * cos(phi), x1, y1, z1);
          }
          // dessin des lignes entre sphères
          for (int i = 0; i < spheres.length; i++) {
            float x1 = spheres[i].x;
            float y1 = spheres[i].y;
            float z1 = spheres[i].z;
    
            int j =1;
            for (j+=i; j<spheres.length; j++) {
              float x2 = spheres[j].x;
              float y2 = spheres[j].y;
              float z2 = spheres[j].z;     
              float d= dist(x1, y1, z1, x2, y2, z2);
              if (d<=range) {
                float a = constrain((range - d)*3, 0, 255);
                strokeWeight(1);
                stroke(255, a); 
    
                line(x1, y1, z1, x2, y2, z2);
              }
            }
            spheres[i].display(); 
            spheres[i].move();
            //println ("spheres["+i+"]:"+spheres[i].printPos());
          }
    
    
         // println("sphere "+ nom +" x: "+x+", y: "+y+", z: "+z+".");
        }
        popMatrix();
      }
    
      void getWeird(float sStep, float tStep, float radius) {
        float s = 0;
        float t = 0;
        float lastx = 0;
        float lasty = 0;
        float lastz = 0;
        while (t < 180) {
          s += sStep;
          t += tStep;
          float radianS = radians(s);
          float radianT = radians(t);
          float thisx = 0 + (radius * cos(radianS) * sin(radianT)) + noise(random(10)*25);
          float thisy = 0 + (radius * sin(radianS) * sin(radianT)) + noise(random(10)*25);
          float thisz = 0 + (radius * cos(radianT)) + noise(random(10))*25;
          if (lastx != 0) {
            line(thisx, thisy, thisz, lastx, lasty, lastz);
          }
          lastx = thisx;
          lasty = thisy;
          lastz = thisz;
        }
      }
    }
    
  • code doesn't compile...

    float d = getDistance;
    

    not a valid statement.

  • Thanks for your reply koogs, I'll fix the code later on and will send it back, I've sent the wrong version.

  • Please find my code below. I don't manage to select the satellites, just the spheres (in the mouseClicked method and the selectSphere method). A problem of recursivity I guess. Dont' find a way out ! Thanks in advance for your help.

    import peasy.test.*;
    import peasy.org.apache.commons.math.*;
    import peasy.*;
    import peasy.org.apache.commons.math.geometry.*;
    import processing.opengl.*;
    PeasyCam camera;
    // nbre de Sphères
    Sphere [] sats;
    Sphere [] spheres;
    Sphere sun;
    Sphere currentSphereCenter;
    boolean pressed = false;
    boolean locked = false;
    float radiusSphere = 600;
    float radius;
    float range;
    float rangeCount = 600;
    PVector camPos;   
    float theta;
    float phi;
    boolean resetOnDoubleClick =true;
    
    
    String[] words = { 
      "peur du noir", "suée nocturne", "troisième oeil", "sombre matière", "clair de nuit", "les lucioles", "trop de lux"
    }; 
    String[][] satWords = { 
      {
        "peur du noir", "couscous"
      }
      , {
        "suée nocturne", "bbbb", "ghhtt"
      }
      , {
        "troisième oeil", "bhbhiu", "jkjkjlkj", "ghhjkkklk"
      }
      , {
        "sombre matière"
      }
      , {
        "clair de nuit"
      }
      , {
        "les lucioles"
      }
      , {
        "trop de lux"
      }
    }; 
    void setup() {
    
      size (1200, 800, P3D);
      hint(DISABLE_DEPTH_TEST);
    
      camera = new PeasyCam(this, width/2, height/2, height/2, width);
      range = rangeCount;
    
      PFont Moonfont = createFont ("RNIB MOON", (52));
      sun = new Sphere(0, 90, TWO_PI, PI/2, "Clair Obscur", Moonfont, null);
      PFont [] fonts = new PFont [8];
      for (int j=0; j<8; j++) {
        println(j);
        fonts [j] = createFont("Helvetica 45 Light", (32+4*j));
      }
      Sphere []spheres = new Sphere [words.length];
      for (int i =0; i<spheres.length; i++) {
        PFont f = fonts[floor(random(fonts.length))] ;
        spheres[i] = new Sphere(radiusSphere, random(25, 50), random(0, TWO_PI), random(0, PI), words[i], f, sun);
        // satWords = liste de liste des noms pr chaque satellite et [i] correspond à la planète
    
        Sphere [] sats = new Sphere [satWords[i].length];
        for (int k =0; k<satWords[i].length; k++) {
          PFont g = fonts[floor(random(fonts.length))] ;
          sats [k] = new Sphere(radiusSphere/4, random(10, 20), random(0, TWO_PI), random(0, PI), satWords[i][k], g, spheres[i]);
        }
        spheres [i].addSatellites(sats);
        //String[] fontList = PFont.list();
        // printArray(fontList);
      }
      sun.addSatellites(spheres);
      println(rangeCount);
      //selectSphere(sun);  
      frameRate(25);
    }
    void draw() {
      background (0); 
      sun.display();
      pushMatrix();
      translate (0, 0, 0);
      sphereWave();
      /*noStroke();
       stroke (75, 75, 75, 15);
       getWeird(150, 0.1, radiusSphere);*/
      popMatrix();
    }
    
    void sphereWave() {
      float radious = radiusSphere;
      float noiseScale = 0.01;
      float timeScale = 0.03;
      stroke (75, 75, 75, 100);
      noFill();
    
      //rotateZ(map(mouseX, 0, width, -PI * 2.0 / 3, PI * 2.0 / 3));
      //rotateX(map(mouseY, 0, height, -PI * 2.0 / 3, PI * 2.0 / 3));
      for (float y = -radious/3 * 2.0; y <= radious * 2.0 / 3; y += 12) {
        float r = radious * cos(asin(abs(y / radious)));
        beginShape();
        for (float radian = 0; radian < TWO_PI; radian += PI / 128) {
          float x = r * cos(radian);
          float z = r * sin(radian);
          float yy = y + map(noise(x * noiseScale, frameCount * timeScale, z * noiseScale), 0, 1, -radious / 3, radious / 3);
          if (yy > radious) {
            yy = radious;
          }
          float rr = radious * cos(asin(abs(yy / radious)));
          float xx = rr * cos(radian);
          float zz = rr * sin(radian);
          vertex(xx, yy, zz);
        }
        endShape(CLOSE);
      }
    }
    
    void mouseClicked () {
    
      Sphere clickedSphere = sun.spheres[0];
      float lastd = 10000;
      for (Sphere s : sun.spheres) {
        float x1 = screenX (s.x, s.y, s.z);
        float y1 = screenY (s.x, s.y, s.z);
        float d = dist(x1, y1, mouseX, mouseY);
    
        if ( d < lastd) {
          lastd = d;
          clickedSphere = s;
          camera.lookAt(s.x, s.y, s.z);
          camera.setDistance (d+200);
        }
      }
      if (lastd <= clickedSphere.radius*1.2) {
        selectSphere (clickedSphere);
      } else {
        backLevel();
        camera.lookAt(sun.x, sun.y, sun.z);
        camera.setDistance(1400, 400);
      }
    }
    void backLevel() {
      println("backLevel");
      if (currentSphereCenter != sun) {
        selectSphere (currentSphereCenter.sphereCenter);
      }
    }
    void selectSphere (Sphere s) {
      println ("planete la plus proche: "+ s.nom);
      currentSphereCenter = s;
    }
    
    
    
    class Sphere {
      float theta;
      float phi;
      float vTheta;
      float vPhi;
      float  r, x, y, z;
      float radius;
      float radiusSphere;
      String nom;
      PFont font;
      color c;
      Sphere[] spheres;
    Sphere sphereCenter;
    
      Sphere (float radiusSphere, float rad, float theta, float phi, String s, PFont f, Sphere sC) {
        this.theta = theta ;
        this.phi = phi;
        this.r = radiusSphere;
        vTheta =0;
        vPhi =0;
        x = r * sin(phi) * cos(theta);
        y = r * sin(phi) * sin(theta);
        z = r * cos(phi);
        radius = rad;
        c = color(random(200, 255));
        nom = s;
        font = f;
        sphereCenter = sC;
      }
    
      String printPos () {
        return "x:"+x+ " y:"+y+" z:"+z;
      }
    
      void move() {
    
        vTheta = noise (-0.0001, 0.0001);
        theta+=vTheta/100;
        if (theta < 0||theta > TWO_PI) {
          vTheta*= -1;
        }
        vPhi = noise (-0.0001, 0.0001);
        phi+=vPhi/100;
        if (phi < 0 ||phi > PI) {
          vPhi*= -1;
        }
        vPhi*=2;
        vTheta*=2;
        x = r * sin(phi) * cos(theta);
        y = r * sin(phi) * sin(theta) ;
        z = r * cos(phi);
        if (spheres != null) {
          for (int i = 0; i < spheres.length; i++) {
            spheres[i].move();
          }
        }
      }
      void addSatellites (Sphere [] sats) {
        spheres= sats;
      }
    
      void display () {
        //println ("spheres["+i+"]:"+spheres[i].printPos());
        textFont(font);
        textAlign(CENTER);
        pushMatrix();
        fill(255);
        noStroke();
        float[] rotations = camera.getRotations();
        translate(x, y - (radius+10), z);
        rotateX(rotations[0]);
        rotateY(rotations[1]);
        rotateZ(rotations[2]);
        scale(0.5);
        text(nom, 0, 0, 0);
        popMatrix();
        pushMatrix();
        translate(x, y, z);
        fill(c);
        stroke(c, 100);
        getWeird(7, 0.09, radius);
    
        if (spheres != null) {
    
          // lignes qui pointent des spheres vers le sun
          for (int i = 0; i < spheres.length; i++) {
            float x1 = spheres[i].x;
            float y1 = spheres[i].y;
            float z1 = spheres[i].z;
            strokeWeight(1);
            stroke(random (180,225));  
            line (0, 0, 0, x1, y1, z1);
            //line (radius * sin(phi) * cos(theta), radius * sin(phi) * sin(theta), radius * cos(phi), x1, y1, z1);
          }
          // dessin des lignes entre sphères
          for (int i = 0; i < spheres.length; i++) {
            float x1 = spheres[i].x;
            float y1 = spheres[i].y;
            float z1 = spheres[i].z;
    
            int j =1;
            for (j+=i; j<spheres.length; j++) {
              float x2 = spheres[j].x;
              float y2 = spheres[j].y;
              float z2 = spheres[j].z;      
              float d= dist(x1, y1, z1, x2, y2, z2);
              if (d<=range) { 
                float a = constrain((range - d)*4, 0, 225);
                strokeWeight(1);
                stroke(100, a);  
    
                line(x1, y1, z1, x2, y2, z2);
              }
            }
            spheres[i].display();  
            spheres[i].move();
            //println ("spheres["+i+"]:"+spheres[i].printPos());
          }
    
    
         // println("sphere "+ nom +" x: "+x+", y: "+y+", z: "+z+".");
        }
        popMatrix();
      }
    
      void getWeird(float sStep, float tStep, float radius) {
        float s = 0;
        float t = 0;
        float lastx = 0;
        float lasty = 0;
        float lastz = 0;
        while (t < 180) {
          s += sStep;
          t += tStep;
          float radianS = radians(s);
          float radianT = radians(t);
          float thisx = 0 + (radius * cos(radianS) * sin(radianT)) + noise(random(10)*25);
          float thisy = 0 + (radius * sin(radianS) * sin(radianT)) + noise(random(10)*25);
          float thisz = 0 + (radius * cos(radianT)) + noise(random(10))*25;
          if (lastx != 0) {
            line(thisx, thisy, thisz, lastx, lasty, lastz);
          }
          lastx = thisx;
          lasty = thisy;
          lastz = thisz;
        }
      }
    }
    
  • my code does compile now ;) Found out a way to zoom smoothly with peasy but still haven't found a way to select my satellites! Help!

  • line 129 only loops over the sun.spheres. you need to also loop over the satellites.

    if you make the sun one colour, the spheres another and the satellites another it'll be easier to describe your problem - using 'spheres' to describe things is not great because EVERYTHING is spherical.

  • edited July 2016

    Dear Koogs thank you very much for your answer, yes to call them 'spheres' is really dummy!!

    I tried to loop over the satellites with this kind of loop but it didnt compile:

    Sphere clickedSphere = spheres.sats[0];
    for (Sphere s : spheres.sats)
    

    I'll change the name and give each group a colour, it will certainly help!Thanks

  • Answer ✓

    this works. put it after line 140

    the thing is that the satellites are orbiting the spheres, hence the s.x + sat.x stuff.

      // check the satellites as well
      for (Sphere s : sun.spheres) {
        for (Sphere sat : s.spheres) {
          float x1 = screenX (s.x + sat.x, s.y + sat.y, s.z + sat.z);
          float y1 = screenY (s.x + sat.x, s.y + sat.y, s.z + sat.z);
          float d = dist(x1, y1, mouseX, mouseY);
    
          if (d < lastd) {
            lastd = d;
            clickedSphere = sat;
            camera.lookAt(s.x + sat.x, s.y + sat.y, s.z + sat.z);
            camera.setDistance(d + 200);
          }
        }
      }
    
  • Thank you a million times koogs, that makes so much sense! And this time I didn't forget to check the 'answer' buton ;)) Thank you for taking some time to solve my problem, you helped me a lot :)

  • Here is the code with the loop line 30 where the problem occures :

    import java.util.*;
    
    PGraphics pg;
    int SizeText=50;
    int particlesPerLetter = 50;
    color textCol= color(245);
    int Border = 30;
    int REST = 0;
    int MOUSE = 1;
    int OVER  = 2;
    float y;
    int mode = REST;
    String [] words = { 
      "Ici\non m'a dit d'ailleurs\nailleurs\nque j'étais d'ici", 
      "je suis d’ici d’ailleurs\nj’y suis allé, voir si j’y étais\nMais je ne m’y trouvais pas", 
      "on m’a dit : ici, là-bas, c’est idem\ntoi du pareil au même\nalors à quoi bon me chercher là? ", 
      "je me suis là et suis las\nd’être ici sans y être même\nmais si je me trouve peu m'importe\nsi je te trouve devant ma porte", 
      "toi ni d'ici ni d'ailleurs\nchez toi partout pourtant\ntoi qui arrimes mon cœur\nd'ailleurs par tous les temps"
    };
    Particle [] particles;
    Write [] phrases;
    Write []phrases1;
    int nbParticle;
    
    void setup () {
      size(1080, 1080, P2D);
    
      Write[]phrases = new Write [words.length];
      for (int i=0; i< phrases.length; i++) { 
        phrases[i] = new Write (words[i]);
      }
    }
    
    
    void draw() {
      fill (0); 
      stroke(0, 200);
      rect(0, 0, width, height);
      stroke(255);
    
    
      for (int i=0; i< nbParticle; i++) {
        particles[i].update(mode);
        particles[i].display();
        particles[i].edges();
      }
    }
    void mousePressed()
    {
      if (mouseButton == LEFT)
      {
        mode = MOUSE;
      } else if (mouseButton == RIGHT) {
        mode = REST;
      }
    }
    void mouseReleased()
    {
      mode= OVER;
    }
    class Particle {
    
      float friction1 = 0.97;
      float fact1 =0.05;
      float slow1 =0.9;
      float friction2 = 0.97;
      float fact2 = 5;
      float slow2 = 0.92;
      float friction3 = 0.8;
      float fact3 = 0.002;
      float mass = random(2, 6);
      color col = color(random(180, 220));
      float rad = random(0.5, 1.5);
      PVector location;
      PVector acceleration;
      PVector origin;
      Boolean in = false;
      float angle;
    
      Particle () { 
    
        acceleration = new PVector(0, 0);
        while (!in)
        {
          int x = (int)random(width);
          int y = (int)random(height);
          if (pg.pixels[y * width + x] == textCol)
          {
            location = new PVector(x, y);
            origin = location.get();
            in = true;
          }
        }
      }
    
      void update(int p_mode) {
    
        PVector but;
        float m;
        switch(p_mode) {
    
        case 0:
          but = origin.get();
          but.sub(location);
          but.limit(10);
          but.mult(friction1 * fact1 * mass);
          acceleration.add(but);
          acceleration.mult(slow1);
          break;
          case 1: 
          but = new PVector(mouseX, mouseY);
          but.sub(location);
          m = but.mag()< 60 ? 500 :but.mag();
          but.normalize();
          but.mult(friction2 * fact2 * mass/ (sqrt(m)));
          acceleration.add(but);
          acceleration.mult(slow2);
          break;
        case 2:
          but = new PVector(mouseX+ noise(cos(angle)), mouseY + noise(sin(angle)));
          but.sub(location);
          but.limit(10);
          m = but.mag()< 120 ? 800 :but.mag();
          but.mult(friction3 * fact1 * mass/ (sqrt(m)));
         acceleration.add(but);
          acceleration.mult(0.98);
          break;
        }
      }
      void display () {
      }
      void edges () {
    
        location.add(acceleration);
        if (location.x < Border) {
          location.x = Border;
          acceleration.x*=-1;
        } else if (location.x > width-Border) {
          location.x = width-Border;
          acceleration.x*=-1;
        }
        if (location.y < Border) {
          location.y = Border;
          acceleration.y*=-1;
        } else if (location.y > height - Border) {
          location.y = height - Border;
          acceleration.y *=-1;
        }
        fill(col, 0);
        ellipse (location.x, location.y, rad, rad);
      }
    }
    class Write {
    
      float PgSize;
      String words;
    
      Write(String _words) {
    
        pushMatrix();
        nbParticle = particlesPerLetter * _words.length();
        particles = new Particle[nbParticle];
        pg = createGraphics(width, height);
        pg.beginDraw();
        pg.textSize(50);
        pg.fill(textCol);
        pg.textAlign(CENTER);
        pg.text(_words, Border, Border+100, width-Border, height-Border);
        pg.endDraw();
    
        pg.loadPixels();
        for (int i =0; i <nbParticle; i++) {
          particles[i] = new Particle();
        }
        pg.updatePixels();
        popMatrix();
      }
    }
    
  • Answer ✓

    Here is the code with the loop line 30 where the problem occures

    what problem?

    is this the same problem as 3 months ago or a new problem?

    this doesn't even look like the same code as 3 months ago.

  • No, sorry Kogs I've pasted this new code in the complete wrong place I apologize... I'll post it in the general a/q dedicated place

  • Dear koogs, I've just posted a new message regarding minim library, if you have some time I'd be very glad if you can help me ;) thank you very much

  • Dear Koogs,

    I work once again on my program with planets and satellites, I try to check out the distance between both and between satellites themselves in order they won't 'coagulate' together ... I tried to calculate them with the method dist() and then added the result to my variable d, but it doesn't seem to work how should I proceed please? Thanks a lot in advance

    class Sphere {
      float theta;
      float phi;
      float vTheta;
      float vPhi;
      float  r, x, y, z;
      float radius;
      //float radiusSphere;
      String name;
      PFont font;
      int level;
      color strokeColor;
      Sphere[] planets;
      Sphere sphereCenter;
    
      boolean isSelected;
      boolean wasVisited;
    
      float phiStep;
      float thetaStep;
      PVector nameSize;
      float noiseScaleZ;
    
      Sphere (float radiusSphere, float rad, float theta, float phi, String s, PFont f, Sphere sC, int l, color StrokeCol) {
        this.theta = theta ;
        this.phi = phi;
        this.r = radiusSphere;
        vTheta =0;
        vPhi =0;
        x = r * sin(phi) * cos(theta);
        y = r * sin(phi) * sin(theta);
        z = r * cos(phi);
        radius = rad;
        name = s;
        font = f;
        sphereCenter = sC;
        level = l;
        strokeColor = StrokeCol;
        phiStep = 8;
        thetaStep = 0.05;
        noiseScaleZ = 1;
    
    
        isSelected = false;
        wasVisited = false;
    
        // calcule de la taille du texte
        textFont(f);
        float tw = textWidth(s); 
        float th = f.getSize();
        nameSize = new PVector(tw, th);
      }
    
      String printPos () {
        return "x:"+x+ " y:"+y+" z:"+z;
      }
    
      void move() {
        vTheta = noise (-0.0001, 0.0001);
        theta+=vTheta/150;
        if (theta < 0||theta > TWO_PI) {
          vTheta*= -1;
        }
        vPhi = noise (-0.0001, 0.0001);
        phi+=vPhi/150;
        if (phi < 0 ||phi > PI) {
          vPhi*= -1;
        }
        vPhi*=2;
        vTheta*=2;
        x = r * sin(phi) * cos(theta);
        y = r * sin(phi) * sin(theta) ;
        z = r * cos(phi);
      }
    
      void addSatellites (Sphere [] sats) {
        planets= sats;
      }
    
      /*void setSelected(int i) {
       this.isSelected = true;
       //println("Got it " + i);
       }*/
    
    
    
      void display () {
        //println ("planets["+i+"]:"+planets[i].printPos());
    
        pushMatrix();
        translate(x, y, z);
        if (level >0) {
          stroke(strokeColor);
          drawNoisySphere();
        }
        // println("level"+level);
        if (planets != null) {
          if (level > 0 ) {
            //lignes qui pointent des planets vers le sun
            for (int k = 0; k < planets.length; k++) {
              float x3 = planets[k].x;
              float y3 = planets[k].y;
              float z3 = planets[k].z;
    
              float d= dist(x3, y3, z3, 0, 0, 0);
              if (d<=range) { 
                d+=random(130,200);
                float a = constrain((range - d)*4, 0, 225);
                stroke(150, a); 
                strokeWeight(1);   
                line (0, 0, 0, x3, y3, z3);
    
              }
            }
          }
    
            // dessin des lignes entre sphères
    
            for (int i = 0; i < planets.length; i++) {
              float x1 = planets[i].x;
              float y1 = planets[i].y;
              float z1 = planets[i].z;
    
              int j =1;
              for (j+=i; j<planets.length; j++) {
                float x2 = planets[j].x;
                float y2 = planets[j].y;
                float z2 = planets[j].z;      
                float d= dist(x1, y1, z1, x2, y2, z2);
    
                if (d<=range) { 
                 d+=random(70,120);
                  float a = constrain((range - d)*4, 0, 225);
                  stroke(150, a); 
                  if (level > 0) {
                    strokeWeight(1);
                  } else if (level > 1) {
                    strokeWeight(0.2);
                  }  
                  line(x1, y1, z1, x2, y2, z2);
    
    
                  println("d :" +d);
                  //println ("planets["+i+"]:"+planets[i].printPos());
                }
              }
              planets[i].display();  
              planets[i].move();
            }
          }
    
        popMatrix();
        textFont(font);
        textAlign(CENTER);
        pushMatrix();
        noStroke();
        float[] rotations = camera.getRotations();
        translate(x, y - (radius+20), z);
        rotateX(rotations[0]);
        rotateY(rotations[1]);
        rotateZ(rotations[2]);
        noStroke();
        scale(0.5);
        rectMode(CENTER);
        if (level>0) {
          fill(0, 200);
          rect(0, 0, nameSize.x+20, nameSize.y);
          fill(255, 250);
          translate(nameSize.x/50, nameSize.y/3, -10);
          text(name, 0, 0, 0);
        } else {
          fill(0, 10);
          rect(0, 0, nameSize.x+20, nameSize.y);
          fill(255, 250);
          translate(nameSize.x/50, nameSize.y/3, -10);
          text(name, 0, 0, 0);
        }
        popMatrix();
      }
    
    
    
      void satSelect() {
        this.isSelected= true;
        if (level > 1) {
          strokeColor = color(255, 0, 0);
          phiStep = 7;
          thetaStep= 0.05;
          noiseScaleZ=0;
        }
      }
      void satDeSelect() {
        this.isSelected = false;
        this.wasVisited = true;
        if (level > 1) {
          strokeColor = color(0, 255, 0);
          phiStep = 5;
          thetaStep= 0.08;
          noiseScaleZ=0;
        }
      }
    
    
      void drawNoisySphere() {
        float s = 0;
        float t = 0;
        float lastx = 0;
        float lasty = 0;
        float lastz = 0;
        float noiseScale = noise (random(10)*25);
        while (t < 180) {
          s += phiStep*(radius/150);
          t += thetaStep ;
    
          float radianS = radians(s);
          float radianT = radians(t);
          float thisx = (radius * cos(radianS) * sin(radianT))+ noiseScale; 
          float thisy = (radius * sin(radianS) * sin(radianT))+ noiseScale;
          float thisz = (radius * cos(radianT)) + ((noise(random(10))*25) * noiseScaleZ);
    
          if (lastx != 0) {
            line(thisx, thisy, thisz, lastx, lasty, lastz);
          }
          lastx = thisx;
          lasty = thisy;
          lastz = thisz;
        }
      }
    }
    
Sign In or Register to comment.