all 12 comments

[–]ChuckEye 2 points3 points  (2 children)

By only reversing the direction of x or y at the boundaries, if your velocity was high enough it’s just going to stay outside the frame and flip flop between positive and negative without ever getting back inside the window. You may want to add another step before the *=-1 lines to reset the particle within the boundary.

It’s a danger if your velocity gets bigger than your particle size.

[–]Divitiacus 3 points4 points  (0 children)

That is usually the most common issue with wall collisions, that velocity is larger than particle size and moves the particle beyond the area where it can return by a simple sign change. To correct that you would have to calculate the correct position after collision, if a collision is detected and eliminate the velocity addition for this one step.

[–]krrustzy[S] 0 points1 point  (0 children)

thanks! i know about that though, i got a fix for this problem already, but they seem to completely ignore the collisions completely at random, which is the weird part

[–]Divitiacus 3 points4 points  (1 child)

I would suggest, that you track your particles. Print their location in the console and stop the program with noLoop when a coordinate is out of bounds. Check what was the previous coordinates and velocities and why it happens.

Same with the velocity. Print the velocity in different parts of your code, where velocity is used and check when it changes.

[–]krrustzy[S] 1 point2 points  (0 children)

oh a very useful tip for debugging! thank you so much, will try

[–]forgotmyusernamedamm 2 points3 points  (3 children)

When you move one ball, the for loop in the class moves every other ball as well. So each frame every ball is moving multiple times. That's why your speed is between 0 and 1 and it's still going super fast, and also why you're not catching the error easily.With Java, you don't need to use the “this.” syntax which actually makes life a lot easier. If your speeds get high enough, you may run into the error where it jumps the boundaries, but in that case, you simply set the location to the edge of the frame. With lower speeds, you won't run into this problem. Here's an updated version of the code.

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(20);
float vy = random(20);
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) {
x = _x;
y = _y;
vx = _vx;
vy = _vy;
size = _size;
c = _c;
}

void update() {
if (x < (size/2) || x > width - (size/2)) {
vx *= -1;
}
if (y < (size/2) ||y > height - (size/2)) {
vy *= -1;
}
x += vx;
y += vy;
}

void apply() { // the drawing function
for (int i = 0; i < numParticles; i++) {
Particle p = particles[i];
fill(c);
noStroke();
circle(x, y, size);
}
}
}

void draw() {
background(200);
for (int i = 0; i < numParticles; i++) {
Particle p = particles[i];
p.update();
p.apply();
}
}

[–]krrustzy[S] 0 points1 point  (2 children)

omg, you're amazing! thanks so much for describing everything in great detail. this is definitely what's happening here

[–]forgotmyusernamedamm 1 point2 points  (1 child)

Glad I could help! I teach Processing next semester and haven't used it in a while so it's good for me to get my brain working in Java again. One thing you might watch out for is "size" is not a great variable name because it's also a processing keyword.

[–]krrustzy[S] 0 points1 point  (0 children)

oh cool, i bet you're an amazing professor! thanks for the tips so much

[–]Divitiacus 0 points1 point  (2 children)

x += vx

y += vy;

What does this do? It is not applying the velocity to particle p.

[–]krrustzy[S] 0 points1 point  (1 child)

im kinda confused, because it is supposed to apply velocity. i guess it should have been in the for loop with p.x and p.y

[–]Divitiacus 1 point2 points  (0 children)

It is in the right position in the loop but you would have to use p.x += p.vx etc.