示例#1
1
    public void applyForce() {
      // force is proportional to the diff between restLen and current idst

      // a vector from A --> B
      Vector diff = new Vector(endA.pos, endB.pos);

      // compute the current distance
      float dist = diff.getMagnitude();
      // compute dx, which is what the force depends on
      float dx = Math.abs(dist - restLen);

      // a vector containing just the direction component of A --> B
      Vector dir = diff.copy().normalize();

      Vector force = dir.copy().scale(-K * dx, -K * dx);

      if (restLen < getDistance()) {
        // forces go INWARDS

        endB.addForce(force);
        endA.addForce(force.reverse());
      } else {
        // forces go OUTWARDS

        endA.addForce(force);
        endB.addForce(force.reverse());
      }
    }
 private void drawShooter() {
   stroke(255);
   double x2 =
       width / 2
           - 75
               * (Math.cos(radians((float) angledegrees))); // calculates point to draw line from
   double y2 =
       75
           * (Math.sin(
               radians(
                   (float)
                       angledegrees))); // calculates y co-ord to draw line from using trig
                                        // fucntions
   line(
       width / 2,
       height - (15 * height / 480),
       (float) x2,
       height - (15 * height / 480) - (float) y2); // draws line
   if (angledegrees == 170
       || angledegrees == 10) { // stop ball being shot horizontally. HAHA WE DIDN'T FORGET!!!
     increase = increase * -1; // sets shooter to rotate in opposite direction
   }
   if (canfire) { // checks if can fire
     angledegrees +=
         increase; // increases only when true. This stops the animation whil ball is moving
   }
 }
 public void run() {
   while (running) {
     if (!paused) {
       if (mode == 0) {
         for (double i = 0; i < 3.14f; i = i + 0.1f) {
           for (int j = 0; j < 12; j++) {
             double osc = Math.sin(i) * 127;
             int value = (int) osc;
             MIDILight(j, value);
           }
           delay(30);
         }
         for (double i = 3.14f; i >= 0; i = i - 0.1f) {
           for (int j = 0; j < 12; j++) {
             double osc = Math.sin(i) * 127;
             int value = (int) osc;
             MIDILight(j, value);
           }
           delay(30);
         }
       } else if (mode == 1) {
         for (int j = 0; j < 12; j++) {
           MIDILight(j, 127);
         }
         mode = 1000;
       } else if (mode == 2) {
         for (int j = 0; j < 12; j++) {
           MIDILight(j, 0);
         }
         mode = 1000;
       } else if (mode == 3) {
         int controller = (int) random(12);
         int randomValue = (int) random(127);
         MIDILight(controller, randomValue);
         delay(30);
       } else if (mode == 4) {
         for (int i = 0; i < 64; i++) {
           for (int j = 0; j < 12; j++) {
             MIDILight(j, i * 2);
           }
           delay(20);
         }
         for (int i = 63; i >= 0; i--) {
           for (int j = 0; j < 12; j++) {
             MIDILight(j, i * 2);
           }
           delay(20);
         }
       } else {
         delay(10);
         // FIX THIS. WITHOUT THIS THE CPU USE GOES THROUGH THE ROOF. NEED A WAY
         // TO SLEEP THIS THREAD WHEN IT'S NOT IN USE.
       }
     }
   }
   System.out.println(id + " thread is done!");
 }
 public boolean isPrime(double dNum) {
   for (int i = 2; i <= Math.sqrt(dNum); i++) {
     if (dNum % i == 0) {
       return false;
     }
   }
   return true;
 }