#include "Swarm.h"
swarm::swarm(){
light.setAmbientColor(ofColor(0, 0, 0));
}
void swarm::init(int nParticles, float positionDispersion, float velocityDispersion){
if(particles.size() != 0){
ofLogWarning("swarm") << "Swarm: Already initialised";
particles.clear();
}
ofSeedRandom();
glm::vec3 position, velocity;
ofColor color;
for(int i = 0; i < nParticles; i++){
position.x = (ofRandom(1.0f) - 0.5f) * positionDispersion;
position.y = (ofRandom(1.0f) - 0.5f) * positionDispersion;
position.z = (ofRandom(1.0f) - 0.5f) * positionDispersion;
velocity.x = (ofRandom(1.0f) - 0.5f) * velocityDispersion;
velocity.y = (ofRandom(1.0f) - 0.5f) * velocityDispersion;
velocity.z = (ofRandom(1.0f) - 0.5f) * velocityDispersion;
color.r = ofRandom(255.0f);
color.g = ofRandom(255.0f);
color.b = 150.0f;
color.a = 255.0f;
particle newParticle;
newParticle.position = position;
newParticle.velocity = velocity;
newParticle.color = color;
particles.push_back(newParticle);
}
}
void swarm::customDraw(){
update();
ofPushStyle();
light.enable();
light.setPosition(particles[0].position);
for(unsigned int i = 0; i < particles.size(); i++){
material.setDiffuseColor(particles[i].color);
material.begin();
ofDrawSphere(particles[i].position, 1.0);
}
material.end();
light.disable();
ofDisableLighting();
ofSetColor(255, 255, 255);
ofDrawSphere(light.getPosition(), 2.0);
ofSetDrawBitmapMode(OF_BITMAPMODE_MODEL);
ofDrawBitmapString(" light", particles[0].position);
ofPopStyle();
}
void swarm::update(){
float dt = ofGetLastFrameTime();
for(unsigned int i = 0; i < particles.size(); i++){
particles[i].position += particles[i].velocity * dt;
particles[i].velocity += -SPRING_CONSTANT * particles[i].position * dt;
particles[i].velocity += -SPRING_CONSTANT * (particles[i].position - light.getPosition()) * dt;
if(glm::length(particles[i].velocity) > MAX_VELOCITY){
particles[i].velocity /= glm::length(particles[i].velocity) * MAX_VELOCITY;
}
}
}
Comments