[SOLVED] Converting Processing Particles array code to OF

Hi,

I’ve been porting some code from Processing but i can’t seem to find the equivalent.

Any help/suggestion will be greatly appreciated. :slight_smile:

This is the Processing code in bold (Everything else is ok, that’s the only thing i’m stuck with):

int numParticles=200;
particle[] Stars = new particle[numParticles];

void setup() {
size(640, 480,P2D);

** for (int i=0;i<numParticles;i++) {
Stars[i] = new particle();
}**
}

When porting from processing I found very usefull the vectors for dealing with arrays. I think they aren´t as fast as regular arrays but they are easy to use.

at the .h

  
#include <vector>  
  
class CLASSNAME{  
public:  
        vector<particle>	particles;  
}  
  

at the .cpp

for adding;

  
particle p = particle();  
particles.push_back(p);  

for itineration:

  
for (int i = 0; i < particles.size(); i++)   
     particles[i].draw();  

for erasing:

  
particles.erase(particles.begin()+i)  

I have one particle system example on my blog if you want:
http://www.patriciogonzalezvivo.com/blog/?p=5

else Chris Surge have good one that use regular arrays

http://csugrue.com/code/

I hope this help

Thank you very much, i’m gonna check and try this way.

This is the particles code i’ve been inspired to make mine.

http://www.coolbubble.com/programs/glitter.htm

I’ve adapted it all to oF, but i just need to figure how to setup the array. :frowning:

int numParticles=200;
particle[] Stars = new particle[numParticles];

void setup() {
size(640, 480,P2D);

** for (int i=0;i<numParticles;i++) {
Stars[i] = new particle();
}**
}

Can you explain me how to convert these two to oF?

I’ve managed to port it, but now i get a single error when building with xCode.

/Users/MP/app/src/Particle.cpp:20:0 /Users/MP/app/src/Particle.cpp:20:0 error: no match for ‘operator=’ in ‘((particle*)this)->particle::Stars. std::vector<_Tp, _Alloc>::operator[] with--Tp-=-particle,--Alloc-=-std::allocator = (((particle*)operator new(76u)), (->particle::particle(), ))’

Anyone knows how to fix this?

Particle.h

  
  
#ifndef _PARTICLE  
#define _PARTICLE  
  
#include "ofMain.h"  
#include "ofAddons.h"  
#include <vector>  
  
class particle {  
vector<particle>   Stars;  
};  
  
#endif  
  

Particle.cpp

  
#include "Particle.h"  
#include "testApp.h"  
  
void particle::setup(){  
	numParticles=200;  
	  
	for (int i=0;i<numParticles;i++)  
	{  
		Stars[i] = new particle();  // < This is where i get the error  
	}  
	  
}  

I’ve finally got it to work!

Thank you patricio :slight_smile:

Particle.h

  
#ifndef _PARTICLE  
#define _PARTICLE  
  
#include "ofMain.h"  
#include "ofAddons.h"  
#include <vector>  
  
class particle {  
vector<particle>   Stars;  
};  
  
#endif  
  

Particle.cpp

  
#include "Particle.h"  
#include "testApp.h"  
  
void particle::setup(){  
   numParticles=200;  
     
   for (int i=0;i<numParticles;i++)  
   {  
     particle p = particle();  
     Stars.push_back(p);  
   }     
}