import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; import netscape.javascript.*; import javax.comm.*; import javax.sound.midi.*; import javax.sound.midi.spi.*; import javax.sound.sampled.*; import javax.sound.sampled.spi.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.sax.*; import javax.xml.transform.stream.*; import org.xml.sax.*; import org.xml.sax.ext.*; import org.xml.sax.helpers.*; public class TugOWar extends BApplet {PSystem ps;
Arcball arcball;

class RectParticle extends Particle {
public RectParticle() {}
  void draw() {
    push();
    stroke(100, 100, 100, 100);
    translate(pos[0], pos[1], pos[2]);
    if (fixed()) {
      fill(0xffFF0000);
    }
    else {
      fill(0xffDDDDDD);
    }
    box(5, 5, 5);
    pop();
  }
}

int PARTICLES_Z = 5;
int PARTICLES_Y = 4;
int PARTICLES_X = 10;

float unitLength = 30;

Particle[][][] particles;

void setup() {
  size(500, 500);
  ps = (PSystem)loadPlugin("PSystem");
  arcball = (Arcball)loadPlugin("Arcball");
  ps.setGravity(-0.3f);
  ps.defaultSpringRestLength = unitLength;
  ps.defaultSpringStrength = 0.5f;
  
  
  makeGrid();

  particles[0][PARTICLES_Y-1][0].fix();
  particles[PARTICLES_X-1][PARTICLES_Y-1][0].fix();
  particles[0][PARTICLES_Y-1][PARTICLES_Z-1].fix();
  particles[PARTICLES_X-1][PARTICLES_Y-1][PARTICLES_Z-1].fix();
}

void loop() {
  translate(width/4, height/2);
  background(0xff99CCCC);
  arcball.run();
  ps.draw();
}

void makeGrid() {
  particles = new Particle[PARTICLES_X][PARTICLES_Y][PARTICLES_Z];

  for (int x = 0; x < PARTICLES_X; x++) {
    for (int z = 0; z < PARTICLES_Z; z++) {
      for (int y = 0; y < PARTICLES_Y; y++) {
        particles[x][y][z] = new RectParticle();
        ps.addParticle(particles[x][y][z]);
        particles[x][y][z].setPos(unitLength*x,unitLength*y,unitLength*z);

        // Don't add springs if x == 3
        if (x != 3) {

          if (x > 0) {
            ps.addForce(new Spring(particles[x][y][z], particles[x-1][y][z]));
          }
          if (y > 0) {
            ps.addForce(new Spring(particles[x][y][z], particles[x][y-1][z]));
          }
          if (z > 0) {
            ps.addForce(new Spring(particles[x][y][z], particles[x][y][z-1]));
          }
        }
      }
    }
  }
  
  // Just add one spring for x == 3
  ps.addForce(new Spring(particles[3][2][2], particles[2][2][2]));
  
}
}