Пример #1
0
  private boolean collision(GravityObject GO) {
    for (int i = 0; i < gravityObjectArray.length; i++) {
      if (gravityObjectArray[i] != null) {
        if (GO != gravityObjectArray[i]) {
          double centerDistance =
              Math.sqrt(
                  (Math.pow((GO.xP - gravityObjectArray[i].xP), 2))
                      + (Math.pow((GO.yP - gravityObjectArray[i].yP), 2))); // <3 Pytagoras
          if (centerDistance < GO.size + gravityObjectArray[i].size) {

            if (GO.size > gravityObjectArray[i].size) {
              GO.eat(gravityObjectArray[i]);
            } else {
              gravityObjectArray[i].eat(GO);
            }
            if (GO.size < gravityObjectArray[i].size) {
              GO.remove();
            } else {
              gravityObjectArray[i].remove();
            }
            return true;
          }
        }
      } else {
        break;
      }
    }
    return false;
  }
Пример #2
0
  private void drawVector() {
    if (drawVector) {
      for (int i = 0; i < gravityObjectArray.length; i++) {
        if (gravityObjectArray[i] != null) {
          GravityObject[] GOA = gravityObjectArray;
          GravityObject GO = gravityObjectArray[i];

          for (int j = 0; j < GOA.length; j++) {
            if (GOA[j] != null
                && GOA[j].xP > 0
                && GOA[j].yP > 0
                && GOA[i].xP > 0
                && GOA[i].yP > 0) {
              double distance =
                  Math.sqrt(
                      (Math.pow((GOA[j].xP - GOA[i].xP), 2))
                          + (Math.pow((GOA[j].yP - GOA[i].yP), 2)));
              double gravityForce =
                  gravConstant * (GOA[i].getMass() * GOA[j].getMass()) / Math.pow(distance, 2);

              float[] gColAr = new float[3];
              gColAr[0] = (float) (gravityForce * 1.3);
              gColAr[1] = (float) (gravityForce * 1);
              gColAr[2] = (float) (gravityForce * 1);
              drawLine(
                  (int) (GOA[i].xP),
                  (int) (GOA[i].yP),
                  (int) (GOA[j].xP),
                  (int) (GOA[j].yP),
                  gColAr);
            }
          }
          float[] sColAr = new float[3];
          sColAr[0] = (float) (0.2);
          sColAr[1] = (float) (0.2);
          sColAr[2] = (float) (0);
          drawLine(
              ((int) GO.xP),
              ((int) GO.yP),
              ((int) (GO.xP + GO.xV * (25))),
              ((int) (GO.yP + GO.yV * (25))),
              sColAr);
        }
      }
    }
  }
Пример #3
0
  private boolean gravityObjectCollision(int x, int y, int size) {
    for (int i = 0; i < gravityObjectArray.length; i++) {
      if (gravityObjectArray[i] != null) {

        double centerDistance =
            Math.sqrt(
                (Math.pow((x - gravityObjectArray[i].xP), 2))
                    + (Math.pow((y - gravityObjectArray[i].yP), 2))); // <3 Pytagoras

        if (centerDistance <= size + gravityObjectArray[i].size) {
          return true;
        }
      } else {
        break;
      }
    }

    return false;
  }
Пример #4
0
  private void applyPhysics() {
    GravityObject[] GOA = gravityObjectArray;

    for (int i = 0; i < GOA.length; i++) {
      int c = 0;

      if (GOA[i] != null) {

        double[] xVelAr = new double[128];
        double[] yVelAr = new double[128];

        for (int j = 0; j < GOA.length; j++) {
          if (GOA[j] != null) {
            if (GOA[j].size != 0 && GOA[i].size != 0) {
              if (i != j) {
                double distance =
                    Math.sqrt(
                        (Math.pow((GOA[j].xP - GOA[i].xP), 2))
                            + (Math.pow((GOA[j].yP - GOA[i].yP), 2)));
                double gravityForce =
                    gravConstant * (GOA[i].getMass() * GOA[j].getMass()) / Math.pow(distance, 2);
                // System.out.println("Gravitational force between "+i+" and "+ j+" is "+
                // gravityForce);

                double xDistance = GOA[j].xP - GOA[i].xP;
                if (xDistance < 0) {
                  xDistance = GOA[i].xP - GOA[j].xP;
                }
                double yDistance = GOA[j].yP - GOA[i].yP;
                if (yDistance < 0) {
                  yDistance = GOA[i].yP - GOA[j].yP;
                }
                // System.out.println("X, Y Distance: "+xDistance+" & "+yDistance);
                double acceleration = gravityForce / GOA[i].getMass();
                // System.out.println("Acceleration of object "+i+" is equal to "+acceleration);

                // System.out.println("Object "+i+" moved towards "+j+" with a speed of:
                // "+acceleration);
                xVelAr[c] = (acceleration / (xDistance + yDistance)) * (GOA[j].xP - GOA[i].xP);
                // System.out.println("xVelAr["+j+"] = ("+acceleration+" / ("+xDistance+" +
                // "+yDistance+")) * ("+GOA[j].xP+" - "+GOA[i].xP+") En dat is: "+ xVelAr[j]);
                yVelAr[c] = (acceleration / (xDistance + yDistance)) * (GOA[j].yP - GOA[i].yP);

                c++; // C, C# etc...
              }
            }
          } else {
            break;
          }
        }

        for (int k = 0; k < xVelAr.length; k++) {
          double xTotalVel = 0;
          if (xVelAr[k] != 0) {
            xTotalVel += xVelAr[k];
          } else {
            break;
          }
          GOA[i].xV += xTotalVel;
        }

        for (int l = 0; l < xVelAr.length; l++) {
          double yTotalVel = 0;
          if (yVelAr[l] != 0) {
            yTotalVel += yVelAr[l];
          } else {
            break;
          }
          GOA[i].yV += yTotalVel;
        }

        if (!collision(GOA[i])) {
          GOA[i].move();
        } else {
          GOA[i].move();
          // colliding is handled in collision()
        }

      } else {
        break;
      }
    }

    // Delete out of bounds spheres
    for (int i = 0; i < gravityObjectArray.length; i++) {
      if (gravityObjectArray[i] != null) {
        if (gravityObjectArray[i].xP > screenWidth
            || gravityObjectArray[i].xP < 0
            || gravityObjectArray[i].yP > screenHeight
            || gravityObjectArray[i].yP < 0) {
          gravityObjectArray[i].remove();
        }
      }
    }
  }