How can i add images to this code please? (Particle System)


#1

Hello everyone. I would like tome convert those particles into an image. I mean every single particle in one image that i have from a picture, any. i leave here the code till i tried it. THANKS for your time!!

//This is a paddle i have for the main code
ParticleSystem ps;

void setup() {
size(800, 600);

ps = new ParticleSystem();
}

void draw() {
background(255, 255);

ps.addParticle();
ps.run();
}

//This is the second paddle named Particles
class Particle {
PVector location, velocity, acceleration;
float lifespan;
float radio;

PImage pocoyo;

Particle() {
location = new PVector(width/2, 20);
velocity = new PVector(0, 0);
acceleration = new PVector(random(-0.01, 0.01), random(0.01, 0.1));

pocoyo = loadImage("pocoyo.jpg");

lifespan = 255;

radio = random(12);

}

boolean isDead() {
if (lifespan <= 0) {
return true;
} else {
return false;
}
}

void falling() {
location.add(velocity);
velocity.add(acceleration);

if (location.y >= width/2) {
  lifespan = lifespan - 10;
}

}

void display() {
image(pocoyo, 0, 0, 20, 20);
fill(150, lifespan);
stroke(0);
strokeWeight(2);
ellipse(location.x, location.y, radio, radio);

}
}

//This is the Third and the last paddle named ParticleSystem
class ParticleSystem{
ArrayList particles;

ParticleSystem(){
particles = new ArrayList();

}

void addParticle(){
particles.add(new Particle());
}

void run(){
for(int i = 0; i < particles.size(); i++){
Particle p = particles.get(i);
p.display();
p.falling();

  if(p.isDead()){
    particles.remove(i);
  }
  
  
}

}

}


#2

Hey, please format your code by pressing Ctrl+T in the Processing IDE code editor and use the <\> button in the message editor.

What is wrong with the code you posted below?


#3

I wanna replace those particles by images on my code. I don’t have that knowledge yet, i’m trying out in different ways but i didn’t get it yet. I am asking if somebody could help me with it, pleas. Thanks


#4

I think it’s because you have to make your image a global variable to all your Particles (and call image(my_image,0,0) (inside of the display function for example). Here, you are just calling loadImage(...) and then you don’t store your image in a variable.