Skip to content

Instantly share code, notes, and snippets.

@jeffcrouse
Created October 13, 2017 19:34
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 jeffcrouse/98dda238fa891025ff22c3dbf5035e0a to your computer and use it in GitHub Desktop.
Save jeffcrouse/98dda238fa891025ff22c3dbf5035e0a to your computer and use it in GitHub Desktop.
End point code for Week 6
#include "ofApp.h"
#define NUM_PARTICLES 100
class Particle {
public:
void setup(ofPoint _pos, ofPoint _vel) {
pos = _pos;
vel = _vel;
}
void update() {
pos += vel;
alpha *= 0.99;
if(pos.x > ofGetWidth()-radius) {
pos.x = ofGetWidth()-radius;
vel.x *= -1;
}
if(pos.y > ofGetHeight()-radius) {
pos.y = ofGetHeight()-radius;
vel.y *= -1;
}
if(pos.x < radius) {
pos.x = radius;
vel.x *= -1;
}
if(pos.y < radius) {
pos.y = radius;
vel.y *= -1;
}
}
void draw() {
ofSetColor(0, alpha);
ofDrawCircle(pos, radius);
}
float radius = 5;
ofPoint pos;
ofPoint vel;
float alpha = 255;
};
vector<Particle> particles;
ofPoint lastMousePos;
//--------------------------------------------------------------
void ofApp::setup(){
}
//--------------------------------------------------------------
void ofApp::update(){
for(int i=particles.size()-1; i>=0; i--) {
particles[i].update();
if(particles[i].alpha < 1) {
particles.erase(particles.begin()+i);
}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
for(int i=0; i<particles.size(); i++) {
particles[i].draw();
}
ofSetColor(0);
ofDrawBitmapString(ofToString(particles.size()) + "\n" + ofToString(ofGetFrameRate()), 10, 20);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
ofPoint pos(x,y);
ofPoint vel = pos-lastMousePos;
vel.limit(5);
vel.x += ofRandom(-4, 4);
vel.y += ofRandom(-4, 4);
Particle p;
p.setup( pos, vel );
particles.push_back( p );
lastMousePos.set(x,y);
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment