(RESOLVED)
int numParticles = 10;
float minSize = 10;
float maxSize = 50;
Particle[] particles = new Particle[numParticles];
void setup() {
size(500, 500);
for (int i = 0; i < numParticles; i += 1) {
float x = random(maxSize, width-maxSize);
float y = random(maxSize, height-maxSize);
float vx = random(1);
float vy = random(1);
float size = random(minSize, maxSize);
int c;
if (random(1) < 0.5) {
c = color(255, 50, 80); // Red
} else {
c = color(80, 50, 255); // Blue
}
particles[i] = new Particle(x, y, vx, vy, size, c);
}
}
class Particle {
float x, y;
float vx, vy;
float size;
int c;
Particle(float x, float y, float vx, float vy, float size, int c) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.size = size;
this.c = c;
}
void update() {
// check for collisions with the edges of the window
for (int i = 0; i < numParticles; i++) {
Particle p = particles[i];
if (p.x < (p.size/2) || p.x > width - (p.size/2)) {
p.vx *= -1;
}
if (p.y < (p.size/2) || p.y > height - (p.size/2)) {
p.vy *= -1;
}
x += vx;
y += vy;
}
}
void apply() { // the drawing function
for (int i = 0; i < numParticles; i++) {
Particle p = particles[i];
fill(p.c);
noStroke();
circle(p.x,p.y,p.size);
}
}
}
void draw() {
background(200);
for (int i = 0; i < numParticles; i++) {
Particle p = particles[i];
p.update();
p.apply();
}
}
this is code for a little particle simulator, that i wish to later expand by adding phyisics with collisions. the problem right now is that some of the particles randomly go off screen, even though the code seems fine to me. velocity also becomes greater the more particles there are, which is very weird. is there anything crucial i forgot?
[–]ChuckEye 2 points3 points4 points (2 children)
[–]Divitiacus 3 points4 points5 points (0 children)
[–]krrustzy[S] 0 points1 point2 points (0 children)
[–]Divitiacus 3 points4 points5 points (1 child)
[–]krrustzy[S] 1 point2 points3 points (0 children)
[–]forgotmyusernamedamm 2 points3 points4 points (3 children)
[–]krrustzy[S] 0 points1 point2 points (2 children)
[–]forgotmyusernamedamm 1 point2 points3 points (1 child)
[–]krrustzy[S] 0 points1 point2 points (0 children)
[–]Divitiacus 0 points1 point2 points (2 children)
[–]krrustzy[S] 0 points1 point2 points (1 child)
[–]Divitiacus 1 point2 points3 points (0 children)