Hi, I’m new at OF and I’ve some issues with the movement in a particle system with mouse attractor, and can’t see why in my code the new position doesn’t update at every loop.
Could anyone help me?
Thanks!!
indent preformatted text by 4 spaces
#include "ofMain.h"
#include "ofApp.h"
//MAIN CLASS
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
//ofApp.h
#pragma once
#include "ofMain.h"
#include "System.h"
#include "Particles.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);
System *pSystem;
};
//ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
pSystem = new System;
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
pSystem->run();
}
//--------------------------------------------------------------
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){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
//particles.h
//
// Particles.h
// Atraction_Particles
//
// Created by Sergi Armengol Suquet on 2/10/15.
//
//
#pragma once
#include "ofMain.h"
class Particles {
public:
Particles();
~Particles();
void setup(float _x, float _y, float _minAcc, float _maxAcc,
float _minMax, float _maxMax,int _minSz, int _maxSz);
void update();
void draw();
void run();
void addForce(ofVec2f f);
void calcularAcercamiento();
void mouseMoved(ofMouseEventArgs & args);
void mouseDragged(ofMouseEventArgs & args);
void mousePressed(ofMouseEventArgs & args);
void mouseReleased(ofMouseEventArgs & args);
private:
ofVec2f pos,vel,acc;
float velAcc,maxVel;
int sz;
float mouseX,mouseY;
};
//particles.cpp
//
// Particles.cpp
// Atraction_Particles
//
// Created by Sergi Armengol Suquet on 2/10/15.
//
//
#include "Particles.h"
Particles::Particles(){
}
Particles::~Particles(){
}
void Particles::setup(float _x, float _y, float _minAcc, float _maxAcc,
float _minMax, float _maxMax,int _minSz, int _maxSz){
pos = ofVec2f(_x,_y);
vel = ofVec2f(0,0);
acc = ofVec2f(0,0);
velAcc = ofRandom(_minAcc,_maxAcc);
maxVel = ofRandom(_minMax, _maxMax);
sz = ofRandom(_minSz, _maxSz);
ofRegisterMouseEvents(this);
}
void Particles::update(){
vel += acc;
vel.limit(maxVel);
pos +=(vel);
acc *=0;
}
void Particles::draw(){
ofVec2f dist = ofVec2f(mouseX-pos.x,mouseY-pos.y);
float ang = atan2(dist.y,dist.x);
cout << pos << endl;
ofPushMatrix();
ofTranslate(pos.x+ofRandom(ofGetWidth()), pos.y);
ofRotate(ang);
ofFill();
ofTriangle(0, 0, -sz, sz/4, -sz, -sz/4);
ofPopMatrix();
}
void Particles::run() {
update();
draw();
}
void Particles::calcularAcercamiento() {
ofVec2f mouse = ofVec2f(mouseX,mouseY);
ofVec2f dir = mouse/pos;
dir.normalized();
dir.x *=velAcc;
dir.y *=velAcc;
addForce(dir);
}
void Particles::addForce(ofVec2f f){
acc += f*sz;
}
void Particles::mouseMoved(ofMouseEventArgs & args){
mouseX = args.x;
mouseY = args.y;
}
void Particles::mouseDragged(ofMouseEventArgs & args){
}
void Particles::mousePressed(ofMouseEventArgs & args){
}
void Particles::mouseReleased(ofMouseEventArgs & args){
}
// System.h
//
// System.h
// Atraction_Particles
//
// Created by Sergi Armengol Suquet on 2/10/15.
//
//
#pragma once
#include "ofMain.h"
#include "Particles.h"
class System {
public:
System();
~System();
void update();
void draw();
void addParticles(float posX,float posY);
void run();
private:
int totalCosas;
vector<Particles> listaParticles;
};
//System.cpp
//
// System.cpp
// Atraction_Particles
//
// Created by Sergi Armengol Suquet on 2/10/15.
//
//
#include "System.h"
System:: System() {
totalCosas = 10;
for ( int i = 0; i< totalCosas;i++){
listaParticles[i].setup(ofRandom(ofGetWidth()),ofRandom(ofGetHeight()),100,100,30,80,20,12);
}
}
System::~System(){
}
void System::update() {
for ( int i = 0; i < listaParticles.size();i++) {
listaParticles[i].update();
}
}
void System:: draw() {
for ( int i = 0; i < listaParticles.size();i++) {
listaParticles[i].draw();
}
}
void System::addParticles(float posX, float posY){
}
void System::run(){
update();
draw();
}
```