Multiple Instances from a Custom Class

Hi everyone, I finally am trying to learn how to properly code OO :wink: I want to make a simple class that draws two circles that get passed variables from testApp.cpp. The following code only draws one circle to the screen. Any idea on why I only see one?

testApp.cpp

vector<Particle> particles;


//--------------------------------------------------------------
void testApp::setup(){
    
    ofSetFrameRate(1);
    
    particles[0].setup();
    particles[1].setup();
    
}

//--------------------------------------------------------------
void testApp::update(){
    
    particles[0].update(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()));
    particles[1].update(ofRandom(0, ofGetWidth()-20), ofRandom(0, ofGetHeight()-20));
    
}

//--------------------------------------------------------------
void testApp::draw(){
    
    ofBackground(255, 255, 255);

    particles[0].draw();
    particles[1].draw();
    
}

and Particle.cpp

#include "Particle.h"

int myCircleX;
int myCircleY;

//--------------------------------------------------------------
void Particle::setup(){
    
    myCircleX = ofRandom(0, ofGetWidth());
    myCircleY = ofRandom(0, ofGetHeight());
}

//--------------------------------------------------------------
void Particle::update(int randx, int randy){
    
    myCircleX = randx;
    myCircleY = randy;
    
}

//--------------------------------------------------------------
void Particle::draw(){
    
    ofSetColor(67, 67, 67, 255);
    ofCircle(myCircleX, myCircleY, 10);
    
}

I think you have to properly fill up your vector first. Try to add this before calling the particle setup functions.

particles.push_back(Particle());
particles.push_back(Particle());

Hi @andehlu,
From your code it appears your vector is actually empty. You need to push back new instances of your Particle class, for example in a loop like this:

for (int i=0; i<100; i++) {
        Particle p;
        p.setup();
        particles.push_back(p);
    }

You can use a similar loop to draw your circles in the draw() function. Not sure if this is the behaviour you want, but remember that you are updating the position of your particles every frame, so they are jumping all over the place.

EDIT @underdoeg beat me to it :wink:

I don’t even know how your getting one circle to draw.

You haven’t added your particle to the particles vector

in your setup you could try

int numOfParticles = 10;
vector<Particle*> particles;

for(int i = 0; i < numOfParticles; i++)
{
    Particle *p = new Particle();
    particles.push_back(p);
}

Then in your draw you can just loop over every particle in your vector

 for(int i = 0; i < numOfParticles; i++)
 {
        particles[i]->draw();
 }

haha. seems like we need the stackoverflow system where you can pick your favorite answer. :stuck_out_tongue:

Wow, thanks for this guys. The stackoverflow system would work well here, I think. So, I made a mashup of the responses so far. Thanks, I had never heard of push_back before. That said, I am still getting one circle appearing. My Particle.cpp hasnt changed from above and my testApp.cpp is changed below. Is there any chance all 10 circles are being drawn at the same x,y?

int numOfParticles = 10;
vector<Particle> particles;

//--------------------------------------------------------------
void testApp::setup(){
    
    ofSetFrameRate(1);
    
    for (int i=0; i<numOfParticles; i++) {
        Particle p;
        p.setup();
        particles.push_back(p);
        
        cout << "called" << endl;
    }
    
}

//--------------------------------------------------------------
void testApp::update(){
    
    for (int i=0; i<numOfParticles; i++) {
        particles[i].update(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()));
    }
    
}

//--------------------------------------------------------------
void testApp::draw(){
    
    ofBackground(255, 255, 255);

    for(int i = 0; i < numOfParticles; i++){
        particles[i].draw();
    }
    
}

I just tested your code.

It works. All 10 particles get drawn in different locations. Do you have any other code that may be interfering?

class Particle
{
    
public:
    int myCircleX;
    int myCircleY;
    
    //--------------------------------------------------------------
    void setup(){
        
        myCircleX = ofRandom(0, ofGetWidth());
        myCircleY = ofRandom(0, ofGetHeight());
    }
    
    //--------------------------------------------------------------
    void update(int randx, int randy){
        
        myCircleX = randx;
        myCircleY = randy;
        
    }
    
    //--------------------------------------------------------------
    void draw(){
        
        ofSetColor(67, 67, 67, 255);
        ofCircle(myCircleX, myCircleY, 10);
        
    }
};

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    
    int numOfParticles;
    vector<Particle> particles;
		
};

void ofApp::setup(){
    ofSetFrameRate(1);
    numOfParticles = 10;
    for (int i=0; i<numOfParticles; i++) {
        Particle p;
        p.setup();
        particles.push_back(p);
        
        cout << "called" << endl;
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    for (int i=0; i<numOfParticles; i++) {
        particles[i].update(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()));
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    ofBackground(255, 255, 255);
    
    for(int i = 0; i < numOfParticles; i++){
        particles[i].draw();
    }
}

Thanks @az_rr I have no code that should be interfering… I have updated all my code to this…and still only get one circle. Thoughts?

testApp.h

#pragma once

#include "ofMain.h"

class Particle{
    
    public:
        int myCircleX;
        int myCircleY;
        
        //--------------------------------------------------------------
        void setup(){
            myCircleX = ofRandom(0, ofGetWidth());
            myCircleY = ofRandom(0, ofGetHeight());
        }
        
        //--------------------------------------------------------------
        void update(int randx, int randy){
            myCircleX = randx;
            myCircleY = randy;
        }
        
        //--------------------------------------------------------------
        void draw(){
            ofSetColor(67, 67, 67, 255);
            ofCircle(myCircleX, myCircleY, 10);
        }
};

class testApp : public ofBaseApp{
	public:
		void setup();
		void update();
		void draw();
		
		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y);
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    
        int numOfParticles;
        vector<Particle> particles;

};

testApp.cpp

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
    
    ofSetFrameRate(1);
    
    numOfParticles = 100;
    
    for (int i=0; i<numOfParticles; i++) {
        Particle p;
        p.setup();
        particles.push_back(p);
        
        cout << "called" << endl;
    }
    
}

//--------------------------------------------------------------
void testApp::update(){
    
    for (int i=0; i<numOfParticles; i++) {
        particles[i].update(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()));
    }
    
}

//--------------------------------------------------------------
void testApp::draw(){
    
    ofBackground(255, 255, 255);

    for(int i = 0; i < numOfParticles; i++){
        particles[i].draw();
    }
    
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 

}

Sorry this does work. Thank you for your help everyone.

1 Like