Point to array with custom class items, from inside class

I’m fooling around with openFrameworks for the first time (long time Processing user)

In my file, there is a class which defines a ball, moving around the screen:

  
class Ball {  
public:  
	ofPoint pos;  
	  
	void init() {  
		pos.set(ofRandomWidth(), ofRandomHeight(), 0);  
	}  
	  
	void update() {  
		//do all kinds of updating, like moving  
	}  
	  
	void draw() {  
		//draw the instance to the screen  
	}  
};  
Ball balls[NUM_POINTS];  
  

I can use the functions in the class instances by using a for-loop:

  
for(int i=0; i<NUM_POINTS; i++){  
	balls[i].update();  
}  

But is it also possible to use the functions and variables of the class instances inside my things[] array, from within those instances themself?

Like, I would like to move the ball to a new position, and then calculate the distance to the other balls. In processing I would put a function like this inside the class

  
  
void calculateDistance(){  
	float nearest = 999;  
	for(int i=0; i<NUM_POINTS; i++){  
		float distance = pox.x - balls[i].pos.x  
		if(distance < nearest){  
			nearest = distance;  
			//leaving you with the smallest distance  
		}  
	}  
}  
  

But in Xcode I get a message saying ‘balls’ is not inside this scope.
How can I point to a array which exists outside of the class instance?

Oh, and another small thing; what does the 0 parameter in pos.set(ofRandomWidth(), ofRandomHeight(), 0); stand for?

to have access to the array of balls you need to pass it as a parameter to the calculateDistance function, so the function will be:

  
void calculateDistance(Ball * balls, int num_balls){  
float nearest = 999;  
   for(int i=0; i<num_balls; i++){  
      float distance = pox.x - balls[i].pos.x  
      if(distance < nearest){  
         nearest = distance;  
         //leaving you with the smallest distance  
      }  
   }  
}  

and the call:

  
calculateDistance(balls, NUM_POINTS);  

the 0 stands for the z coordinate of the point

thanks for the quick reply! It works.
As said I’m new to OF and don’t fully understanding the different variables system (like with the asterisks *).

hi,

if you want to know more about the asterisk, I would recomend the binky-pointer-fun-video as a good starting point.

regards

david