Successfully reported this slideshow.
Upcoming SlideShare
×

# openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII

14,313 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII

1. 1. ‣ ‣‣ ‣ ‣ ‣ ‣ ‣ ‣
2. 2. ‣‣‣
3. 3. ‣‣‣ http://goo.gl/zyfQR (Google Docs )
4. 4. ‣‣
5. 5. ‣‣‣void testApp::mouseMoved(int x, int y ){! prevMouseX = x;! prevMouseY = y;}void testApp::mouseDragged(int x, int y, int button){! //1! float dx = x - prevMouseX;! float dy = y - prevMouseY;!! //! particle myParticle;! myParticle.setInitialCondition(x,y, dx*0.5,dy*0.5);! //! particles.push_back(myParticle);!! //! prevMouseX = x;! prevMouseY = y;!}
6. 6.
7. 7.
8. 8. ‣‣‣
9. 9. ‣‣‣ F -F
10. 10. ‣‣void particle::bounceOffWalls(){! //! float minx = 0;! float miny = 0;! float maxx = ofGetWidth();! float maxy = ofGetHeight();! //! if (pos.x > maxx){! ! pos.x = maxx;! ! vel.x *= -1;! } else if (pos.x < minx){! ! pos.x = minx;! ! vel.x *= -1;! }! //! if (pos.y > maxy){! ! pos.y = maxy;! ! vel.y *= -1;! } else if (pos.y < miny){! ! pos.y = miny;! ! vel.y *= -1;! }}
11. 11.
12. 12. ‣‣‣ F = -kx‣ k F = -kx ma = -kx x = -sin k t m x = Csin(ωt + θ x
13. 13. ‣‣ springForce = -stiffness * stretch‣ = - *‣‣ springForce = -stiffness * (position - restPosition)‣
14. 14. ‣‣ springForce = stiffness * (restPositon - position)‣‣ velocity = dumping * (velocity + springFroce)
15. 15. ‣‣‣‣
16. 16. ‣‣‣void spring::update(){! if ((particleA == NULL) || (particleB == NULL)){! ! return;! }!! ofVec2f pta = particleA->pos;! ofVec2f ptb = particleB->pos;!! float theirDistance = (pta - ptb).length();! float springForce = (springiness * (distance - theirDistance));! ofVec2f frcToAdd = (pta-ptb).normalized() * springForce;!! particleA->addForce(frcToAdd.x, frcToAdd.y);! particleB->addForce(-frcToAdd.x, -frcToAdd.y);}
17. 17. ‣‣void testApp::setup(){!!! ofBackground(0,0,0);!! ofSetVerticalSync(true);! ofSetFrameRate(60);! //Particle A! particle_a.setInitialCondition(400, 400, 0, 0);!! //Particle B! particle_b.setInitialCondition(500, 500, 0, 0);! // (spring)! mySpring.distance = 100; //! mySpring.springiness = 0.1f; //! mySpring.particleA = &particle_a;! mySpring.particleB = &particle_b;}
18. 18. ‣‣void testApp::update(){!! //! particle_a.resetForce();! particle_b.resetForce();!! //! mySpring.update();!! // ( )! particle_a.bounceOffWalls();! particle_a.update();!! particle_b.bounceOffWalls();! particle_b.update();}
19. 19. ‣‣void testApp::draw(){! ofSetColor(255, 255, 255);!! //! mySpring.draw();!! //particle! particle_a.draw();! particle_b.draw();}
20. 20. ‣‣
21. 21. ‣‣‣
22. 22. ‣‣void testApp::setup(){!! ofBackground(0,0,0);! ofSetVerticalSync(true);! ofSetFrameRate(60);! ofSetBackgroundAuto(false);! pimg.loadImage("particle32.png");! //particle! for (int i = 0; i < 20; i++){! ! particle myParticle;! ! myParticle.setInitialCondition(ofRandom(500,550),ofRandom(500,550),0,0);! ! particles.push_back(myParticle);! }!! // spring! for (int i = 0; i < (particles.size()-1); i++){! ! spring mySpring;! ! mySpring.distance = 25;! ! mySpring.springiness = 0.2f;! ! mySpring.particleA = & (particles[i]);! ! mySpring.particleB = & (particles[(i+1)%particles.size()]);! ! springs.push_back(mySpring);! }}
23. 23. ‣‣void testApp::update(){! // particle! for (int i = 0; i < particles.size(); i++){! ! particles[i].resetForce();! }! //! for (int i = 0; i < springs.size(); i++){! ! springs[i].update();! }!! // particle! for (int i = 0; i < particles.size(); i++){! ! particles[i].addDampingForce();! ! particles[i].bounceOffWalls();! ! particles[i].update();! }}
24. 24. ‣‣void testApp::draw(){! //! ofSetColor(255, 255, 255, 127);! for (int i = 0; i < springs.size(); i++){! ! springs[i].draw();! }!! // particle! ofSetColor(255, 255, 255);! for (int i = 0; i < particles.size(); i++){! ! particles[i].draw();! }!}
25. 25.
26. 26.
27. 27.
28. 28. ‣‣‣ ‣ ‣‣
29. 29. ‣‣
30. 30. ‣‣‣‣ ‣ ‣ ‣‣‣
31. 31. ‣int fieldWidthint fieldHeightint fieldSizeint externalWidthint externalHeightvector field
32. 32. ‣vectorField();virtual ~vectorField();void setupField(int innerW, int innerH,int outerW, int outerH);void clear();void fadeField(float fadeAmount);void randomizeField(float scale);void draw();ofVec2f getForceFromPos(float xpos, floatypos);
34. 34. ‣‣ ‣ ‣ ‣ ‣‣
35. 35. ‣‣oid testApp::setup(){!!! ofSetVerticalSync(true);! ofSetFrameRate(60);! ofBackground(0, 0, 0);!! // ( 102 76 )! VF.setupField(102, 76, ofGetWidth(), ofGetHeight());! //! addMode = 1;}
36. 36. ‣‣void testApp::draw(){! //! ofEnableBlendMode(OF_BLENDMODE_ADD);! ofSetColor(0,130,130, 200);! VF.draw();! ofDisableBlendMode();!}
37. 37.
38. 38. ‣‣‣
39. 39. ‣‣void testApp::setup(){!!! ofSetVerticalSync(true);! ofSetFrameRate(60);! ofBackground(0, 0, 0);!! // 10000! for (int i = 0; i < 10000; i++){! ! particle myParticle;! ! myParticle.setInitialCondition! ! (ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight()),0,0);! ! particles.push_back(myParticle);! }!! // ( 102 76 )! VF.setupField(102, 76, ofGetWidth(), ofGetHeight());!! //! addMode = 2;}
40. 40. ‣‣void testApp::update(){!! for (int i = 0; i < particles.size(); i++){! !! ! //particle! ! particles[i].resetForce();! !! ! // particle! ! ofVec2f frc;! ! frc = VF.getForceFromPos(particles[i].pos.x, particles[i].pos.y);! !! ! //Particle! ! particles[i].addForce(frc.x, frc.y);! ! particles[i].addDampingForce();! ! particles[i].bounceOffWalls();! ! particles[i].update();! }!! //! VF.fadeField(0.998f);}
41. 41. ‣‣void testApp::draw(){!! ofEnableBlendMode(OF_BLENDMODE_ADD);!! //! ofSetColor(0,130,130, 127);! VF.draw();!! // particle! ofSetColor(0, 127, 255)! ;! for (int i = 0; i < particles.size(); i++){! ! particles[i].draw();! }!!! ofDisableBlendMode();!}
42. 42.
43. 43. ‣‣
44. 44. ‣‣void testApp::mouseDragged(int x, int y, int button){!! float diffx = x - prevMouseX;! float diffy = y - prevMouseY;!! VF.addVectorCircle((float)x, (float)y, diffx*0.3, diffy*0.3, 60, 0.3f);!! prevMouseX = x;! prevMouseY = y;}
45. 45.
46. 46. ‣‣‣ http://www.msavisuals.com/msaﬂuid
47. 47. ‣‣‣‣‣‣ http://www.red3d.com/cwr/boids/
48. 48. ‣ ‣ ‣ ‣ ‣ ‣ ‣
49. 49. ‣‣‣
50. 50. ‣void particle::addFlockingForce(){! // seperation! if(seperation.count > 0){! ! seperation.sum /= (float)seperation.count;! ! float sepFrc ! = seperation.strength;! ! frc -= (seperation.sum.normalized() * sepFrc);! }!! // alignment! if(alignment.count > 0){! ! alignment.sum /= (float)alignment.count;! ! float alignFrc ! = alignment.strength;! ! frc += (alignment.sum! ! * alignFrc);! }!! // cohesion! if(cohesion.count > 0){! ! cohesion.sum /= (float)cohesion.count;! ! cohesion.sum -= pos;! ! float cohFrc ! = cohesion.strength;! ! frc += (cohesion.sum.normalized() * cohFrc);! }!}
51. 51. ‣void particle::addForFlocking(particle &p){!! ofVec3f diff, diffNormalized;! float distance;!! diff! ! ! = p.pos - pos;! distance! ! = diff.length();! diffNormalized! = diff;! diffNormalized.normalize();! if( distance > 0 && distance < seperation.distance ){! ! seperation.sum += diffNormalized;! ! seperation.count++;! }!! if( distance > 0 && distance < alignment.distance ){! ! alignment.sum += p.vel.getNormalized();! ! alignment.count++;! }!! if( distance > 0 && distance < cohesion.distance ){! ! cohesion.sum += p.pos;! ! cohesion.count++;! }}
52. 52.