How to acces a class's variable value from another class

Hi! I’m working on a generative visual that uses an array of classes to build a mass of particles around the screen. So what i do is, declare an array of class first and fill and run each object in the testapp.cpp. however what I want to do is calculating the distance of other particles within each class object. I used to do this in processing by only referring to the class but in c++ this technique doesn’t seem to work.

how can i access the values of other class objects’ Acproperties from an object?

Processing Example

for (int i=0; i< circArray.length; i++) {
      Sphere otherCirc = circArray[i];

      if (otherCirc != this) {  
        float dis = dist(x, y, z, otherCirc.x, otherCirc.y, otherCirc.z); 
        float overlap = dis  - 10; 

        if (overlap < l1nes) {
          strokeWeight(0.5);
          stroke(0, 70);
          
          vertex(otherCirc.x, otherCirc.y, otherCirc.z);
          vertex(x, y, z);
         
        }

OpenFrameworks:

    void testApp::setup(){
            nSpheres = 100; 
            aSphere = new particles*[nSpheres]; 
    
            for (int i = 0; i < nSpheres; i++){
                aSphere[i] = new particles(ofRandom(i)*200, i*5, 200,ofRandom(0,5));       
            }
        }
    
    void testApp::update(){     
        for (int i = 0; i < nSpheres; i++){
            aSphere[i]->update(radius*ofNoise(float(i)), 5);      
        };
    }
    
    void testApp::draw(){
        for (int i = 0; i < nSpheres; i++){
            aSphere[i]->draw();
        }
        
    }

Here is how the class looks like

      particles::particles(float _s, float _t, float _rad,float _speed)
    {
        rad = _rad;
        s = _s;
        t = _t;
        speed = _speed;
    }
    
    
    void particles::update(float nRad, float nSpeed){
        
        rad +=  (nRad - rad ) * 0.1;

        radianS = ofDegToRad(s);
        radianT = ofDegToRad(t);
        
        x =  ((200+rad)  * cos(radianS ) * sin(radianT) );
        y =  ((200+rad) * sin(radianS) * sin(radianT));
        z =  ((200+rad) * cos(radianT));
        
        finSpeed =  speed + nSpeed;

    }
    

    void particles::draw(){
     /// draw spheres here.
        
    }

In Processing, you were probably able to accomplish this because of Processing / Java’s concept of inner classes. If you use the Processing IDE and you don’t explicitly use external Java files, etc, then your classes will be nested inside of the main PApplet class. This basically gives them access to everything that is at their nesting level or above.

C++ has the ability to use nested classes, but this pattern is not used heavily and does not exactly mimic the functionality you get in the Processing environment. So, I would recommend going a different route.

You could approach it in several ways:

  1. Pass a reference (or a pointer) to the whole collection to each particle along with the nRad and nSpeed variables. Without knowing what time aSphere is, this might look like: void particles::update(std::vector<particles>& system, float nRad, float nSpeed). You might then cycle through the particles and compute distances.
  2. The downside to that approach is that you will likely duplicate a lot of computations. Instead you might take a more top-down approach and do the particle-particle interaction/distance drawing calculations in your main app using a loop like:
void testApp::update()
{     
    for (std::size_t i = 0; i < nSpheres; ++i)
    {
        aSphere[i]->update(radius*ofNoise(float(i)), 5);
    }
     
    for (std::size_t i = 0; i < nSpheres; ++i)
    {
        for (std::size_t  j = i + 1; j < nSpheres; ++j)
        {
            // do rendering based on aSphere[i] and aSphere[j]
        }
    }

}
3 Likes

Thanks a lot ! Your second suggestion worked smoothly.

1 Like