How to use the current coordinates of an object outside its display function

Hello everybody,

I want to drew a graphical interface to control the parameters of few oscillators that I'm going to build through the Minim library. I created a class which consists of a circle with three other little circles inside and this object rotates around itself. (let's say a wheel with three points on it). With the display() function inside the class I drew in draw() three of these wheels.

Now, what I want to do is to get the current position on the screen of the smaller rotating circles of each wheel, in order to calculate the distance between each one of them and the other small circles of the other wheels. The aim is to map each value of the distance with a parameter of my oscillators.

If I calculate the distance in the void display() I cannot use it outside this function and I cannot do it in draw() for example, because the coordinates of the points are valid only in display().

How can I get and use the current position od each small circle?

Wheel wheelA;
Wheel wheelB;
Wheel wheelC;


void setup() {
  size(800, 800);  

  wheelA = new Wheel();
  wheelB = new Wheel();
  wheelC = new Wheel();
}

void draw() {
  background(255);

  pushMatrix();
  wheelA.display(180, 180, 0.02); 
  popMatrix();

  pushMatrix();
  wheelB.display(620, 180, 0.03); 
  popMatrix();

  pushMatrix();
  wheelC.display(400, 620, 0.01);  
  popMatrix();
}

class Wheel { 
  float posX;
  float posY;
  float ang= 0;
  float aVel;

  float xA;
  float yA;
  float xB;
  float yB;
  float xC;
  float yC;

  // The constructor defined
  Wheel() {
  }

  void  display(float posX, float posY, float aVel) {
    xA = 0;
    yA = 80;
    xB= 55;
    yB = -55;
    xC = -55;    
    yC = -55;


    //Wheel - big circle
    stroke(0);
    strokeWeight(3);
    fill(255);
    ellipseMode(CENTER);
    translate(posX, posY);  
    rotate(ang);
    ellipse(0, 0, 200, 200);

    //small circles
    ellipse(xA, yA, 10, 10);
    ellipse(xB, yB, 10, 10);
    ellipse(xC, yC, 10, 10);

    ang=ang+aVel;

  }
}

Answers

  • You haven't really made proper use of OOP (Object Oriented Programming).
    Why exactly is your constructor function empty? In your current code, there really isn't any use of keeping a seperate class. Have you read the Objects tutorial?
    What you should be doing is passing in the parameters to the constructor, and having each instance store its own values. If you read the tutorial, you will understand what I'm trying to say.

  • Another important note - you really must put the pushMatrix() and popMatrix() functions inside the display() function.

  • Thank you a lot for the answer!

    Why exactly is your constructor function empty? In your current code, there really isn't any use of keeping a seperate class.

    Ok, I got this. I post the updated code. But I still don't understand how to get the current coordinates of the little circles. Printing the value of the global variables that I set for the x,y of the circles, the value doesn't change despite the display() function makes them rotate.

    Wheel wheelA;
    Wheel wheelB;
    Wheel wheelC;
    
    float ax;  
    float ay; 
    float bx; 
    float by; 
    float cx;    
    float cy; 
    
    
    void setup() {
      size(800, 800);  
    
      ax = 0;
      ay = 80;
      bx = 55;
      by = -55;
      cx = -55;    
      cy = -55;
    
    
    
      wheelA = new Wheel(180, 180, 0.02, ax, ay, bx, by, cx, cy);
      wheelB = new Wheel(620, 180, 0.03, ax, ay, bx, by, cx, cy);
      wheelC = new Wheel(400, 620, 0.01, ax, ay, bx, by, cx, cy);
    }
    
    void draw() {
      background(255);
    
    
      wheelA.display();   
      wheelB.display();   
      wheelC.display();  
    
      println(ax);
    }
    
    class Wheel { 
      float posX;
      float posY;
      float ang= 0;
      float aVel;
    
      float xA;
      float yA;
      float xB;
      float yB;
      float xC;
      float yC;
    
      // The constructor defined
      Wheel(float posX_, float posY_, float aVel_, float xA_, float yA_, float xB_, float yB_, float xC_, float yC_) {
        posX = posX_;
        posY = posY_;
        aVel = aVel_;
    
        xA = xA_;
        yA = yA_;
        xB = xB_;
        yB = yB_;
        xC = xC_;    
        yC = yC_;
      }
    
      void  display() {
    
    
        //Wheel - big circle
        stroke(0);
        strokeWeight(3);
        fill(255);
        pushMatrix();
        ellipseMode(CENTER);
        translate(posX, posY);  
        rotate(ang);
        ellipse(0, 0, 200, 200);
    
        //small circles
        ellipse(xA, yA, 10, 10);
        ellipse(xB, yB, 10, 10);
        ellipse(xC, yC, 10, 10);
    
        ang=ang+aVel;
        popMatrix();
      }
    }
    
  • edited December 2016

    please don't pass the parameters that are inside the class to the display function - that defies the principle of class

    as has been said, pass them to the constructor instead (see tutorial)

    also: you can access values like this: wheelC.posX(apart from the fact that those values are not set in your code)

  • @Chrisir Did you read his last post?
    As to the question, I think your questions have been answered by now.

  • Or maybe not.
    So, do you know trigonometry? You need it.

  • No, I haven't seen his post. It must have been posted while I was still working at my post

    No use to give this ax, ay, bx, by, cx, cy to the constructor, they are the same for all objects

  • afaiK:

    https://www.processing.org/reference/screenX_.html

    and screenY

    (or was it modelX and modelY?)

    can give you coordinates after a matrix manipulation (like rotate)

    just store

    screenax, screenay, screenbx, screenby, screencx, screency

    using screenX and screenY

  • I doubt if that is a problem - he may be planning to have different values later on.
    Generally, it is not really recommended to use global variables if you're a beginner.

  • global variables!? No I would suggest to let them be part of the class

  • OK, that's what you meant. I misunderstood.

  • Thank you guys for the helpful answers. But even with the suggested improvements I still cannot solve my problem. I cannot get the current values of the coordinates of my objects, even if I put them in the constructor when I put it in a variable in draw() and print it, it's still not giving me the changing value of the coordinate.

    Wheel wheelA;
    Wheel wheelB;
    Wheel wheelC;
    
    float an1;  
    float an2; 
    float an3; 
    
    
    void setup() {
      size(800, 800);  
    
      wheelA = new Wheel(180, 180, 0.02, an1, an2, an3);
      wheelB = new Wheel(620, 180, 0.03, an1, an2, an3);
      wheelC = new Wheel(400, 620, 0.01, an1, an2, an3);
    }
    
    void draw() {
      background(255);
    
      wheelA.display();  
      wheelB.display();   
      wheelC.display();  
    
      float par = wheelA.angle1;
      println(par);
    }
    class Wheel { 
      float posX;
      float posY;
      float ang= 0;
      float aVel;
    
      float angle1;
      float angle2;
      float angle3;
    
    
      // The constructor defined
      Wheel(float posX_, float posY_, float aVel_, float angle1_, float angle2_, float angle3_) {
        posX = posX_;
        posY = posY_;
        aVel = aVel_;
    
        angle1 = angle1_;
        angle2 = angle2_;
        angle3 = angle3_;
      }
    
      void  display() {
    
        //Wheel - big circle
        stroke(0);
        strokeWeight(3);
        fill(255);
        pushMatrix();
        ellipseMode(CENTER);
        translate(posX, posY);  
        rotate(ang);
        ellipse(0, 0, 200, 200);
    
    
        //small circles
        float angle1 = 0;
        float xA= 80*cos(angle1);
        float yA= 80*sin(angle1);
        ellipse(xA, yA, 10, 10);
    
    
        float angle2 = 2.2;
        float xB= 80*cos(angle2);
        float yB= 80*sin(angle2);
        ellipse(xB, yB, 10, 10);
    
        float angle3 = 4.4;
        float xC= 80*cos(angle3);
        float yC= 80*sin(angle3);
        ellipse(xC, yC, 10, 10);
    
    
        ang=ang+aVel;
         popMatrix();
    
      }
    }
    
  • ScreenX and screenY??

  • Now, you have two choices -

    • Try using @Chrisir's method.
    • Directly rely on the fact that each instance of your class has its own ang variable.
Sign In or Register to comment.