Weitere ähnliche Inhalte Ähnlich wie openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートII (20) Mehr von Atsushi Tadokoro (20) Kürzlich hochgeladen (20) openFrameworks – パーティクルを動かす、静的配列と動的配列 - 多摩美メディアアートII11. To invent programs, you need to be able to capture abstractions and ex
design. It’s the job of a programming language to help you do this. The
process of invention and design by letting you encode abstractions tha
It should let you make your ideas concrete in the code you write. Surf
the architecture of your program.
‣
‣ All programming languages provide devices that help express abstrac
are ways of grouping implementation details, hiding them, and giving
‣ a common interface—much as a mechanical object separates its interfa
illustrated in “Interface and Implementation” .
‣
Figure 2-1 Inte rfa ce a nd Im ple m e nta tion
interface implementation
11
10
9
8
7
6
24. ‣
#pragma once
#include "ofMain.h"
class Particle {
public:
!
ofVec2f pos; //
ofVec2f vel; //
ofVec2f frc; // ( )
float damping; //
Particle();
virtual ~Particle(){};
void resetForce();
void addForce(float x, float y);
void addDampingForce();
void setInitialCondition(float px, float py, float vx, float vy);
void update();
void draw();
protected:
private:
};
25. ‣
#include "Particle.h"
// ( )
Particle::Particle(){
! setInitialCondition(0,0,0,0);
! damping = 0.01f;
}
// ( )
void Particle::resetForce(){
frc.set(0,0);
}
//
void Particle::addForce(float x, float y){
frc.x = frc.x + x;
frc.y = frc.y + y;
}
//
void Particle::addDampingForce(){
frc.x = frc.x - vel.x * damping;
frc.y = frc.y - vel.y * damping;
}
28. ‣
#pragma once
#include "ofMain.h"
#include "Particle.h"
class testApp : public ofSimpleApp{
!
public:
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();
// Particle
Particle p;
};
29. ‣
#include "testApp.h"
void testApp::setup(){!
! ofSetVerticalSync(true);
! ofSetFrameRate(60);
! ofBackground(0, 0, 0);
! p.setInitialCondition(ofGetWidth()/2, ofGetHeight()/2,
ofRandom(-10,10), ofRandom(-10,10));
}
void testApp::update(){
! p.resetForce();
! p.addDampingForce();
! p.update();
}
void testApp::draw(){
! ofSetColor(255, 255, 255);
! p.draw();
}
38. ‣
‣
Particle p[NUM]
p[0]
p[1]
p[2] NUM
p[NUM]
39. ‣
#pragma once
#include "ofMain.h"
#include "Particle.h"
#define NUM 100
class testApp : public ofSimpleApp{
!
public:
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();
// Particle (NUM )
Particle p[100];
};
40. ‣
#include "testApp.h"
void testApp::setup(){!
! ofSetVerticalSync(true);
! ofSetFrameRate(60);
! ofBackground(0, 0, 0);
}
void testApp::update(){
for (int i = 0; i < NUM; i++) {
p[i].resetForce();
p[i].addForce(0, 0.1);
p[i].addDampingForce();
p[i].update();
}
}
void testApp::draw(){
! ofSetColor(255, 255, 255);
for (int i = 0; i < NUM; i++) {
p[i].draw();
}
}
43. ‣
Vector <Particle> particles
particles[0]
particles[1]
particles[2]
44. ‣
‣
‣
particles.push_back(p);
‣
particles.pop_back();
‣
particles.clear();
45. ‣
#pragma once
#include "ofMain.h"
#include "Particle.h"
#define NUM 100
class testApp : public ofSimpleApp{
!
public:
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();
// Particle particles
vector <Particle> particles;
};
46. ‣
#include "testApp.h"
void testApp::setup(){!
! ofSetVerticalSync(true);
! ofSetFrameRate(60);
! ofBackground(0, 0, 0);
}
void testApp::update(){
for (int i = 0; i < particles.size(); i++) {
particles[i].resetForce();
particles[i].addForce(0, 0.1);
particles[i].addDampingForce();
particles[i].update();
}
}
void testApp::draw(){
! ofSetColor(255, 255, 255);
for (int i = 0; i < particles.size(); i++) {
particles[i].draw();
}
}
47. ‣
void testApp::mousePressed(int x, int y, int button){
particles.clear();
for (int i = 0; i < NUM; i++) {
//Particle → myParticle
Particle myParticle;
//
float vx = ofRandom(-10, 10);
float vy = ofRandom(-10, 10);
myParticle.setInitialCondition(x, y, vx, vy);
//
particles.push_back(myParticle);
}
}
50. ‣
#pragma once
#include "ofMain.h"
#include "Particle.h"
class testApp : public ofSimpleApp{
!
public:
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();
// Particle particles
vector <Particle> particles;
};
51. ‣
#include "testApp.h"
void testApp::setup(){!
! ofSetVerticalSync(true);
! ofSetFrameRate(60);
! ofBackground(0, 0, 0);
}
void testApp::update(){
for (int i = 0; i < particles.size(); i++) {
particles[i].resetForce();
particles[i].addDampingForce();
particles[i].update();
}
}
void testApp::draw(){
! ofSetColor(255, 255, 255);
! //
! string message = "current particle num = " + ofToString(particles.size(),0);
! ofDrawBitmapString(message, 20, 20);
for (int i = 0; i < particles.size(); i++) {
particles[i].draw();
}
}
52. ‣
void testApp::keyPressed (int key){
! //'c'
! if (key == 'c') {
! ! particles.clear();
! }
! //'f'
! if (key == 'f') {
! ! ofToggleFullscreen();
! }
}
void testApp::mouseDragged(int x, int y, int button){
! //
! Particle myParticle;
! float vx = ofRandom(-3, 3);
! float vy = ofRandom(-3, 3);
! myParticle.setInitialCondition(x, y, vx, vy);
! particles.push_back(myParticle);
}
56. ‣
void testApp::draw(){
! ofSetColor(255, 255, 255);
! //
! string message = "current particle num = "
! + ofToString(particles.size(),0);
! ofDrawBitmapString(message, 20, 20);
!
! ofNoFill();
! ofBeginShape();
! for (int i = 0; i < particles.size(); i++){
! ! ofCurveVertex(particles[i].pos.x, particles[i].pos.y);
! }
! ofEndShape();
}
59. ‣
#pragma once
#include "ofMain.h"
#include "Particle.h"
class testApp : public ofSimpleApp{
!
public:
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();
// Particle particles
vector <Particle> particles;
! //
! ofImage img;
};
60. ‣
#include "testApp.h"
void testApp::setup(){!
! ofSetVerticalSync(true);
! ofSetFrameRate(60);
! ofBackground(0, 0, 0);
! ofEnableBlendMode(OF_BLENDMODE_ADD);
! //
! img.loadImage("particle32.png");
}
void testApp::update(){
for (int i = 0; i < particles.size(); i++) {
particles[i].resetForce();
particles[i].addDampingForce();
particles[i].update();
}
}
61. ‣
void testApp::draw(){
! //
! ofSetColor(255, 255, 255);
! string message = "current particle num = " + ofToString(particles.size(),0);
! ofDrawBitmapString(message, 20, 20);
! //
! for (int i = 0; i < particles.size(); i++){
! ! float posx = particles[i].pos.x - 16;
! ! float posy = particles[i].pos.y - 16;
! ! img.draw(posx, posy);
! }
}
void testApp::keyPressed (int key){
! //'c'
! if (key == 'c') {
! ! particles.clear();
! }
! //'f'
! if (key == 'f') {
! ! ofToggleFullscreen();
! }
}
62. ‣
void testApp::mouseDragged(int x, int y, int button){
! //
! Particle myParticle;
! float vx = ofRandom(-1, 1);
! float vy = ofRandom(-1, 1);
! myParticle.setInitialCondition(x, y, vx, vy);
! particles.push_back(myParticle);
}