Loading...
Logo
Processing Forum
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.

Copy code
  1. ArrayList<Atom> listA ;
  2. int count = 0 ;
  3. void setup ()
  4. {
  5.   size (400,400) ;
  6.   listA =            new ArrayList<Atom>();
  7. }
  8. void draw()
  9. {  
  10.   background(0) ;
  11.   for (Atom atm : listA)
  12.   {
  13.     atm.update(listA) ;
  14.     atm.display() ;
  15.     atm.drag2D() ;
  16.    
  17.     atm.collision(listA) ;
  18.    
  19.    
  20.    
  21.   }
  22. }
  23. void mousePressed()
  24.   PVector posA = new PVector ( mouseX, mouseY) ;
  25.   for (Atom atm : listA)
  26.   {
  27.     if(atm.insideAtom()) return ;
  28.   }
  29.   count += 1 ;
  30.   Atom atm = new Atom( posA, count) ;
  31.   listA.add(atm) ;
  32. }
  33. //////////CLASS ATOM//////////////////////////////////////////////////////////
  34. /////////////////////////////////////////////////////////////////////////////
  35. class Atom
  36. {
  37.   ArrayList<Atom> listA_collision ;
  38.   ArrayList<Atom> listA_noCollision ;
  39.  
  40.   boolean lock, inside, collision ;
  41.   int d = 50 ;
  42.   PVector pos ;    // position of the atom
  43.   PVector vel = new PVector ( random(-1, 1), random(-1, 1)) ; // velocity of the atom
  44.   color blanc = color (255) ;
  45.   color rouge = color (255, 35, 35) ;
  46.   color c = rouge ;
  47.   int count, numero, addCount ;
  48.  
  49.   Atom( PVector p_, int count_) {
  50.     pos = p_ ;
  51.     count = count_ ;
  52.     listA_collision = new ArrayList<Atom>();
  53.     listA_noCollision = new ArrayList<Atom>(); 
  54.   }
  55.   //::::::::::Display
  56.   void display()
  57.   {
  58.     numero = count + addCount  ;
  59.     fill(c) ;
  60.     ellipse(pos.x, pos.y, d, d) ;
  61.     fill(0) ;
  62.     text (numero, pos.x, pos.y ) ;
  63.   }
  64.   //:::::::Update
  65.   void update(ArrayList<Atom> listA)
  66.   {
  67.     int r = radius() ;
  68.     // wall right
  69.     if (pos.x > width -r || pos.x < r ) {
  70.     vel.x = -vel.x  ;        
  71.     } else if (pos.y > height -r || pos.y < r  ) {
  72.       vel.y = -vel.y ;    
  73.     }
  74.     pos.x +=vel.x ;
  75.     pos.y +=vel.y ;  
  76.   }
  77.   //::::::::::Collision
  78.   void collision(ArrayList<Atom> listA )
  79.   {
  80.     for (Atom target : listA) {
  81.       //listA_collision.clear() ;
  82.       if (target != this) {    // don't collide with ourselves. that would be weird.
  83.          if (!collide(target)) {
  84.           noContact(target) ;
  85.           listA_collision.add(target); // when a collision is found, add it to a list for later use.
  86.          // return ;
  87.         }
  88.         if (collide(target)) {
  89.           listA_collision.add(target); // when a collision is found, add it to a list for later use.
  90.           contact(target) ;
  91.           println("Target List :" + listA_collision.size()) ;
  92.           println("Atom List :" + listA.size()) ;
  93.         }
  94.       }
  95.     }
  96.     listA_collision.clear() ;
  97.   }
  98.   //::::::::Contact and NoContact
  99.   void contact (Atom target) 
  100.   {
  101.     addCount = target.count ;
  102.     color colorC = colorIn (target);
  103.   }
  104.   //______
  105.   void noContact (Atom target) 
  106.   {
  107.     addCount = 0 ;
  108.     color colorC = colorOut (target);
  109.   }
  110.  
  111.   //:::::::::DRAG
  112.   void drag2D() {
  113.     inside = detectionCursor2D() ;
  114.     if(mousePressed && inside) {
  115.       lock = true;
  116.     }
  117.     if(!mousePressed) {
  118.       lock = false;
  119.     }
  120.     if(lock)  {
  121.       pos.x = mouseX;
  122.       pos.y = mouseY;
  123.     }
  124.   }
  125. //:::::::::::::::::::RETURN:::::::::::::::::::::::::::::::::::::
  126.   //::::::::::::Color
  127.   color colorIn(Atom target)
  128.   {
  129.     color newColor = color (blanc) ;
  130.     c = newColor ;
  131.     return newColor ;
  132.   }
  133.   //::::::::::::::
  134.   color colorOut (Atom target)
  135.   {
  136.     color newColor = color (rouge, 175) ;
  137.     target.c = newColor ;
  138.     return newColor ;
  139.   }
  140.   //:::::::::::::Detection
  141.   // Detection the cursor is on the atom
  142.   boolean detectionCursor2D() { return dist(pos.x, pos.y, mouseX, mouseY) < d / 2; }
  143.  
  144.   //:::::::::detect a collision with the other proton
  145.   boolean collide(Atom target)
  146.   {
  147.     float distance = target.pos.dist(pos);      // distance between our center and the other ball center
  148.     float thresh = target.radius() + radius();
  149.     if (distance < thresh) {                   // if the distance is less than the threshold, we are colliding!
  150.       return true;
  151.     } else {
  152.       return false;
  153.     }
  154.   } 
  155.   //::::::::::::::::::::::::::::::::
  156.   boolean insideAtom() { if (inside) { return true ; } else { return false ; } }
  157.  
  158.   //::::::::::::::::::::Misc
  159.   int radius() { return d/2 ; }
  160. }

Replies(3)

Without diving too deep in your code: It seems that you check every object for collisions with every other object.
And if there is no contact with any of the other objects, you call noContact(). So the objects can only have the "collision-state" if they have collison with all other objects.

Suggestion:
– erase lines 84-88
– put "noContact(this)" into update()
– change the order in draw() to
Copy code
  1.  atm.update(listA) ;
     atm.collision(listA) ;
     atm.display() ;
     atm.drag2D() ;
I didn't test much, but it seems to work.
Whaou when it's simple it's awesome. The order solution is great, but I must keep the line 84-88, if I erase i don't have the reverse color.

Now, I must resolve the problem of addition of ball's number in contact, an other game !!!!

Thanks a lot for your new sight on my code.
The final code is here : http://www.openprocessing.org/sketch/68091
I add an adaptation of angle collision from Ira greenberg