Glitchy multiple object collision.
in
Programming Questions
•
2 years ago
I've sketched out the following, which is the beginning to something a bit bigger I'm working on, I'm happy with what I've got so far, but the object collision seams to have a glitch. The collision its self only works for 100% of all collisions when an even number of particles is on the page,(Even then left to run for a long time it will eventually develop a problem ), I think its caused by some sort of symmetry coming from the class, but I cant think how to rework it.
ArrayList particles;
float Dist;
int PVEL = 1;
boolean hover = true;
boolean lock = false;
float difx = 0.0;
float dify = 0.0;
float Dist;
int PVEL = 1;
boolean hover = true;
boolean lock = false;
float difx = 0.0;
float dify = 0.0;
void setup() {
size(800, 400, P2D);
ellipseMode(CENTER);
particles = new ArrayList();
smooth();
Dist = (50);
}
size(800, 400, P2D);
ellipseMode(CENTER);
particles = new ArrayList();
smooth();
Dist = (50);
}
void draw() {
background(255);
for (int i=0; i<particles.size(); i++) {
Particle p = (Particle) particles.get(i);
p.run();
//p.gravity();
p.edge();
p.Drag();
p.dump();
p.collide();
p.display();
}
for (int i=particles.size()-1; i>=0; i--) {
Particle p1 = (Particle) particles.get(i);
for (int j=0; j < particles.size(); j++) {
Particle p2 = (Particle) particles.get(j);
if (dist(p1.x, p1.y, p2.x, p2.y) < Dist) {
line(p1.x, p1.y, p2.x, p2.y);
if (mouseX > p1.x-10 && mouseX < p1.x+10 && mouseY > p1.y-10 && mouseY < p1.y+10 ) {
hover = true;
fill(120);
if(!lock);
}
else {
hover = false;
fill(0, 25);
}
ellipse(p1.x, p1.y, 20, 20);
}
}
}
}
background(255);
for (int i=0; i<particles.size(); i++) {
Particle p = (Particle) particles.get(i);
p.run();
//p.gravity();
p.edge();
p.Drag();
p.dump();
p.collide();
p.display();
}
for (int i=particles.size()-1; i>=0; i--) {
Particle p1 = (Particle) particles.get(i);
for (int j=0; j < particles.size(); j++) {
Particle p2 = (Particle) particles.get(j);
if (dist(p1.x, p1.y, p2.x, p2.y) < Dist) {
line(p1.x, p1.y, p2.x, p2.y);
if (mouseX > p1.x-10 && mouseX < p1.x+10 && mouseY > p1.y-10 && mouseY < p1.y+10 ) {
hover = true;
fill(120);
if(!lock);
}
else {
hover = false;
fill(0, 25);
}
ellipse(p1.x, p1.y, 20, 20);
}
}
}
}
void mousePressed() {
if (mouseButton == LEFT)
particles.add(new Particle(10));
}
if (mouseButton == LEFT)
particles.add(new Particle(10));
}
void keyPressed() {
switch(key) {
case '=':
Dist+=20;
break;
case '-':
Dist-=20;
break;
}
}
//////////////////////////////////// || CLASS
class Particle {
float x, y;
float r;
PVector vel;
float x, y;
float r;
PVector vel;
Particle(float tempR) {
r = tempR;
x = mouseX;
y = mouseY;
vel = new PVector(random(-PVEL,PVEL), random(-PVEL,PVEL));
}
r = tempR;
x = mouseX;
y = mouseY;
vel = new PVector(random(-PVEL,PVEL), random(-PVEL,PVEL));
}
void run() {
x = x + vel.x;
y = y + vel.y;
}
x = x + vel.x;
y = y + vel.y;
}
void gravity() {
vel.y += 0.5;
}
void collide() {
for(int i=0; i<particles.size();i++) {
Particle p1 = (Particle)particles.get(i);
for(int j=i+1;j<particles.size();j++) {
if(j!=i) {
Particle p2 = (Particle)particles.get(j);
float dx = p1.x - p2.x;
float dy = p1.y - p2.y;
float distance = (sqrt(dx*dx+dy*dy));
float minDist = r*2;
if(distance<minDist) {
ellipse(p1.x, p1.y, r*2, r*2);
ellipse(p2.x, p2.y, r*2, r*2);
println("collision!");
p1.vel.x *= -1;
p1.vel.y *= -1;
p2.vel.x *= -1;
p2.vel.y *= -1;
}
}
}
}
}
vel.y += 0.5;
}
void collide() {
for(int i=0; i<particles.size();i++) {
Particle p1 = (Particle)particles.get(i);
for(int j=i+1;j<particles.size();j++) {
if(j!=i) {
Particle p2 = (Particle)particles.get(j);
float dx = p1.x - p2.x;
float dy = p1.y - p2.y;
float distance = (sqrt(dx*dx+dy*dy));
float minDist = r*2;
if(distance<minDist) {
ellipse(p1.x, p1.y, r*2, r*2);
ellipse(p2.x, p2.y, r*2, r*2);
println("collision!");
p1.vel.x *= -1;
p1.vel.y *= -1;
p2.vel.x *= -1;
p2.vel.y *= -1;
}
}
}
}
}
void Drag() {
if (mouseButton == CENTER && mouseX > x-r-3 && mouseX < x+r-3 && mouseY > y-r-3 && mouseY < y+r-3 ) {
lock = true;
x = mouseX;
y = mouseY;
}
else {
if (mouseButton == LEFT && mouseX > x-r-3 && mouseX < x+r-3 && mouseY > y-r-3 && mouseY < y+r-3 ) {
lock = false;
}
}
}
if (mouseButton == CENTER && mouseX > x-r-3 && mouseX < x+r-3 && mouseY > y-r-3 && mouseY < y+r-3 ) {
lock = true;
x = mouseX;
y = mouseY;
}
else {
if (mouseButton == LEFT && mouseX > x-r-3 && mouseX < x+r-3 && mouseY > y-r-3 && mouseY < y+r-3 ) {
lock = false;
}
}
}
void dump() {
if (mouseButton == CENTER);
lock = false;
}
if (mouseButton == CENTER);
lock = false;
}
void edge() {
x += vel.x;
y += vel.y;
if (x>width || x<0) {
vel.x *= -1;
}
if (y>height || y<0) {
vel.y *= -1;
}
}
void display() {
fill (0, 55);
strokeWeight(0.5);
stroke(100);
ellipseMode(CENTER);
//ellipse(x, y, r*2, r*2);
}
}
fill (0, 55);
strokeWeight(0.5);
stroke(100);
ellipseMode(CENTER);
//ellipse(x, y, r*2, r*2);
}
}
1