physics
Class Geometry

java.lang.Object
  extended by physics.Geometry

public class Geometry
extends java.lang.Object

The Geometry library contains procedural abstractions which are useful in modeling the physical interactions between objects.

The library is described in terms of these concepts:

The intended use of the Geometry library is as follows:

  1. The client calls the timeUntilCollision() methods to calculate the times at which the ball(s) will collide with each of the bouncers or with another ball. The minimum of all these times (call it "mintime") is the time of the next collision.
  2. The client updates the position of the ball(s) and the bouncers to account for mintime passing. At this point, the ball and the object it is about to hit are exactly adjacent to one another.
  3. The client calls the appropriate reflect() method to calculate the change in the ball's velocity.
  4. The client updates the ball's velocity and repeats back to step 1.

The timeUntilCollision() methods assume constant ball velocity. That is, no force will be acting on the ball, so it will follow a straight-line path. Therefore, if external forces (such as gravity or friction) need to be accounted for, the client must do so before or after the of the "time until / update position / reflect" series of steps - never inbetween those three steps.

Important note: The methods which deal with line segment bouncers do NOT deal with the end-points of the segment. To ensure realistic behavior, shapes should be constructed from a combination of line segments with zero-radius circles at the end points.

For example: A ball is located at (0,0) and is moving in the (1,1) direction towards two line segments; one segments spans the points (1,1),(1,2) and the other spans (1,1),(2,1). The ball will hit the ends of both line segments at a 45 degree angle and something REALLY WEIRD will happen. However, if a circle with zero radius is placed at (1,1) then the ball will bounce off the circle in the expected manner.


Nested Class Summary
static class Geometry.DoublePair
          DoublePair is a simple immutable record type representing a pair of doubles.
static class Geometry.VectPair
          VectPair is a simple immutable record type representing a pair of Vects.
 
Field Summary
static Geometry.DoublePair DOUBLE_PAIR_NAN
          DoublePair with both d1 and d2 set to Double.NaN
 
Method Summary
static Vect applyReflectionCoeff(Vect incidentVect, Vect reflectedVect, double rCoeff)
          Accounts for the effects of inelastic collisions given the intial and resulting velocities of the collision assuming elasticity.
static double distanceSquared(double x1, double y1, double x2, double y2)
           
static double distanceSquared(Vect v1, Vect v2)
           
static double minQuadraticSolution(double a, double b, double c)
          Solves the quadratic equation.
static Vect perpendicularPoint(LineSegment line, Vect point)
          Returns the point on line which forms a line with point that is perpendicular to line.
static Vect perpendicularPointWholeLine(LineSegment line, Vect point)
          Returns the point on the infinitly long line represented by line which forms a line with point that is perpendicular to line.
static Geometry.DoublePair quadraticSolution(double a, double b, double c)
          Solves the quadratic equation.
static Geometry.VectPair reflectBalls(Vect center1, double mass1, Vect velocity1, Vect center2, double mass2, Vect velocity2)
          Computes the resulting velocities of two balls which collide.
static Vect reflectCircle(Vect circle, Vect ball, Vect velocity)
          Computes the new velocity of a ball reflecting off of a circle.
static Vect reflectCircle(Vect circle, Vect ball, Vect velocity, double reflectionCoeff)
          Computes the new velocity of a ball reflecting off of a circle.
static Vect reflectRotatingCircle(Circle circle, Vect center, double angularVelocity, Circle ball, Vect velocity)
          Computes the new velocity of a ball reflected off of a rotating circle.
static Vect reflectRotatingCircle(Circle circle, Vect center, double angularVelocity, Circle ball, Vect velocity, double reflectionCoeff)
          Computes the new velocity of a ball reflected off of a rotating circle.
static Vect reflectRotatingWall(LineSegment line, Vect center, double angularVelocity, Circle ball, Vect velocity)
          Computes the new velocity of a ball reflecting off of a wall which is rotating about a point with constant angular velocity.
static Vect reflectRotatingWall(LineSegment line, Vect center, double angularVelocity, Circle ball, Vect velocity, double reflectionCoeff)
          Computes the new velocity of a ball reflecting off of a wall which is rotating about a point with constant angular velocity.
static Vect reflectWall(LineSegment line, Vect velocity)
          Computes the new velocity of a ball after bouncing (reflecting) off a wall.
static Vect reflectWall(LineSegment line, Vect velocity, double reflectionCoeff)
          Computes the new velocity of a ball after bouncing (reflecting) off a wall.
static Circle rotateAround(Circle circle, Vect cor, Angle a)
          Rotates the circle represented by circle by a around the center of rotation, cor, and returns the result.
static LineSegment rotateAround(LineSegment line, Vect cor, Angle a)
          Rotates the line segment represented by line by a around the center of rotation, cor, and returns the result.
static Vect rotateAround(Vect point, Vect cor, Angle a)
          Rotates the point represented by p by a around the center of rotation, cor, and returns the result.
static void setForesight(double maximumForesight)
          Modifies the behavior of this class to use the specified maximumForesight.
static void setGeometry(GeometryInterface impl)
           
static void setTuningParameters(double maximumForesight, boolean useDoughnut, int numberOfSlices)
          Modifies the behavior of this class to use the specified maximumForesight and numberOfSlices.
static double timeUntilBallBallCollision(Circle ball1, Vect vel1, Circle ball2, Vect vel2)
          Computes the time until two balls collide.
static double timeUntilCircleCollision(Circle circle, Circle ball, Vect velocity)
          Computes the time until a ball represented by a circle, travelling at a specified velocity collides with a specified circle.
static Geometry.DoublePair timeUntilCircleCollision(Circle circle, Vect point, Vect velocity)
          Computes the times when the point moving along the given trajectory will intersect the given circle
static double timeUntilRotatingCircleCollision(Circle circle, Vect center, double angularVelocity, Circle ball, Vect velocity)
          Computes the time until a ball travelling at a specified velocity collides with a rotating circle.
static double timeUntilRotatingWallCollision(LineSegment line, Vect center, double angularVelocity, Circle ball, Vect velocity)
          Computes the time until a ball travelling at a specified velocity collides with a rotating line segment.
static double timeUntilWallCollision(LineSegment line, Circle ball, Vect velocity)
          Computes the time until a ball, represented by a circle, travelling at a specified velocity collides with a specified line segment.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DOUBLE_PAIR_NAN

public static final Geometry.DoublePair DOUBLE_PAIR_NAN
DoublePair with both d1 and d2 set to Double.NaN

See Also:
Double.NaN
Method Detail

setGeometry

public static void setGeometry(GeometryInterface impl)
Parameters:
impl - the object to be used as the singleton
See Also:
setForesight(double), setTuningParameters(double, boolean, int)

setTuningParameters

public static void setTuningParameters(double maximumForesight,
                                       boolean useDoughnut,
                                       int numberOfSlices)
Modifies the behavior of this class to use the specified maximumForesight and numberOfSlices. If useDoughnut is true then doughnut optimizations are enabled. The values used by default are <+Inf, true, 15>. Many uses may prefer to simply use setForesight instead.

Parameters:
maximumForesight - The maximal time in the future that a collision will be searched for. Collisions may still be returned that happen farther than maximumForesight in the future, but no extra effort will be made to find them. If set to +Infinity, useDoughnut must also be true.
useDoughnut - When true, the timeUntilRotating* methods perform extra calculations to reduce the time during which collisions are searched for. If maximumForesight is small, it is sometimes quicker to skip these additional checks. Must be true if maximumForesight is +Infinity.
numberOfSlices - The number of slices that the time being searched for a possible collision is divided into. Since some methods (noteably timeUntilRotating*) cannot use closed form formula, they must search for possible collisions over some time frame. Increasing the size of this will decrease the likelihood of one of the timeUntilRotating* methods missing a collision, but will also cause them to run slower.
See Also:
setForesight(double), Double.POSITIVE_INFINITY

setForesight

public static void setForesight(double maximumForesight)
Modifies the behavior of this class to use the specified maximumForesight.

Parameters:
maximumForesight - The maximal time in the future that a collision will be searched for. Collisions may still be returned that happen farther than maximumForesight in the future, but no extra effort will be made to find them.
See Also:
Double.POSITIVE_INFINITY

quadraticSolution

public static Geometry.DoublePair quadraticSolution(double a,
                                                    double b,
                                                    double c)
Solves the quadratic equation.

Returns:
a pair containing the roots of the equation a*x2 + b*x + c = 0 with the lesser of the two roots in result.d1. If no real roots exist, the returned pair will contain NaN for both values.
See Also:
Double.NaN

minQuadraticSolution

public static double minQuadraticSolution(double a,
                                          double b,
                                          double c)
Solves the quadratic equation.

Returns:
the lesser of the two roots of the quadratic equation specified by a*x2 + b*x + c = 0, or NaN if no real roots exist.
See Also:
Double.NaN

perpendicularPoint

public static Vect perpendicularPoint(LineSegment line,
                                      Vect point)
Returns the point on line which forms a line with point that is perpendicular to line.

Returns:
the point on line which forms a line with point that is perpendicular to line, or null if no such point exists within the given line segment.
See Also:
perpendicularPointWholeLine(LineSegment, Vect)

perpendicularPointWholeLine

public static Vect perpendicularPointWholeLine(LineSegment line,
                                               Vect point)
Returns the point on the infinitly long line represented by line which forms a line with point that is perpendicular to line.

Returns:
the point on the infinitly long line represented by line which forms a line with point that is perpendicular to line, or null if no such point exists within the given line segment.
See Also:
perpendicularPoint(LineSegment, Vect)

applyReflectionCoeff

public static Vect applyReflectionCoeff(Vect incidentVect,
                                        Vect reflectedVect,
                                        double rCoeff)
Accounts for the effects of inelastic collisions given the intial and resulting velocities of the collision assuming elasticity.

Parameters:
incidentVect - the intial velocity of the ball
reflectedVect - the resulting velocity after the collision assuming elasticity.
rCoeff - the reflection coefficent.
Returns:
the resulting velocity after an inelastic collision.

timeUntilWallCollision

public static double timeUntilWallCollision(LineSegment line,
                                            Circle ball,
                                            Vect velocity)
Computes the time until a ball, represented by a circle, travelling at a specified velocity collides with a specified line segment.

Parameters:
line - the line segment representing a wall or (part of) an object that might be collided with
ball - a circle indicate the size and location of a ball which might collide with the given line segment
velocity - the velocity of the ball before impact
Returns:
the time until collision, or POSITIVE_INFINITY if the collision will not occur
See Also:
Double.POSITIVE_INFINITY, endpoint effects

reflectWall

public static Vect reflectWall(LineSegment line,
                               Vect velocity,
                               double reflectionCoeff)
Computes the new velocity of a ball after bouncing (reflecting) off a wall.

Parameters:
line - the line segment representing the wall which is being hit
velocity - the velocity of the ball before impact
reflectionCoeff - the reflection coefficent
Returns:
the velocity of the ball after impacting the given wall

reflectWall

public static Vect reflectWall(LineSegment line,
                               Vect velocity)
Computes the new velocity of a ball after bouncing (reflecting) off a wall.

Parameters:
line - the line segment representing the wall which is being hit
velocity - the velocity of the ball before impact
Returns:
the velocity of the ball after impacting the given wall

distanceSquared

public static double distanceSquared(Vect v1,
                                     Vect v2)
Returns:
the square of the distance between two points represented by v1 and v2.

distanceSquared

public static double distanceSquared(double x1,
                                     double y1,
                                     double x2,
                                     double y2)
Returns:
the square of the distance between two points represented by (x1, y1) and (x2, y2).

timeUntilCircleCollision

public static double timeUntilCircleCollision(Circle circle,
                                              Circle ball,
                                              Vect velocity)
Computes the time until a ball represented by a circle, travelling at a specified velocity collides with a specified circle.

Parameters:
circle - a circle representing the circle with which the ball may collide
ball - a circle representing the size and initial location of the ball
velocity - the velocity of the ball before impact
Returns:
the time until collision or POSITIVE_INFINITY if the collision will not occur
See Also:
Double.POSITIVE_INFINITY

reflectCircle

public static Vect reflectCircle(Vect circle,
                                 Vect ball,
                                 Vect velocity,
                                 double reflectionCoeff)
Computes the new velocity of a ball reflecting off of a circle.

Parameters:
circle - the center point of the circle which is being hit
ball - the center point of the ball
velocity - the velocity of the ball before impact
reflectionCoeff - the reflection coefficient
Returns:
the velocity of the ball after impacting the given circle

reflectCircle

public static Vect reflectCircle(Vect circle,
                                 Vect ball,
                                 Vect velocity)
Computes the new velocity of a ball reflecting off of a circle.

Parameters:
circle - the center point of the circle which is being hit
ball - the center point of the ball
velocity - the velocity of the ball before impact
Returns:
the velocity of the ball after impacting the given circle

rotateAround

public static Vect rotateAround(Vect point,
                                Vect cor,
                                Angle a)
Rotates the point represented by p by a around the center of rotation, cor, and returns the result.

Parameters:
point - the initial location of the point to be rotated
cor - the point indicating the center of rotation
a - the amount by which to rotate point
Returns:
point point rotated around cor by a

rotateAround

public static LineSegment rotateAround(LineSegment line,
                                       Vect cor,
                                       Angle a)
Rotates the line segment represented by line by a around the center of rotation, cor, and returns the result.

Parameters:
line - the initial location of the line segment to be rotated
cor - the point indicating the center of rotation
a - the amount by which to rotate point
Returns:
line segment line rotated around cor by a

rotateAround

public static Circle rotateAround(Circle circle,
                                  Vect cor,
                                  Angle a)
Rotates the circle represented by circle by a around the center of rotation, cor, and returns the result.

Parameters:
circle - the initial location of the circle to be rotated
cor - the point indicating the center of rotation
a - the amount by which to rotate point
Returns:
circle circle rotated around cor by a

timeUntilCircleCollision

public static Geometry.DoublePair timeUntilCircleCollision(Circle circle,
                                                           Vect point,
                                                           Vect velocity)
Computes the times when the point moving along the given trajectory will intersect the given circle

Parameters:
circle - circle to find collisions with
point - initial position of the point
velocity - linear velocity of the point
Returns:
the times until intersection, with lesser result in d1, or +Infs if no collisions will occur
See Also:
Double.POSITIVE_INFINITY

timeUntilRotatingWallCollision

public static double timeUntilRotatingWallCollision(LineSegment line,
                                                    Vect center,
                                                    double angularVelocity,
                                                    Circle ball,
                                                    Vect velocity)
Computes the time until a ball travelling at a specified velocity collides with a rotating line segment.

Parameters:
line - the initial position of the rotating line segment (wall)
center - the center of rotation for line
angularVelocity - the angular velocity of the rotation of line in radians per second. A positive angular velocity denotes a rotation in the direction from the positive x-axis to the positive y-axis.
ball - the size and initial location of the ball
velocity - the initial velocity of the ball. The ball is assumed to travel at a constant velocity until impact.
Returns:
the time until collision or POSITIVE_INFINITY if no collision was detected.
See Also:
Double.POSITIVE_INFINITY, endpoint effects

reflectRotatingWall

public static Vect reflectRotatingWall(LineSegment line,
                                       Vect center,
                                       double angularVelocity,
                                       Circle ball,
                                       Vect velocity)
Computes the new velocity of a ball reflecting off of a wall which is rotating about a point with constant angular velocity.

Parameters:
line - the line segment representing the initial position of the rotating wall
center - the point about which line rotates
angularVelocity - the angular velocity at which line rotates, in radians per second. A positive angular velocity denotes a rotation in the direction from the positive x-axis to the positive y-axis.
velocity - the velocity of the ball before impact
Returns:
the velocity of the ball after impacting the wall

reflectRotatingWall

public static Vect reflectRotatingWall(LineSegment line,
                                       Vect center,
                                       double angularVelocity,
                                       Circle ball,
                                       Vect velocity,
                                       double reflectionCoeff)
Computes the new velocity of a ball reflecting off of a wall which is rotating about a point with constant angular velocity.

Parameters:
line - the line segment representing the initial position of the rotating wall
center - the point about which line rotates
angularVelocity - the angular velocity at which line rotates, in radians per second. A positive angular velocity denotes a rotation in the direction from the positive x-axis to the positive y-axis.
velocity - the velocity of the ball before impact
reflectionCoeff - the reflection coefficient
Returns:
the velocity of the ball after impacting the wall

timeUntilRotatingCircleCollision

public static double timeUntilRotatingCircleCollision(Circle circle,
                                                      Vect center,
                                                      double angularVelocity,
                                                      Circle ball,
                                                      Vect velocity)
Computes the time until a ball travelling at a specified velocity collides with a rotating circle.

Parameters:
circle - a circle representing the initial location and size of the rotating circle
center - the point around which the circle is rotating
angularVelocity - the angular velocity with which circle is rotating about center, in radians per second. A positive angular velocity denotes a rotation in the direction from the positive x-axis to the positive y-axis.
ball - a circle representing the size and initial position of the ball
velocity - the velocity of the ball before impact
See Also:
Double.POSITIVE_INFINITY

reflectRotatingCircle

public static Vect reflectRotatingCircle(Circle circle,
                                         Vect center,
                                         double angularVelocity,
                                         Circle ball,
                                         Vect velocity)
Computes the new velocity of a ball reflected off of a rotating circle.

Parameters:
circle - the rotating circle
center - the point about which circle is rotating
angularVelocity - the angular velocity with which circle is rotating about center, in radians per second. A positive angular velocity denotes a rotation in the direction from the positive x-axis to the positive y-axis.
ball - the size and position of the ball before impact
velocity - the velocity of the ball before impact
Returns:
the velocity of the ball after impacting the rotating circle

reflectRotatingCircle

public static Vect reflectRotatingCircle(Circle circle,
                                         Vect center,
                                         double angularVelocity,
                                         Circle ball,
                                         Vect velocity,
                                         double reflectionCoeff)
Computes the new velocity of a ball reflected off of a rotating circle.

Parameters:
circle - the rotating circle
center - the point about which circle is rotating
angularVelocity - the angular velocity with which circle is rotating about center, in radians per second. A positive angular velocity denotes a rotation in the direction from the positive x-axis to the positive y-axis.
ball - the size and position of the ball before impact
velocity - the velocity of the ball before impact
reflectionCoeff - the reflection coefficient
Returns:
the velocity of the ball after impacting the rotating circle

timeUntilBallBallCollision

public static double timeUntilBallBallCollision(Circle ball1,
                                                Vect vel1,
                                                Circle ball2,
                                                Vect vel2)
Computes the time until two balls collide.

Parameters:
ball1 - a circle representing the size and initial position of the first ball.
vel1 - the velocity of the first ball before impact
ball2 - a circle representing the size and initial position of the second ball.
vel2 - the velocity of the second ball before impact
Returns:
the time until collision or POSITIVE_INFINITY if the collision will not occur
See Also:
Double.POSITIVE_INFINITY

reflectBalls

public static Geometry.VectPair reflectBalls(Vect center1,
                                             double mass1,
                                             Vect velocity1,
                                             Vect center2,
                                             double mass2,
                                             Vect velocity2)
Computes the resulting velocities of two balls which collide.

Parameters:
center1 - the position of the center of the first ball
mass1 - the mass of the first ball
velocity1 - the velocity of the first ball before impact
center2 - the position of the center of the second ball
mass2 - the mass of the second ball
velocity2 - the velocity of the second ball before impact
Returns:
a VectPair, where the first Vect is the velocity of the first ball after the collision and the second Vect is the velocity of the second ball after the collision.