public class Geometry
extends java.lang.Object
The library is described in terms of these concepts:
The intended use of the Geometry library is as follows:
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 in between 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.
Modifier and Type | Class and Description |
---|---|
static class |
Geometry.DoublePair
DoublePair is a simple immutable type representing
a pair of double s. |
static class |
Geometry.VectPair
VectPair is a simple immutable type representing
a pair of Vect s. |
Modifier and Type | Field and Description |
---|---|
static Geometry.DoublePair |
DOUBLE_PAIR_NAN
DoublePair with both
d1 and d2
set to Double.NaN |
Modifier and Type | Method and Description |
---|---|
static Vect |
applyReflectionCoeff(Vect incidentVect,
Vect reflectedVect,
double rCoeff)
Accounts for the effects of inelastic collisions given the initial
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 infinitely 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 |
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,
traveling 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 traveling 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 traveling 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,
traveling at a specified velocity collides with a specified line
segment.
|
public static final Geometry.DoublePair DOUBLE_PAIR_NAN
d1
and d2
set to Double.NaN
Double.NaN
public static void setTuningParameters(double maximumForesight, boolean useDoughnut, int numberOfSlices)
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.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 (notably 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.setForesight(double)
,
Double.POSITIVE_INFINITY
public static void setForesight(double maximumForesight)
maximumForesight
.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.Double.POSITIVE_INFINITY
public static Geometry.DoublePair quadraticSolution(double a, double b, double c)
result.d1
. If no real roots exist, the
returned pair will contain NaN
for both values.Double.NaN
public static double minQuadraticSolution(double a, double b, double c)
NaN
if no real roots exist.Double.NaN
public static Vect perpendicularPoint(LineSegment line, Vect point)
line
which forms a line with
point
that is perpendicular to line
.line
- ; requires that line
has non-zero lengthline
which forms a line with
point
that is perpendicular to line
, or
null
if no such point exists within the given line
segment.perpendicularPointWholeLine(LineSegment, Vect)
public static Vect perpendicularPointWholeLine(LineSegment line, Vect point)
line
which forms a line with point
that
is perpendicular to line
.line
- ; requires that line
has non-zero lengthline
which forms a line with point
that
is perpendicular to line
, or null
if no
such point exists within the given line segment.perpendicularPoint(LineSegment, Vect)
public static Vect applyReflectionCoeff(Vect incidentVect, Vect reflectedVect, double rCoeff)
incidentVect
- the initial velocity of the ballreflectedVect
- the resulting velocity after the collision
assuming elasticity.rCoeff
- the reflection coefficient; requires that rCoeff
>= 0reflectedVect
. A
reflection coefficient of 0 implies that the collision will
absorb any energy that was reflected in the elastic case.public static double timeUntilWallCollision(LineSegment line, Circle ball, Vect velocity)
line
- the line segment representing a wall or (part of) an
object that might be collided with; requires that line
has non-zero lengthball
- a circle indicate the size and location of a ball
which might collide with the given line segmentvelocity
- the velocity of the ball before impactDouble.POSITIVE_INFINITY
,
endpoint effectspublic static Vect reflectWall(LineSegment line, Vect velocity, double reflectionCoeff)
line
- the line segment representing the wall which is being hit;
requires that line
has non-zero lengthvelocity
- the velocity of the ball before impactreflectionCoeff
- the reflection coefficient; requires that
reflectionCoeff
>= 0public static Vect reflectWall(LineSegment line, Vect velocity)
line
- the line segment representing the wall which is being hit;
requires that line
has non-zero lengthvelocity
- the velocity of the ball before impactpublic static double distanceSquared(Vect v1, Vect v2)
v1
and v2
.public static double distanceSquared(double x1, double y1, double x2, double y2)
(x1, y1)
and (x2,
y2)
.public static double timeUntilCircleCollision(Circle circle, Circle ball, Vect velocity)
circle
- a circle representing the circle with which the
ball may collideball
- a circle representing the size and initial location
of the ball; requires that ball.radius > 0velocity
- the velocity of the ball before impactDouble.POSITIVE_INFINITY
public static Vect reflectCircle(Vect circle, Vect ball, Vect velocity, double reflectionCoeff)
circle
- the center point of the circle which is being hitball
- the center point of the ballvelocity
- the velocity of the ball before impactreflectionCoeff
- the reflection coefficient; requires that
reflectionCoeff
>= 0public static Vect reflectCircle(Vect circle, Vect ball, Vect velocity)
circle
- the center point of the circle which is being hitball
- the center point of the ballvelocity
- the velocity of the ball before impactpublic static Vect rotateAround(Vect point, Vect cor, Angle a)
p
by
a
around the center of rotation, cor
,
and returns the result.point
- the initial location of the point to be rotatedcor
- the point indicating the center of rotationa
- the amount by which to rotate point
point
rotated around cor
by a
public static LineSegment rotateAround(LineSegment line, Vect cor, Angle a)
line
by a
around the center of
rotation, cor
, and returns the result.line
- the initial location of the line segment to be rotatedcor
- the point indicating the center of rotationa
- the amount by which to rotate point
line
rotated around cor
by a
public static Circle rotateAround(Circle circle, Vect cor, Angle a)
circle
by a
around the center of
rotation, cor
, and returns the result.circle
- the initial location of the circle to be rotatedcor
- the point indicating the center of rotationa
- the amount by which to rotate point
circle
rotated around cor
by a
public static Geometry.DoublePair timeUntilCircleCollision(Circle circle, Vect point, Vect velocity)
circle
- circle to find collisions withpoint
- initial position of the pointvelocity
- linear velocity of the pointDouble.POSITIVE_INFINITY
public static double timeUntilRotatingWallCollision(LineSegment line, Vect center, double angularVelocity, Circle ball, Vect velocity)
line
- the initial position of the rotating line segment (wall);
requires that line
has non-zero lengthcenter
- 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 ballvelocity
- the initial velocity of the ball. The ball is
assumed to travel at a constant velocity until impact.Double.POSITIVE_INFINITY
,
endpoint effectspublic static Vect reflectRotatingWall(LineSegment line, Vect center, double angularVelocity, Circle ball, Vect velocity)
line
- the line segment representing the initial position of
the rotating wall; requires that line
has non-zero lengthcenter
- the point about which line
rotates; requires
that the ball is at the point of impactangularVelocity
- 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 impactline
, velocity
is
returned.public static Vect reflectRotatingWall(LineSegment line, Vect center, double angularVelocity, Circle ball, Vect velocity, double reflectionCoeff)
line
- the line segment representing the initial position of
the rotating wall; requires that line
has non-zero lengthcenter
- the point about which line
rotates; requires
that the ball is at the point of impactangularVelocity
- 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 impactreflectionCoeff
- the reflection coefficient; requires that
reflectionCoeff
>= 0line
, velocity
is
returned.public static double timeUntilRotatingCircleCollision(Circle circle, Vect center, double angularVelocity, Circle ball, Vect velocity)
circle
- a circle representing the initial location and size
of the rotating circlecenter
- the point around which the circle is rotatingangularVelocity
- 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 ballvelocity
- the velocity of the ball before impactDouble.POSITIVE_INFINITY
public static Vect reflectRotatingCircle(Circle circle, Vect center, double angularVelocity, Circle ball, Vect velocity)
circle
- the rotating circlecenter
- the point about which circle
is
rotatingangularVelocity
- 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;
requires that the ball is at the point of impactvelocity
- the velocity of the ball before impactpublic static Vect reflectRotatingCircle(Circle circle, Vect center, double angularVelocity, Circle ball, Vect velocity, double reflectionCoeff)
circle
- the rotating circlecenter
- the point about which circle
is
rotatingangularVelocity
- 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;
requires that the ball is at the point of impactvelocity
- the velocity of the ball before impactreflectionCoeff
- the reflection coefficientpublic static double timeUntilBallBallCollision(Circle ball1, Vect vel1, Circle ball2, Vect vel2)
ball1
- a circle representing the size and initial position
of the first ball.vel1
- the velocity of the first ball before impactball2
- a circle representing the size and initial position
of the second ball.vel2
- the velocity of the second ball before impactDouble.POSITIVE_INFINITY
public static Geometry.VectPair reflectBalls(Vect center1, double mass1, Vect velocity1, Vect center2, double mass2, Vect velocity2)
center1
- the position of the center of the first ball; requires
that the distance between the two balls is approximately equal to the
sum of their radii; that is, the balls are positioned at the point of impact.mass1
- the mass of the first ball; requires that mass1 > 0velocity1
- the velocity of the first ball before impactcenter2
- the position of the center of the second ballmass2
- the mass of the second ball; requires that mass2 > 0velocity2
- the velocity of the second ball before impactVectPair
, 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.