How to get the width and x values of a custom object?

Hi all,

I've created an object called Cloud that consists of may circles. The Cloud is animated and moves out of the screen at some point. I want to check if it has left the screen completely so I can remove it from my arrayList. But I don't know how to get to the width and x values of the whole Cloud object. Is there a way to do this?

Thanks very much in advance for your help, best, Danielle.

Tagged:

Comments

  • Have you created a class for the Cloud? If so could you show the code for it?

  • edited February 2015
    class Cloud{
      PVector location;
      PVector velocity;
      PVector acceleration;
      float t, t1, t2;
      int cloudCircles;
      float[] rVals;
      PVector[] wholeCloud;
    
      float baseHeight;
      Random random = new Random();
      boolean high;
      float mass;
      PVector w;
    
      Cloud(PVector wind_){
        w = wind_;
        velocity = new PVector(0,0);
        acceleration = new PVector(0,0);
    
        cloudCircles = int(random(50,100));
        baseHeight = random(60, height/4);
        //velocity = random(-0.2,0);
        t = random(0.3, 0.4);
        t1 = random(0.90, 1);
        high = random.nextBoolean();
        println("high = "+high);
        if(high){   
          println("w "+w);   
          cloudCircles = int(random(70,120));
          rVals = new float[cloudCircles];
          wholeCloud = new PVector[cloudCircles];
          baseHeight = random(-10, 50);
          for(int i = 0; i < cloudCircles; i++){
            float n = noise(t);
            float x = map(n, 0, 1, 0, 400); 
            float n1 = noise(t1);
            float y = map(n1, 0, 1, 0, 60);//
            float n2 = noise(t2);
            float r = map(n2, 0, 1, 30, 100);
            if(w.x<0) // from left
              location = new PVector(x + width, y + baseHeight);
            else // from right
              location = new PVector(0 - x, y + baseHeight);
            wholeCloud[i] = location;
            rVals[i] = r;
            t+=0.35;
            t1+=0.99;
            t2+=0.45;
          }
        } else {
          rVals = new float[cloudCircles];
          wholeCloud = new PVector[cloudCircles];
          //println("low cloud");
          for(int i = 0; i < cloudCircles; i++){
            float n = noise(t);
            float x = map(n, 0, 1, 0, 200); // width of total cloud
            float n1 = noise(t1);
            float y = map(n1, 0, 1, 0, 50);//
            float n2 = noise(t2);
            float r = map(n2, 0, 1, 10, 50);
            if(w.x<0)
              location = new PVector(x + width, y + baseHeight);
            else
              location = new PVector(0 - x, y + baseHeight);
            wholeCloud[i] = location;
            rVals[i] = r;
            t+=0.35;
            t1+=0.99;
            t2+=0.45;
          }
        }
        mass = cloudCircles;
      }
    
      void update(){
        for(int i = 0; i < cloudCircles; i++){
          velocity.add(acceleration);
          wholeCloud[i].add(velocity);
          acceleration.mult(0);
        }
      }
    
      void applyForce(PVector force){
        PVector f = PVector.div(force,mass);
        acceleration.add(f);
      }
    
      void display(){
        noStroke();
        fill(240);
        for(int i = 0; i < cloudCircles; i++){
          ellipse(wholeCloud[i].x, wholeCloud[i].y, rVals[i], rVals[i]/4);
        }
      }
    }
    
  • Hmm, something went wrong with the formatting, hope you can still understand?

  • I have corrected the code formatting for you in future highlight the code and press Ctrl+O (that is the letter O, in the previous forum it was K and hopefully it will go back to that sometime)

    Just to clarify

    wholeCloud is an array of the circle centres, and rVals is the radius of each circle it that correct?

  • absolutely correct

  • To be a bit more precise, a PVector with x and y values for the circles

  • To be a bit more precise, a PVector with x and y values for the circles

  • edited February 2015

    There are several ways that you could program a solution. The code below is one such way, it simply tests to see if a part of the cloud fits inside a rectangle, if it does then the cloud must be visible and the testing stops.

    Add this method to your class. I haven't tested it but I believe it is correct. Simply call the method with the position of the rectangle you are interested in.

    boolean isInside(float lowX, float highX, float lowY, float highY) {
      for (int i = 0; i < wholeCloud.length; i++) {
        PVector v = wholeCloud[i];
        float r = rVals[i];
        boolean outside = v.x + r < lowX || v.x - r > highX || v.y + r < lowY || v.y - r > highX;
        if (!outside)
          return true;
      }
      return false;
    }
    
  • BTW I made a mistake in the code above I have changed it :)

  • Wow, thanks very much, that's very elegant! So you constantly check the whole PVector array and add the radius to the location to see if it's completely left view. Great, works like a charm.

  • The advantage of doing it that way is that not only can the clouds move but also the circles can move within the cloud if you want. This is a much more flexible approach than calculating the cloud 'limits'.

Sign In or Register to comment.