How to add image to particle system


PImage fondo;  
Globos[] globos = new Globos[0];
int cantGlobos =400;

void setup () {
size (900, 606);
noStroke();
smooth();
fondo=loadImage ("fondoperdiste.jpg");

}
void draw() {
  
background(fondo);
 
globos = (Globos[])append(globos, new Globos(0, 0));
 
for (int i=0; i<globos.length; i++) {
  
globos[i].x += globos[i].Yvel;



ellipse(globos[i].y, globos[i].x, globos[i].BALLONtam, globos[i].BALLONtam);

fill (random(231,76 ), random(0,244), random(0,255));


  }
} 

class Globos {

float x;
float y;

float Xvel;
float Yvel;

float BALLONtam;

Globos(float Xpos, float Ypos) {

y = Ypos = random(0,900);

Yvel = random (0.10, 0.40);

BALLONtam = random (0.9, 3.5);

}
}

I want to replace the ellipse with a png image, but don’t know-how

That part where you use ellipse, replace the word with image, and insert another argument, the image you want to show, as the first one. That‘s gonna do it.

PImage img;

img = loadImage("Cats.png");

image(img, x, y, targetWidth, targetHeight);

These are all the things you‘ll need, but they have to be in different parts! Like global variable, load img in setup and image() replaces ellipse.

1 Like

thank you:raised_hands:

2 Likes

You should load the image once in the constructor and not throughout. Because that’s very expensive on the processor when you load again and again

So move the loadImage to the constructor and make img a variable at class level.

1 Like

@Chrisir I think you might‘ve misunderstood what i meant, and i‘ll tag @dawn too, just in case.

These are the lines you need, but not in the same part of the Code!

What i understood, is that you want to have 1 image instead of all the ellipses (like an emoji instead of ellipses, or so).

You have the PImage img as a global variable and load this image in setup! Then you draw the image with the second line image(img,x,y,w,h) instead of using ellipse(x, y, w, h)!

I‘ll edit the post by making img a global variable, just to avoid confusions :sweat_smile: .

3 Likes

Thank you for that.

I know that you didn’t mean that but we often see it with beginners that they use loadImage in draw()

1 Like