예제 #1
0
 /**
  * Detect if two bodies are crashing into each other If they are, then create and return the
  * resultant body, if they aren't, then return a null value. *
  */
 private static Body crash(Body m1, Body m2) {
   // Distance formula to determine if two bodies are touching
   if (StrictMath.sqrt(
           StrictMath.pow((m2.getposx() - m1.getposx()), 2)
               + StrictMath.pow((m2.getposy() - m1.getposy()), 2))
       < (m1.getSize() + m2.getSize())) {
     int mass, size;
     double vX, vY, pX, pY;
     Color color;
     mass = (m1.getMass() + m2.getMass());
     size = ((m1.getSize() + m2.getSize()));
     color = combineColor(m1, m2);
     // Get the resultant velocities with the inelastic collisions equation:
     // V = (M1*V1 + M2*V2)/(M1+M2)
     vX =
         ((m1.getvelx() * m1.getMass() + m2.getvelx() * m2.getMass())
             / (m1.getMass() + m2.getMass()));
     vY =
         ((m1.getvely() * m1.getMass() + m2.getvely() * m2.getMass())
             / (m1.getMass() + m2.getMass()));
     // Whichever body is bigger absorbs the other body
     if (m1.getMass() > m2.getMass()) {
       pX = m1.getposx();
       pY = m1.getposy();
     } else {
       pX = m2.getposx();
       pY = m2.getposy();
     }
     return (new Body(mass, size, color, vX, vY, pX, pY));
   } else return null;
 }
예제 #2
0
 /** Change the position to the new position * */
 private static void posChanges(Body b) {
   b.setposx(b.getposx() + b.getvelx());
   b.setposy(b.getposy() + b.getvely());
 }
예제 #3
0
  private static double calculateGravity(Body m1, Body m2, boolean returnX) {
    /**
     * *** Newton's universal law of gravitation with the distance formula substituted for r^2 ***
     */
    double force =
        ((G * m1.getMass() * m2.getMass())
            / (StrictMath.pow((m2.getposx() - m1.getposx()), 2)
                + StrictMath.pow((m2.getposy() - m1.getposy()), 2)));

    double angle = Math.atan((m2.getposy() - m1.getposy()) / (m2.getposx() - m1.getposx()));
    if (returnX) // If we want the X component of the force.
    if (m2.getposx() < m1.getposx() && m2.getposy() > m1.getposy()
          || m2.getposx() < m1.getposx() && m2.getposy() < m1.getposy())
        // Flip the force to the correct direction.
        force = force * Math.cos(angle) * -1;
      else force = force * Math.cos(angle);
    else // Else, we want the Y component of the force.
    if (m2.getposx() < m1.getposx() && m2.getposy() > m1.getposy()
        || m2.getposx() < m1.getposx() && m2.getposy() < m1.getposy())
      // Flip the force to the correct direction.
      force = force * Math.sin(angle) * -1;
    else force = force * Math.sin(angle);
    return force;
  }