How to make a basic particle engine as shown in the tutorials

I started off by following the tutorials at Hello Cinder except they turned out to be slightly outdated. In the samples there is also a Particles project but it had way more complicated things like perlin noise

I took both concepts and wrote this

#include <cinder/app/App.h>
#include <cinder/app/RendererGl.h>
#include <cinder/Rand.h>
#include <cinder/gl/draw.h>
#include <cinder/gl/wrapper.h>

using namespace ci;
using namespace ci::app;

#define NUM_PARTICLES 50

class Particle {
public:
	vec2 mPosition = randVec2();
	vec2 mVelocity = randVec2();
	vec2 mDirection = randVec2();
	void update()
	{
		mPosition = mDirection * mVelocity;
	}
	void draw()
	{
		gl::drawSolidCircle(mPosition, 10.0f);
	}
};

class BasicApp : public App {
  public:
	void setup() override;
	void update() override;
	void draw() override;
	void resize() override;

	std::vector<Particle>	mParticles;	
};

void prepareSettings( BasicApp::Settings* settings ) 
{
	settings->setWindowSize(1000,700);
	settings->setTitle("BasicApp");
}

void BasicApp::setup() 
{
	mParticles.reserve( NUM_PARTICLES );
}

void BasicApp::resize()
{
	gl::setMatricesWindow(getWindowSize());
}

void BasicApp::update() 
{
	for( auto &particle : mParticles ) 
	{
		particle.update();
	}
}

void BasicApp::draw()
{
	gl::clear();
	for( auto &particle : mParticles ) 
	{
		particle.draw();
	}
}

CINDER_APP( BasicApp, RendererGl, prepareSettings )

But the screen is just pure black


What am I doing wrong?

Couple of small things:

  1. mParticles.reserve( NUM_PARTICLES ); doesn’t instantiate any particles, it just allocates the memory in preparation for them. What you want in this case is mParticles.resize ( NUM_PARTICLES ); which will allocate the necessary space and initialize the particles for you.

  2. After that, you’re setting your particles to the same position every frame with mPosition = mDirection * mVelocity;. You probably meant to append to the position, i.e mPosition += mDirection * mVelocity;

There’s a few further issues you’re going to run into shortly after these, but it’s a good start and since it seems to be a learning exercise it’s best you try and solve them yourself if/when they arise :slight_smile:

A

1 Like