Boid
提供:kuhalaboWiki
- Bird.h
#pragma once #include "ofMain.h" class Bird { public: Bird(vector<Bird>* const, const int, ofColor, vector<Bird>* const, vector<Bird>* const, vector<Bird>* const); virtual ~Bird(); virtual void update(); virtual void draw(); int id; float head; float tail; ofVec2f pos; ofVec2f vel; ofVec2f acc; ofColor ourColor; protected: vector<Bird>* pSelfGroup; vector<Bird>* pEnemyGroup; vector<Bird>* pFriendGroup; vector<Bird>* pFriendGroup2; ofVec2f separationVel; ofVec2f alignmentVel; ofVec2f cohesionVel; ofVec2f groupCenterPos; };
- Bird.cpp
#include "Bird.h" Bird::Bird( vector<Bird>* const _pSelfGroup, const int _id, const ofColor _ourColor, vector<Bird>* const _pEnemyGroup, vector<Bird>* const _pFriendGroup, vector<Bird>* const _pFriendGroup2 ) { pSelfGroup = _pSelfGroup; pEnemyGroup = _pEnemyGroup; pFriendGroup = _pFriendGroup; pFriendGroup2 = _pFriendGroup2; id = _id; ourColor = _ourColor; pos.set( ofRandom(0,ofGetWidth() ) , ofRandom(0,ofGetHeight() ) ); vel.set( ofRandom(-2,2), ofRandom(-2,2) ); head = 2; tail = head * 4.0; } Bird::~Bird() { } void Bird::update() { int separationCount = 0; int alignmentCount = 0; int cohesionCount = 0; separationVel.set(0,0); alignmentVel.set(0,0); cohesionVel.set(0,0); groupCenterPos.set(0,0); for(int i = 0; i < pSelfGroup->size(); i++){ Bird& rAnother = pSelfGroup->at(i); if(rAnother.id != id){ float dist = pos.distance( rAnother.pos ); if(dist < 10){ separationVel += pos - rAnother.pos; separationCount++; } else if(dist < 35){ alignmentVel += rAnother.vel.getNormalized(); alignmentCount++; } else if(dist < 60){ groupCenterPos += rAnother.pos; cohesionCount++; } } } for(int i = 0; i < pFriendGroup->size(); i++){ Bird& rAnother = pFriendGroup->at(i); float dist = pos.distance( rAnother.pos ); if(dist < 10){ separationVel += pos - rAnother.pos; separationCount++; } else if(dist < 35){ alignmentVel += rAnother.vel.getNormalized(); alignmentCount++; } else if(dist < 60){ groupCenterPos += rAnother.pos; cohesionCount++; } } for(int i = 0; i < pFriendGroup2->size(); i++){ Bird& rAnother = pFriendGroup2->at(i); float dist = pos.distance( rAnother.pos ); if(dist < 10){ separationVel += pos - rAnother.pos; separationCount++; } else if(dist < 35){ alignmentVel += rAnother.vel.getNormalized(); alignmentCount++; } else if(dist < 60){ groupCenterPos += rAnother.pos; cohesionCount++; } } for(int i = 0; i < pEnemyGroup->size(); i++){ Bird& rAnother = pEnemyGroup->at(i); float dist = pos.distance( rAnother.pos ); if(dist < 60){ separationVel += pos - rAnother.pos; separationCount++; } } separationVel /= separationCount; separationVel.normalize(); alignmentVel /= alignmentCount; alignmentVel.normalize(); groupCenterPos /= cohesionCount; cohesionVel = groupCenterPos - pos; cohesionVel.normalize(); vel += (separationVel*0.15) + (alignmentVel*0.035) + (cohesionVel*0.02); vel.scale(1.5); pos += vel; if( pos.x < 0 ){ vel.x *= -1; } else if( pos.x >= ofGetWidth() ){ vel.x *= -1; } if( pos.y < 0 ){ vel.y *= -1; } else if( pos.y >= ofGetHeight() ){ vel.y *= -1; } } void Bird::draw() { ofSetColor(ourColor); ofCircle(pos, head); ofLine(pos.x + vel.x * tail * 0.5, pos.y + vel.y * tail * 0.5, pos.x - vel.x * tail, pos.y - vel.y * tail); }
- ofApp.h
#pragma once #include "ofMain.h" #include "ofGraphics.h" #include "Bird.h" class ofApp : public ofBaseApp { 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(int x, int y, int button); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); vector<Bird> birds1; vector<Bird> birds2; vector<Bird> birds3; vector<Bird> birds4; vector<Bird> nullBirds; bool active; };
- ofApp.cpp
#include "ofApp.h" #define BOID1 100 #define BOID2 80 #define BOID3 120 #define BOID4 50 #define BGCOLOR ofColor(0,0,0,32) // black #define COLOR1 ofColor(255,0,0,255) // red #define COLOR2 ofColor(0,255,0,255) // green #define COLOR3 ofColor(0,255,255,255) //cyan #define COLOR4 ofColor(255,255,0,255) // yellow #define FRATE 20 //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(FRATE); ofEnableAlphaBlending(); ofSetBackgroundColor(0,0,0); ofSetBackgroundAuto(false); ofSetCircleResolution(64); ofHideCursor(); ofSetBackgroundColor(BGCOLOR); active = true; // self, enemy, friend1, friend2 birds1.reserve(BOID1); for(int i = 0; i < BOID1; i++){ birds1.push_back(*new Bird( &birds1, i, COLOR1, &birds2, &birds3, &nullBirds)); } birds2.reserve(BOID2); for(int i = 0; i < BOID2; i++){ birds2.push_back(*new Bird( &birds2, i, COLOR2, &nullBirds, &birds3, &nullBirds)); } birds3.reserve(BOID3); for(int i = 0; i < BOID3; i++){ birds3.push_back(*new Bird( &nullBirds, i, COLOR3, &nullBirds, &birds2, &nullBirds)); } birds4.reserve(BOID4); for(int i = 0; i < BOID4; i++){ birds4.push_back(*new Bird( &birds4, i, COLOR4, &birds1, &birds3, &nullBirds)); } } //-------------------------------------------------------------- void ofApp::update() { if(active){ for(int i = 0; i < birds1.size(); i++){ birds1[i].update(); } for(int i = 0; i < birds2.size(); i++){ birds2[i].update(); } for(int i = 0; i < birds3.size(); i++){ birds3[i].update(); } for(int i = 0; i < birds4.size(); i++){ birds4[i].update(); } } } //-------------------------------------------------------------- void ofApp::draw() { ofSetColor(BGCOLOR); ofRect(0,0, ofGetWidth(),ofGetHeight() ); for(int i = 0; i < birds1.size(); i++){ birds1[i].draw(); } for(int i = 0; i < birds2.size(); i++){ birds2[i].draw(); } for(int i = 0; i < birds3.size(); i++){ birds3[i].draw(); } for(int i = 0; i < birds4.size(); i++){ birds4[i].draw(); } } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ switch (key){ case ' ': active = !active; break; case 'f': ofToggleFullscreen(); break; case 's': ofShowCursor(); break; case 'h': ofHideCursor(); break; default: break; } }