Package ucb.proj2

Class Physics

java.lang.Object
ucb.proj2.Physics

public class Physics extends Object
Methods for computing collision times and effects on round equal-sized moving objects in 2D. Don't change this class.

For the purposes of this package, we represent the state of an object as a four-element double[] containing [x, y, vx, vy]: the coordinates of the object's center, and the x- and y-components of its velocity.

The vector-manipulating routines add, len2, len, and ip operate on 2-element segments of arrays of doubles. To specify such a segment, you supply an array and the index of the first element of the segment. Thus, if A, V, and C are 4-element arrays, then add (C, 0, A, 0, t, V, 2) adds (A[0], A[1]) + t * (V[2], V[3]) and places the result in C[0], C[1]. In comments, we use the notation X[i:i+1] to refer to one of these two-element segments.

The computations are not exactly physical (especially when the objects interpenetrate slightly due to round-off), but they should do for this project.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    add(double[] Z, int iz, double[] X, int ix, double[] Y, int iy)
    Same as add (Z, IZ, 1.0, X, IX, 1.0, Y, IY).
    static double[]
    add(double[] Z, int iz, double[] X, int ix, double ay, double[] Y, int iy)
    Same as add (Z, IZ, 1.0, X, IX, AY, Y, IY).
    static double[]
    add(double[] X, int ix, double ay, double[] Y, int iy)
    Same as add (NZ, 0, 1.0, X, IX, AY, Y, IY), where NZ is a new double[2].
    static double[]
    add(double[] Z, int iz, double ax, double[] X, int ix, double ay, double[] Y, int iy)
    Set Z[IZ : IZ+1] to AX*X[IX : IX+1] + AY*Y[IY : IY+1], returning Z.
    static double[]
    add(double ax, double[] X, int ix, double ay, double[] Y, int iy)
    Same as add (NZ, 0, AX, X, IX, AY, Y, IY), where NZ is a new double[2].
    static double
    collide(double[] p0, double[] p1, double r)
    Given two objects with radius R with states P0 and P1 (see the class documentation for ucb.proj2.Physics, above), compute the time at which they next collide, assuming that initially, they are separated by a distance of >= 2R.
    static double
    ip(double[] X, int ix, double[] Y, int iy)
    The inner product X[IX] * Y[IY] + X[IX+1] * Y[IY+1].
    static double
    len(double[] X, int ix)
    The length of X[IX : IX+1]
    static double
    len(double[] X, int ix, double[] Y, int iy)
    The length of X[IX : IX+1] - Y[IY : IY+1].
    static double
    len2(double[] X, int ix)
    The squared length of X[IX : IX+1].
    static double
    len2(double[] X, int ix, double[] Y, int iy)
    The squared length of X[IX : IX+1] - Y[IY : IY+1].
    static void
    rebound(double[] p0, double[] p1)
    Given two objects with P0 and P1 (see the class documentation for ucb.proj2.Physics, above) that are in collision with each other, (destructively) modify their velocities as a result of that collision.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Physics

      public Physics()
  • Method Details

    • collide

      public static double collide(double[] p0, double[] p1, double r)
      Given two objects with radius R with states P0 and P1 (see the class documentation for ucb.proj2.Physics, above), compute the time at which they next collide, assuming that initially, they are separated by a distance of >= 2R. Objects that will not collide return a result of positive infinity (Double.POSITIVE_INFINITY). Objects whose centers are moving away from each others are taken not to collide.
    • rebound

      public static void rebound(double[] p0, double[] p1)
      Given two objects with P0 and P1 (see the class documentation for ucb.proj2.Physics, above) that are in collision with each other, (destructively) modify their velocities as a result of that collision. The objects are assumed to be spherical and of equal radius, the collision is assumed to be elastic (kinetic- energy preserving), and the material is assumed to be extremely stiff and slippery.
    • add

      public static double[] add(double[] Z, int iz, double ax, double[] X, int ix, double ay, double[] Y, int iy)
      Set Z[IZ : IZ+1] to AX*X[IX : IX+1] + AY*Y[IY : IY+1], returning Z.
    • add

      public static double[] add(double[] Z, int iz, double[] X, int ix, double[] Y, int iy)
      Same as add (Z, IZ, 1.0, X, IX, 1.0, Y, IY).
    • add

      public static double[] add(double[] Z, int iz, double[] X, int ix, double ay, double[] Y, int iy)
      Same as add (Z, IZ, 1.0, X, IX, AY, Y, IY).
    • add

      public static double[] add(double ax, double[] X, int ix, double ay, double[] Y, int iy)
      Same as add (NZ, 0, AX, X, IX, AY, Y, IY), where NZ is a new double[2].
    • add

      public static double[] add(double[] X, int ix, double ay, double[] Y, int iy)
      Same as add (NZ, 0, 1.0, X, IX, AY, Y, IY), where NZ is a new double[2].
    • ip

      public static double ip(double[] X, int ix, double[] Y, int iy)
      The inner product X[IX] * Y[IY] + X[IX+1] * Y[IY+1].
    • len2

      public static double len2(double[] X, int ix)
      The squared length of X[IX : IX+1].
    • len

      public static double len(double[] X, int ix)
      The length of X[IX : IX+1]
    • len2

      public static double len2(double[] X, int ix, double[] Y, int iy)
      The squared length of X[IX : IX+1] - Y[IY : IY+1].
    • len

      public static double len(double[] X, int ix, double[] Y, int iy)
      The length of X[IX : IX+1] - Y[IY : IY+1].