org.jrabbit.base.graphics.transforms
Class Rotation

java.lang.Object
  extended by org.jrabbit.base.graphics.transforms.Rotation
All Implemented Interfaces:
GLTransform
Direct Known Subclasses:
CameraTransforms.ViewRotation

public class Rotation
extends java.lang.Object
implements GLTransform

A Rotation is a convenience class that represents the rotation of an object. It has a rotation (in clockwise degrees), and some convenience methods to affect it. These methods include operations like rotating by a certain amount and rotating towards a specific point. A Rotation can have listeners added to it. These listeners are alerted whenever the Rotation changes.

Author:
Chris Molini

Field Summary
protected  float degrees
          The degrees of this Rotation.
private  java.util.ArrayList<RotationListener> listeners
          The list of all objects listening to this Rotation's changes.
 
Constructor Summary
Rotation()
          Creates a Rotation of 0 degrees with an empty list of listeners.
Rotation(float degrees)
          Creates a Rotation with an initial setting and an empty list of listeners.
 
Method Summary
 void addListener(RotationListener listener)
          Adds a listener to the Rotation.
protected  void alertChange()
          Alerts all listeners that the Rotation has changed.
 void bind()
          Rotates the OpenGL matrix by the degrees in this Rotation.
 Rotation copy()
          Creates a copy of the current Rotation.
 float degrees()
          Learns the current rotation.
 boolean equals(java.lang.Object other)
          As equals() in Object, but if the supplied object is a Rotation, it checks to see if their angle of rotation is the same.
 int getRotationDirection(float targetAngle, float degreesToTurn)
          Indicates whether or not this Rotation needs to rotate clockwise or counter-clockwise to face the indicated angle.
 void release()
          Rotates the OpenGL matrix by the negative of the degrees in this Rotation.
 void removeListener(RotationListener listener)
          Removes a listener from the Rotation.
 void reset()
          Sets the Rotation to 0 degrees.
 void rotate(float degrees)
          Shifts the rotation.
 void rotateToFace(Vector2f start, Vector2f target)
          Defines the Rotation to have the same rotation as the angle between the two locations.
 void rotateTowards(float targetAngle, float degreesToTurn)
          Rotates the Rotation closer to the target angle.
 void rotateTowards(Vector2f start, Vector2f target, float degreesToTurn)
          Similar to rotateToFace(), but gradually rotates instead of completely changing rotation.
 void set(float degrees)
          Redefines the rotation.
 void set(Rotation rotation)
          Redefines the Rotation to have the same angle as another.
 float theta()
          Learns the current rotation, in radians.
 java.lang.String toString()
          Obtain the Rotation as a String.
 Vector2f unitVector()
          Creates a unit vector from this rotation's angle.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

degrees

protected float degrees
The degrees of this Rotation.


listeners

private java.util.ArrayList<RotationListener> listeners
The list of all objects listening to this Rotation's changes.

Constructor Detail

Rotation

public Rotation()
Creates a Rotation of 0 degrees with an empty list of listeners.


Rotation

public Rotation(float degrees)
Creates a Rotation with an initial setting and an empty list of listeners.

Parameters:
degrees - The angle the Rotation is initially set to.
Method Detail

copy

public Rotation copy()
Creates a copy of the current Rotation.

Returns:
A Rotation that has the same angle as this one.

addListener

public void addListener(RotationListener listener)
Adds a listener to the Rotation. This listener will be alerted whenever a change occurs.

Parameters:
listener - The listener to add.

removeListener

public void removeListener(RotationListener listener)
Removes a listener from the Rotation.

Parameters:
listener - The listener to remove.

alertChange

protected void alertChange()
Alerts all listeners that the Rotation has changed.


degrees

public float degrees()
Learns the current rotation.

Returns:
The clockwise degrees this is currently rotated.

theta

public float theta()
Learns the current rotation, in radians.

Returns:
The clockwise radians this is currently rotated.

reset

public void reset()
Sets the Rotation to 0 degrees.


set

public void set(Rotation rotation)
Redefines the Rotation to have the same angle as another.

Parameters:
rotation - The Rotation to copy.

set

public void set(float degrees)
Redefines the rotation.

Parameters:
degrees - The new degrees of rotation.

rotate

public void rotate(float degrees)
Shifts the rotation.

Parameters:
degrees - The amount of degrees to add to the current rotation.

rotateToFace

public void rotateToFace(Vector2f start,
                         Vector2f target)
Defines the Rotation to have the same rotation as the angle between the two locations.

Parameters:
start - The starting coordinates.
target - The coordinates to rotate towards.

rotateTowards

public void rotateTowards(Vector2f start,
                          Vector2f target,
                          float degreesToTurn)
Similar to rotateToFace(), but gradually rotates instead of completely changing rotation. The most obvious use for this method to to make aiming/following AI a little simpler.

Parameters:
start - The starting coordinates.
target - The coordinates to rotate towards.
degreesToTurn - The maximum degrees to turn.

rotateTowards

public void rotateTowards(float targetAngle,
                          float degreesToTurn)
Rotates the Rotation closer to the target angle. If the amount left to rotate is less than the rotation allowed, then the rotation is set to the target angle.

Parameters:
targetAngle - The angle to rotate towards.
degreesToTurn - The degrees permitted to rotate.

getRotationDirection

public int getRotationDirection(float targetAngle,
                                float degreesToTurn)
Indicates whether or not this Rotation needs to rotate clockwise or counter-clockwise to face the indicated angle. If the target angle is less than the allowed degrees of rotation away, this method indicates that the Rotation can simply face the target angle immediately.

Parameters:
targetAngle - The angle to rotate towards.
degreesToTurn - The degrees permitted to rotate.
Returns:
0 if the target angle is less than the degrees of freedom away, 1 if clockwise rotation is most efficient, and -1 if counter-clockwise.

unitVector

public Vector2f unitVector()
Creates a unit vector from this rotation's angle.

Returns:
A new Vector2f that corresponds with this angle on the unit circle.

bind

public void bind()
Rotates the OpenGL matrix by the degrees in this Rotation.

Specified by:
bind in interface GLTransform

release

public void release()
Rotates the OpenGL matrix by the negative of the degrees in this Rotation.

Specified by:
release in interface GLTransform

equals

public boolean equals(java.lang.Object other)
As equals() in Object, but if the supplied object is a Rotation, it checks to see if their angle of rotation is the same.

Overrides:
equals in class java.lang.Object
Parameters:
other - The object to check against.
Returns:
Whether or not this object is equivalent to the target.

toString

public java.lang.String toString()
Obtain the Rotation as a String.

Overrides:
toString in class java.lang.Object
Returns:
A String representation of the Rotation.