topical media & game development

talk show tell print

#javascript-processing-example-topic-motion-bouncybubbles.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>BouncyBubbles</h2>
  
  <p>Based on code from Keith Peters (www.bit-101.com). 
  
  Multiple-object collision.</p>
  
  <p><a href="http://processing.org/learning/topics/bouncybubbles.html"><b>Original Processing.org Example:</b> BouncyBubbles</a><br>
  <script type="application/processing">
  int numBalls = 12;
  float spring = 0.05;
  float gravity = 0.03;
  Ball[] balls = new Ball[numBalls];
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    smooth();
    for (int i = 0; i < numBalls; i++) {
      balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
    }
  }
  
  void draw() 
  {
    background(0);
    for (int i = 0; i < numBalls; i++) {
      balls[i].collide();
      balls[i].move();
      balls[i].display();  
    }
  }
  
  class Ball {
    float x, y;
    float diameter;
    float vx = 0;
    float vy = 0;
    int id;
    Ball[] others;
   
    Ball(float xin, float yin, float din, int idin, Ball[] oin) {
      x = xin;
      y = yin;
      diameter = din;
      id = idin;
      others = oin;
    } 
    
    void collide() {
      for (int i = id + 1; i < numBalls; i++) {
        float dx = others[i].x - x;
        float dy = others[i].y - y;
        float distance = sqrt(dx*dx + dy*dy);
        float minDist = others[i].diameter/2 + diameter/2;
        if (distance < minDist) { 
          float angle = atan2(dy, dx);
          float targetX = x + cos(angle) * minDist;
          float targetY = y + sin(angle) * minDist;
          float ax = (targetX - others[i].x) * spring;
          float ay = (targetY - others[i].y) * spring;
          vx -= ax;
          vy -= ay;
          others[i].vx += ax;
          others[i].vy += ay;
        }
      }   
    }
    
    void move() {
      vy += gravity;
      x += vx;
      y += vy;
      if (x + diameter/2 > width) {
        x = width - diameter/2;
        vx += -0.9; 
      }
      else if (x - diameter/2 < 0) {
        x = diameter/2;
        vx *= -0.9;
      }
      if (y + diameter/2 > height) {
        y = height - diameter/2;
        vy *= -0.9; 
      } 
      else if (y - diameter/2 < 0) {
        y = diameter/2;
        vy *= -0.9;
      }
    }
    
    void display() {
      fill(255, 204);
      ellipse(x, y, diameter, diameter);
    }
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  int numBalls = 12;
  float spring = 0.05;
  float gravity = 0.03;
  Ball[] balls = new Ball[numBalls];
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    smooth();
    for (int i = 0; i &lt; numBalls; i++) {
      balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
    }
  }
  
  void draw() 
  {
    background(0);
    for (int i = 0; i &lt; numBalls; i++) {
      balls[i].collide();
      balls[i].move();
      balls[i].display();  
    }
  }
  
  class Ball {
    float x, y;
    float diameter;
    float vx = 0;
    float vy = 0;
    int id;
    Ball[] others;
   
    Ball(float xin, float yin, float din, int idin, Ball[] oin) {
      x = xin;
      y = yin;
      diameter = din;
      id = idin;
      others = oin;
    } 
    
    void collide() {
      for (int i = id + 1; i &lt; numBalls; i++) {
        float dx = others[i].x - x;
        float dy = others[i].y - y;
        float distance = sqrt(dx*dx + dy*dy);
        float minDist = others[i].diameter/2 + diameter/2;
        if (distance &lt; minDist) { 
          float angle = atan2(dy, dx);
          float targetX = x + cos(angle) * minDist;
          float targetY = y + sin(angle) * minDist;
          float ax = (targetX - others[i].x) * spring;
          float ay = (targetY - others[i].y) * spring;
          vx -= ax;
          vy -= ay;
          others[i].vx += ax;
          others[i].vy += ay;
        }
      }   
    }
    
    void move() {
      vy += gravity;
      x += vx;
      y += vy;
      if (x + diameter/2 &gt; width) {
        x = width - diameter/2;
        vx += -0.9; 
      }
      else if (x - diameter/2 &lt; 0) {
        x = diameter/2;
        vx *= -0.9;
      }
      if (y + diameter/2 &gt; height) {
        y = height - diameter/2;
        vy *= -0.9; 
      } 
      else if (y - diameter/2 &lt; 0) {
        y = diameter/2;
        vy *= -0.9;
      }
    }
    
    void display() {
      fill(255, 204);
      ellipse(x, y, diameter, diameter);
    }
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.