Skip to content

Instantly share code, notes, and snippets.

@danzeeeman
Created December 2, 2022 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danzeeeman/5199ec04516e838262ac178bcf1f87f9 to your computer and use it in GitHub Desktop.
Save danzeeeman/5199ec04516e838262ac178bcf1f87f9 to your computer and use it in GitHub Desktop.
write an openFrameworks particle system
#include "ofMain.h"
class Particle {
public:
ofPoint position;
ofPoint velocity;
float radius;
ofColor color;
Particle() {
position.set(ofRandom(ofGetWidth()), ofRandom(ofGetHeight()));
velocity.set(ofRandom(-10, 10), ofRandom(-10, 10));
radius = ofRandom(3, 15);
color.setHsb(ofRandom(255), 200, 200);
}
void update() {
position += velocity;
// Add a gravitational pull towards the center of the screen
ofPoint center(ofGetWidth() / 2, ofGetHeight() / 2);
ofPoint direction = center - position;
float distance = direction.length();
direction.normalize();
float strength = 0.1;
velocity += direction * strength;
// Bounce off the edges of the screen
if (position.x < 0 || position.x > ofGetWidth()) {
velocity.x *= -1;
}
if (position.y < 0 || position.y > ofGetHeight()) {
velocity.y *= -1;
}
}
void draw() {
ofSetColor(color);
ofDrawCircle(position, radius);
}
};
class ofApp : public ofBaseApp{
public:
vector<Particle> particles;
void setup() {
ofBackground(0);
for (int i = 0; i < 1000; i++) {
particles.push_back(Particle());
}
}
void update() {
for (int i = 0; i < particles.size(); i++) {
particles[i].update();
}
}
void draw() {
for (int i = 0; i < particles.size(); i++) {
particles[i].draw();
}
}
};
int main() {
ofSetupOpenGL(1024, 768, OF_WINDOW);
ofRunApp(new ofApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment