org.jrabbit.base.math.geom
Class Geometry

java.lang.Object
  extended by org.jrabbit.base.math.geom.Geometry
All Implemented Interfaces:
Vertexed

public class Geometry
extends java.lang.Object
implements Vertexed

A Geometry object represents a 2D arbitrary shape. It is designed to work with all Java2D shapes. The point of Geometry is to provide an advanced geometry/collision detection mechanism that is fast and useful enough to be effective in a variety of situations. Geometry has two main components - a base shape, and its transformed child. The base shape defines the unscaled, unrotated, and untranslated shape. The transformed child is created to represent the shape with a scale, rotation, and translation. In this way, a game object can have a complex shape, and (as it moves, rotates, and scales/flips) then it can tell its Geometry to update its child shape. After this update, all intersection and similar calculations utilize this transformed child. Despite the complexity of this setup, for reasonably simple shapes (say, a 10-sided, straight-edged polygon) this is fairly fast and can allow a large number of objects. Additionally, a Geometry can be used to access the vertice data for its base shape. This allows it to interact with other objects not designed to work with Java2D Shapes (for example, Primitive rendering).

Author:
Chris Molini

Field Summary
protected  java.awt.geom.Path2D shape
          The base shape of the Geometry.
protected  java.awt.geom.AffineTransform transform
          The AWT transform that updates the base shape to create the transformed child shape.
protected  java.awt.geom.Path2D transformedShape
          The updated shape that accomodates translation, rotation, and scaling.
(package private)  float[][] vertices
          The stored vertices of the base shape.
 
Constructor Summary
Geometry()
          Creates an empty Geometry.
Geometry(float[][] vertices)
          Creates a Geometry from the supplied vertice data.
Geometry(float width, float height)
          Creates a rectangular Geometry with the indicated base dimensions.
Geometry(java.awt.Shape shape)
          Creates a Geometry with the indicated base shape.
Geometry(Vertexed vertexed)
          Creates a Geometry that defines its geometry from those of the indicated Vertexed.
 
Method Summary
 void apply(float x, float y, float theta, float xScale, float yScale)
          Recalculates the transformed shape so that it represents the base shape, but translated, rotated, and scaled as indicated.
 void apply(Vector2f location, Rotation rotation, Scalar scale)
          Recalculates the transformed shape so that it represents the base shape, but translated, rotated, and scaled as indicated.
 java.awt.geom.Path2D baseShape()
          Accesses the base shape that defines this geometry.
 boolean contains(float x, float y)
          Determines if the currently transformed Geometry contains the indicated point.
 boolean contains(Vector2f point)
          Determines if the currently transformed Geometry contains the indicated point.
static float[][] extractVertices(java.awt.geom.Path2D path)
          Converts a GeneralPath to its vertice data.
 java.awt.geom.Area intersection(Geometry target)
          Calculates the intersecting area of the two Geometries.
 java.awt.geom.Area intersection(java.awt.Shape target)
          Calculates the intersecting area of the the indicated Shape and the transformed Shape of this Geometry.
 boolean intersects(Geometry target)
          Determines if the two Geometries intersect.
 boolean intersects(java.awt.Shape target)
          Determines if the indicated Shape intersects the transformed Shape belonging to this Geometry.
 void set(float[][] vertices)
          Constructs a new base shape from the indicated vertex data.
 void set(java.awt.Shape shape)
          Redefines the base shape to be the indicated Java AWT Shape.
 void set(java.awt.Shape shape, double offsetX, double offsetY)
          Redefines the base shape to be the indicated Java AWT Shape.
 void set(Vertexed vertexed)
          Redefines the Geometry based on the geometry of the indicated Vertexed.
 java.awt.geom.Path2D shape()
          Accesses the updated shape derived from the base shape.
 float[][] vertices()
          Accesses the vertex data that approximates the base geometry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

shape

protected java.awt.geom.Path2D shape
The base shape of the Geometry.


transformedShape

protected java.awt.geom.Path2D transformedShape
The updated shape that accomodates translation, rotation, and scaling.


transform

protected java.awt.geom.AffineTransform transform
The AWT transform that updates the base shape to create the transformed child shape.


vertices

float[][] vertices
The stored vertices of the base shape. Every time the shape is redefined, these are recalculated, to make retrieving them quicker.

Constructor Detail

Geometry

public Geometry()
Creates an empty Geometry.


Geometry

public Geometry(float width,
                float height)
Creates a rectangular Geometry with the indicated base dimensions.

Parameters:
width - The width of the rectangle.
height - The height of the rectangle.

Geometry

public Geometry(java.awt.Shape shape)
Creates a Geometry with the indicated base shape.

Parameters:
shape - The Java Shape to use for the Geometry.

Geometry

public Geometry(float[][] vertices)
Creates a Geometry from the supplied vertice data. The resulting Geometry is straight-edged (no curved sides).

Parameters:
vertices - The point data to use for calculating the geometry.

Geometry

public Geometry(Vertexed vertexed)
Creates a Geometry that defines its geometry from those of the indicated Vertexed.

Parameters:
vertexed - The Vertexed object to acquire geometry info from.
Method Detail

extractVertices

public static float[][] extractVertices(java.awt.geom.Path2D path)
Converts a GeneralPath to its vertice data. This works best with straight-edged paths. For curved paths, this can screw up fairly badly. However, it returns a tolerable estimate, and gives correct results for straight-edged polygons.

Parameters:
path - The GeneralPath to convert.
Returns:
The vertices that comprise the corners of the path.

set

public void set(java.awt.Shape shape)
Redefines the base shape to be the indicated Java AWT Shape. This assumes that the supplied Shape is offset with its top-left corner at the origin. As such, it offsets the resulting shape so that the origin is at the center of its bounding box. NOTE: The current transformed shape is unchanged by this method; it will be updated on the next adjust() call.

Parameters:
shape - The new base shape.

set

public void set(java.awt.Shape shape,
                double offsetX,
                double offsetY)
Redefines the base shape to be the indicated Java AWT Shape. The resultant base shape is offset by the indicated amount. NOTE: The current transformed shape is unchanged by this method; it will be updated on the next adjust() call.

Parameters:
shape - The new base shape.
offsetX - The amount to offset the shape's X coordinate.
offsetY - The amount to offset the shape's Y coordinate.

set

public void set(float[][] vertices)
Constructs a new base shape from the indicated vertex data. The format for vertices should be {{x1, y2,}, {x2, y2}...}. The coordinates represent the base shape, and the center of the geometry will be at {0, 0}.

Parameters:
vertices - The vertex data used to build the new shape.

set

public void set(Vertexed vertexed)
Redefines the Geometry based on the geometry of the indicated Vertexed.

Parameters:
vertexed - The Vertexed object that contains the needed geometry.
See Also:
set(float[][])

baseShape

public java.awt.geom.Path2D baseShape()
Accesses the base shape that defines this geometry.

Returns:
The "prototype" shape.

shape

public java.awt.geom.Path2D shape()
Accesses the updated shape derived from the base shape.

Returns:
The current, transformed shape used for operations.

vertices

public float[][] vertices()
Accesses the vertex data that approximates the base geometry.

Specified by:
vertices in interface Vertexed
Returns:
An array of vertex coordinates that corresponds to the base shape.

apply

public void apply(float x,
                  float y,
                  float theta,
                  float xScale,
                  float yScale)
Recalculates the transformed shape so that it represents the base shape, but translated, rotated, and scaled as indicated. NOTE: To flip the shape, simply supply a negative value for one of the scale values.

Parameters:
x - The amount to translate the shape on the x axis.
y - The amount to translate the shape on the y axis.
theta - The amount to rotate, in clockwise radians.
xScale - The amount to scale the base shape on the x axis.
yScale - The amount to scale the base shape on the y axis.

apply

public void apply(Vector2f location,
                  Rotation rotation,
                  Scalar scale)
Recalculates the transformed shape so that it represents the base shape, but translated, rotated, and scaled as indicated.

Parameters:
location - The Vector2f that indicates how far to translate.
rotation - The Rotation that indicates how much to rotate.
scale - The Scalar that indicates how to scale.
See Also:
apply(float, float, float, float, float)

contains

public boolean contains(float x,
                        float y)
Determines if the currently transformed Geometry contains the indicated point.

Parameters:
x - The x-coordinate.
y - The y-coordinate.
Returns:
True if the point is contained, false if not.

contains

public boolean contains(Vector2f point)
Determines if the currently transformed Geometry contains the indicated point.

Parameters:
point - The Vector2f that represents the point to check for.
Returns:
True if the point is contained, false if not.
See Also:
contains(float, float)

intersects

public boolean intersects(Geometry target)
Determines if the two Geometries intersect. This compares their updated, transformed shapes for collision.

Parameters:
target - The Geometry to check against.
Returns:
True if the Geometries intersect, false if not.
See Also:
intersection(Shape)

intersects

public boolean intersects(java.awt.Shape target)
Determines if the indicated Shape intersects the transformed Shape belonging to this Geometry. To determine if the shapes collide, first the bounding boxes of the two Shapes are compared for intersection. If those intersect, a Java Area is created as the overlap of the two Shapes, and is checked to see if it encloses any space.

Parameters:
target - The Shape to check against.
Returns:
True if the Shapes intersect, false if not.

intersection

public java.awt.geom.Area intersection(Geometry target)
Calculates the intersecting area of the two Geometries.

Parameters:
target - The Geometry to check against.
Returns:
A Java Area that represents the overlapping space between the two Shapes.
See Also:
intersection(Shape)

intersection

public java.awt.geom.Area intersection(java.awt.Shape target)
Calculates the intersecting area of the the indicated Shape and the transformed Shape of this Geometry.

Parameters:
target - The Shape to check against.
Returns:
A Java Area that represents the overlapping space between the two Shapes.