Multi Collision with class and arrayList
in
Programming Questions
•
1 year ago
Hello Everybody,
I try to make a system to show the collision between particles, not the effect ; for that it's ok.
So in my example I want see particles white and add the number between them when there is collision. It's ok when there is two particles, but with more the last particle make law for the number, and for the color all particles must be together to became white. I walk around my code since few days but I don't find the other way of code.
So if anybody have an idea, i take it, i don't find track of my purpose in the forum.
PS : clic to create particle, you can drag this.
I try to make a system to show the collision between particles, not the effect ; for that it's ok.
So in my example I want see particles white and add the number between them when there is collision. It's ok when there is two particles, but with more the last particle make law for the number, and for the color all particles must be together to became white. I walk around my code since few days but I don't find the other way of code.
So if anybody have an idea, i take it, i don't find track of my purpose in the forum.
PS : clic to create particle, you can drag this.
- ArrayList<Atom> listA ;
- int count = 0 ;
- void setup ()
- {
- size (400,400) ;
- listA = new ArrayList<Atom>();
- }
- void draw()
- {
- background(0) ;
- for (Atom atm : listA)
- {
- atm.update(listA) ;
- atm.display() ;
- atm.drag2D() ;
- atm.collision(listA) ;
- }
- }
- void mousePressed()
- {
- PVector posA = new PVector ( mouseX, mouseY) ;
- for (Atom atm : listA)
- {
- if(atm.insideAtom()) return ;
- }
- count += 1 ;
- Atom atm = new Atom( posA, count) ;
- listA.add(atm) ;
- }
- //////////CLASS ATOM//////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- class Atom
- {
- ArrayList<Atom> listA_collision ;
- ArrayList<Atom> listA_noCollision ;
- boolean lock, inside, collision ;
- int d = 50 ;
- PVector pos ; // position of the atom
- PVector vel = new PVector ( random(-1, 1), random(-1, 1)) ; // velocity of the atom
- color blanc = color (255) ;
- color rouge = color (255, 35, 35) ;
- color c = rouge ;
- int count, numero, addCount ;
- Atom( PVector p_, int count_) {
- pos = p_ ;
- count = count_ ;
- listA_collision = new ArrayList<Atom>();
- listA_noCollision = new ArrayList<Atom>();
- }
- //::::::::::Display
- void display()
- {
- numero = count + addCount ;
- fill(c) ;
- ellipse(pos.x, pos.y, d, d) ;
- fill(0) ;
- text (numero, pos.x, pos.y ) ;
- }
- //:::::::Update
- void update(ArrayList<Atom> listA)
- {
- int r = radius() ;
- // wall right
- if (pos.x > width -r || pos.x < r ) {
- vel.x = -vel.x ;
- } else if (pos.y > height -r || pos.y < r ) {
- vel.y = -vel.y ;
- }
- pos.x +=vel.x ;
- pos.y +=vel.y ;
- }
- //::::::::::Collision
- void collision(ArrayList<Atom> listA )
- {
- for (Atom target : listA) {
- //listA_collision.clear() ;
- if (target != this) { // don't collide with ourselves. that would be weird.
- if (!collide(target)) {
- noContact(target) ;
- listA_collision.add(target); // when a collision is found, add it to a list for later use.
- // return ;
- }
- if (collide(target)) {
- listA_collision.add(target); // when a collision is found, add it to a list for later use.
- contact(target) ;
- println("Target List :" + listA_collision.size()) ;
- println("Atom List :" + listA.size()) ;
- }
- }
- }
- listA_collision.clear() ;
- }
- //::::::::Contact and NoContact
- void contact (Atom target)
- {
- addCount = target.count ;
- color colorC = colorIn (target);
- }
- //______
- void noContact (Atom target)
- {
- addCount = 0 ;
- color colorC = colorOut (target);
- }
- //:::::::::DRAG
- void drag2D() {
- inside = detectionCursor2D() ;
- if(mousePressed && inside) {
- lock = true;
- }
- if(!mousePressed) {
- lock = false;
- }
- if(lock) {
- pos.x = mouseX;
- pos.y = mouseY;
- }
- }
- //:::::::::::::::::::RETURN:::::::::::::::::::::::::::::::::::::
- //::::::::::::Color
- color colorIn(Atom target)
- {
- color newColor = color (blanc) ;
- c = newColor ;
- return newColor ;
- }
- //::::::::::::::
- color colorOut (Atom target)
- {
- color newColor = color (rouge, 175) ;
- target.c = newColor ;
- return newColor ;
- }
- //:::::::::::::Detection
- // Detection the cursor is on the atom
- boolean detectionCursor2D() { return dist(pos.x, pos.y, mouseX, mouseY) < d / 2; }
- //:::::::::detect a collision with the other proton
- boolean collide(Atom target)
- {
- float distance = target.pos.dist(pos); // distance between our center and the other ball center
- float thresh = target.radius() + radius();
- if (distance < thresh) { // if the distance is less than the threshold, we are colliding!
- return true;
- } else {
- return false;
- }
- }
- //::::::::::::::::::::::::::::::::
- boolean insideAtom() { if (inside) { return true ; } else { return false ; } }
- //::::::::::::::::::::Misc
- int radius() { return d/2 ; }
- }
1