• fullscreen
  • ParticleManager.pde
  • bouncy02.pde
  • particle.pde
  • class ParticleManager { 
    
      int numParticles = 5;  
      Particle[] particle = new Particle[numParticles];  
    
      void instantiatePrts() {  
        for(int i=0; i < numParticles; i++) {  
          particle[i] = new Particle(new Vector3D(0,0), new Vector3D(random(-2,2), random(-2,2)), new Vector3D(random(width), random(height)));  
        }
      }
    
      void setTouching() {
    
      }
    
      void collision() {
        for (int i=0; i < numParticles;i++) {
          for (int j=0; j<i; j++) {
            if (dist(particle[i].loc.x, particle[i].loc.y, particle[j].loc.x, particle[j].loc.y) <= (30))
            {
              // swap velocity
              Vector3D vi = new Vector3D();
              Vector3D vj = new Vector3D();
              
              vi = particle[i].vel;
              vj = particle[j].vel;
       
              particle[i].vel = vj;
              particle[j].vel = vi;
    
              particle[i].touch();
              particle[j].touch();
    
    
              //particle[j].vel.y *= -1;
    
    
            }
            else {
              particle[i].touching = false;
              particle[j].touching = false;
            }
          }
        }
      }
    
      void drawPrts() {  
        for(int i = 0; i < numParticles; i++) { 
          particle[i].go();
        }
      } 
    }
    
    import noc.*;  
      
    ParticleManager PM;  
    void setup() {  
      size(300, 300);  
      smooth(); 
      PM = new ParticleManager();  
      PM.instantiatePrts(); 
    }  
      
    void draw() {  
      background(255);  
      noStroke();  
      fill(0);
      PM.drawPrts();
      PM.collision();
    }  
    
    class Particle { 
      Vector3D loc; 
      Vector3D vel; 
      Vector3D acc;
      float radius = 30; 
      float gravity = 1.1;
      boolean touching = false;
       
      //The Constructor (called when the object is first created) 
      Particle(Vector3D a, Vector3D v, Vector3D l) { 
        acc = a.copy(); 
        vel = v.copy(); 
        loc = l.copy(); 
      } 
     
      //main function to operate object) 
      void go() { 
        update(); 
        borders(); 
        render();
        //gravity();
        
      } 
       
      //function to update location 
      void update() { 
        vel.add(acc); 
        loc.add(vel); 
      } 
       
      void borders() { 
        if (loc.y > height || loc.y <= 0)
        {
          vel.y *= -1;
        }
        if (loc.x > width || loc.x <= 0)
        {
          vel.x *= -1;
        }
      }
      
      void touch() {
        if(!touching) {
          //vel.x *= -1;
          //vel.y *= -1;
          touching = true;
        }
        
        
        
      }
      
      void gravity() {
        if (loc.y >= height) {
          acc.mult(gravity);
        }
      }
     
      //function to display 
      void render() { 
        //rectMode(CENTER); 
        noStroke(); 
        fill(200); 
        ellipse(loc.x,loc.y, radius, radius); 
      } 
     
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Cameron Cundiff

    Billiard Balls v1

    Add to Faves Me Likey@! 1
    You must login/register to add this sketch to your favorites.

    Billiard ball simulation with no friction. Certain tangential collisions don't behave properly.

    You need to login/register to comment.