예제 #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
 static void drawTorus(float r, float R, int nsides, int rings) {
   float ringDelta = 2.0f * (float) Math.PI / rings;
   float sideDelta = 2.0f * (float) Math.PI / nsides;
   float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
   for (int i = rings - 1; i >= 0; i--) {
     float theta1 = theta + ringDelta;
     float cosTheta1 = (float) Math.cos(theta1);
     float sinTheta1 = (float) Math.sin(theta1);
     GL11.glBegin(GL11.GL_QUAD_STRIP);
     float phi = 0.0f;
     for (int j = nsides; j >= 0; j--) {
       phi += sideDelta;
       float cosPhi = (float) Math.cos(phi);
       float sinPhi = (float) Math.sin(phi);
       float dist = R + r * cosPhi;
       GL11.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
       GL11.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
       GL11.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
       GL11.glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
     }
     GL11.glEnd();
     theta = theta1;
     cosTheta = cosTheta1;
     sinTheta = sinTheta1;
   }
 }
예제 #3
0
  private float[] randomColorArray() {
    float[] array = new float[3];

    array[0] = (float) Math.random();
    array[1] = (float) Math.random();
    array[2] = (float) Math.random();

    return array;
  }
예제 #4
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);
        }
      }
    }
  }
예제 #5
0
  private void drawCircle(double x, double y, int radius, float color[], float alpha) {
    GL11.glColor4f(color[0], color[1], color[2], alpha);

    float incr = (float) (2 * Math.PI / slices);
    /*xCoord = xCoord + radius;
    yCoord = yCoord + radius;*/

    GL11.glBegin(GL11.GL_TRIANGLE_FAN);
    for (int i = 0; i < slices; i++) {
      float angle = incr * i;

      float Xc = (float) (x + Math.cos(angle) * radius);
      float Yc = (float) (y + Math.sin(angle) * radius);

      GL11.glVertex2f(Xc, Yc);
    }
    GL11.glEnd();
  }
예제 #6
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;
  }
예제 #7
0
 public RenderEngine() {
   glEnable(GL_DEPTH_TEST);
   projection = new Matrix4().initPerspective((float) Math.toRadians(90), 16f / 9f, 0.001f, 1000);
 }
예제 #8
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();
        }
      }
    }
  }