Problem with a particle system tutorial


Hi everyone.
Just to take my first steps with OF, I started following this-tutorial.

I’m currently stuck in the first part (when you sample the image’s pixels and then show the particles). I pretty much understand the logic of the code (and I was able to correct an error in the vector variable declaration), but still I have problems making it work. Basically all I get is a black screen.

I tried to put some debugs to see what’s happening. It seems the image is sampled correctly, but when it comes to get the colors from the particles stored in the vector, something wrong is going on: at this point all particles show a color of 0,0,0,0.

So the problem should lie in the way the vector is populated. Unfortunately I’m really a newbie with c++, so I cannot understand precisely what’s wrong. I post all the code here:

#ifndef myParticles_Particle_h  
#define myParticles_Particle_h  
#include "ofMain.h"  
class Particle  
    Particle (ofPoint _position, ofColor _color)  
        position = _position;  
        color = _color;  
    ofPoint position, velocity;  
    ofColor color;  


#pragma once  
#include "ofMain.h"  
#include "Particle.h"  
class testApp : public ofBaseApp{  
		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);  
        ofImage image;  
        int sampling; //pixels to skip;  
        vector<Particle> particles;  
        int numParticles;  
        //spring and sink factors  
    bool springEnabled;  
    float forceRadius;  
    float velocityDampingFactor;  
    float springFactor;  
    int cursorMode;  

and finally the actual code, testApp.cpp:

#include "testApp.h"  
void testApp::setup(){  
    unsigned char * pixels = image.getPixels();  
    int w = image.width;  
    int h = image.height;  
    int xOffset = (ofGetWidth()-w)/2;  
    int yOffset = (ofGetHeight()-w)/2;  
    for (int x=0; x<w; x+=sampling)  
        for (int y=0; y<h; y+=sampling)  
            int index = (y * w + x)*3;  
            ofColor color;  
            particles.push_back(Particle(ofPoint(x+xOffset, y+yOffset),color));  
void testApp::update(){  
    std::vector<Particle>::iterator p;  
    for (p=particles.begin(); p!=particles.end(); p++) {  
void testApp::draw(){  
    std::vector<Particle>::iterator p;  
    for (p=particles.begin(); p!=particles.end(); p++);  
        ofColor color = p->color;  
        ofLog(OF_LOG_NOTICE, "the color is " + ofToString(color));  
        ofCircle(p->position.x, p->position.y, 1);  

Where am I wrong?


I just copied the example, fixing the vector declaration (vector) also, it worked ok :slight_smile:

Are you sure you have the image in the correct place?

example_root/src/main.cpp <-- and all the code

I’m attaching the code :slight_smile:


Found the error.
There was a STUPID semicolon after the For declaration in the draw method.
Thank you so much :slight_smile:


Where is the tutorial?