Adding particles to the array using key press

Hi, I’ve been playing around with the particle demo that’s provided by OF and have been trying to add particles to the array using the spacebar however it doesn’t seem to work and I’m not sure why.

So the number of particles are initialised in testApp.setup like so

int num = 10;

Then I’ve made another function to add to the array.

void testApp::updateParticles(float _n)
{
num += _n;
}

Then am calling it in void testApp::keyPressed(int key){
if (key == ’ '){
updateParticles(2);
}
}

Please tell me where I’ve gone wrong cause I really don’t understand.

Where are you updating the array? maybe if you paste everything the error will be easier to spot…

#include “testApp.h”

//--------------------------------------------------------------
void testApp::setup(){
ofSetVerticalSync(true);

num = 10;     

p.assign(num, demoParticle());
currentMode = PARTICLE_MODE_ATTRACT;

currentModeStr = "1 - PARTICLE_MODE_ATTRACT: attracts to mouse"; 

resetParticles();

}

//--------------------------------------------------------------
void testApp::resetParticles(){

//these are the attraction points used in the forth demo 
attractPoints.clear();
for(int i = 0; i < 4; i++){ // Push Back adds elements to the arry
	attractPoints.push_back( ofPoint( ofMap(i, 0, 4, 100, ofGetWidth()-100) , ofRandom(100, ofGetHeight()-100) ) );
}

attractPointsWithMovement = attractPoints;

for(unsigned int i = 0; i < num; i++){
	p[i].setMode(currentMode);		
	p[i].setAttractPoints(&attractPointsWithMovement);;
	p[i].reset(ofPoint(1,0,0));
}	

}

//--------------------------------------------------------------
void testApp::update(){
for(unsigned int i = 0; i < p.size(); i++){
p[i].setMode(currentMode);
p[i].update();
}

//lets add a bit of movement to the attract points
for(unsigned int i = 0; i < attractPointsWithMovement.size(); i++){
	attractPointsWithMovement[i].x = attractPoints[i].x + ofSignedNoise(i * 10, ofGetElapsedTimef() * 0.7) * 12.0;
	attractPointsWithMovement[i].y = attractPoints[i].y + ofSignedNoise(i * -10, ofGetElapsedTimef() * 0.7) * 12.0;
}	

}

//--------------------------------------------------------------
void testApp::draw(){
ofBackgroundGradient(ofColor(60,60,60), ofColor(10,10,10));

for(unsigned int i = 0; i < p.size(); i++){
	p[i].draw();
}

ofSetColor(190);
if( currentMode == PARTICLE_MODE_NEAREST_POINTS ){
	for(unsigned int i = 0; i < attractPoints.size(); i++){
		ofNoFill();
		ofCircle(attractPointsWithMovement[i], 10);
		ofFill();
		ofCircle(attractPointsWithMovement[i], 4);
	}
}

ofSetColor(230);	
ofDrawBitmapString(currentModeStr + "\n\nSpacebar to reset. \nKeys 1-4 to change mode.", 10, 20);

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
if( key == ‘1’){
currentMode = PARTICLE_MODE_ATTRACT;
currentModeStr = “1 - PARTICLE_MODE_ATTRACT: attracts to mouse”;
updateParticles(1);

}
if( key == '2'){
	currentMode = PARTICLE_MODE_REPEL;
	currentModeStr = "2 - PARTICLE_MODE_REPEL: repels from mouse"; 
	
}
if( key == '3'){
	currentMode = PARTICLE_MODE_NEAREST_POINTS;
	currentModeStr = "3 - PARTICLE_MODE_NEAREST_POINTS: hold 'f' to disable force"; 						
}
if( key == '4'){
	currentMode = PARTICLE_MODE_NOISE;
	currentModeStr = "4 - PARTICLE_MODE_NOISE: snow particle simulation"; 						
	//resetParticles();
}	

if( key == ' ' ){
	updateParticles(2);
	//resetParticles();
	
}

}

void testApp::updateParticles(float _n)
{
num += _n;

}

Yes I think you need to re assign the number “num” to the particle class “p”. The variable num is just a value… it doesn’t have any relation to particle until you assign it.
try something like this:

void testApp::updateParticles(float n)
{
num += n;
p.assign(num, demoParticle());
}

maybe you will need to reset particles too… not sure what the rest of the code is…

That get’s rid of the particles all together

I’ve tried to reset them as well but that doesn’t seem to work.
It’s just a basic particle system, the OF demo.

this works for me:

void ofApp::updateParticles(float n)
{
	num += n;
	p.assign(num, demoParticle());
	resetParticles();
}

Oh I had the reset before the p.assign.
Thank you for your help!!

:smile: