org.jrabbit.base.graphics.transforms
Class Vector2f

java.lang.Object
  extended by org.jrabbit.base.graphics.transforms.Vector2f
All Implemented Interfaces:
GLTransform, BaseVector2f
Direct Known Subclasses:
CameraTransforms.ViewpointVector

public class Vector2f
extends java.lang.Object
implements GLTransform, BaseVector2f

Vector2f is the general-purpose vector class of jRabbit. It has an x and a y coordinate, and has quite a lot of functionality for controlling them. As a GLTransform, a Vector2f is used to translate the Modelview Matrix. This lets it be used as a "location" for graphical objects, as they can set their coordinate values and then translate their rendering to the appropriate position. A Vector2f can have listeners added to it. These listeners are alerted whenever the Vector2f changes.

Author:
Chris Molini

Field Summary
private  java.util.ArrayList<VectorListener> listeners
          The list of listeners to alert whenever changes occur.
protected  float x
          The x coordinate.
protected  float y
          The y coordinate.
 
Constructor Summary
Vector2f()
          Creates a vector at [0, 0].
Vector2f(float x, float y)
          Creates a vector at the indicated coordinates.
 
Method Summary
 void add(BaseVector2f vector)
          Adds another vector's coordinates.
 void add(BaseVector2f vector, float multiplier)
          Adds another vector's coordinates, scaled by the supplied multiplier.
 void add(float shiftX, float shiftY)
          Shifts the coordinates.
 void addListener(VectorListener vL)
          Adds a listener to the Vector2f.
 void addPolar(float magnitude, float theta)
          Shifts the vector via polar coordinates.
 void addX(float shiftX)
          Shifts the x coordinate.
 void addY(float shiftY)
          Shifts the y coordinate.
protected  void alertChange()
          Alerts all listeners that the Vector2f has been moved.
 float angle()
          Returns the degrees of rotation this vector has when converted to polar coordinates.
 float angleTowards(BaseVector2f vector)
          Calculates the polar angle from this vector to the target.
 float angleTowards(float x, float y)
          Calculates the polar angle from this vector to the target coordinates.
 void bind()
          Translates the OpenGL Modelview Matrix by the x and y values of this vector.
 void cap(float magnitude)
          Ensures that the vector does not have a magnitude greater than the one supplied.
 Vector2f copy()
          Creates a copy of this Vector2f.
 void decrease(float amount)
          "Shrinks" the vector by the indicated amount.
 void decreaseX(float amount)
          "Shrinks" the x coordinate of the vector.
 void decreaseY(float amount)
          "Shrinks" the y coordinate of the vector.
 Vector2f difference(BaseVector2f vector)
          Creates a vector that represents the difference between this vector and the other.
 float distanceSquared(float x, float y)
          Calculates the squared distance between this and the indicated coordinates.
 float distanceSquared(Vector2f vector)
          Calculates the squared distance between this and another vector.
 float distanceTo(BaseVector2f vector)
          Calculates the distance between this and another vector.
 float distanceTo(float x, float y)
          Calculates the distance between this and the indicated coordinates.
 void divide(float amount)
          Divides the vector.
 boolean equals(java.lang.Object other)
          As equals() in Object, but if the supplied object is a Vector2f, it checks to see if their coordinates are the same.
 float magnitude()
          Learns the size of the vector.
 void moveTowards(BaseVector2f target, float amount)
          Moves the vector's coordinates closer to the target vector by a certain amount.
 void moveTowards(float targetX, float targetY, float distance)
          Moves the vector's coordinates closer to the target coordinates by a certain amount.
 void multiply(float amount)
          Scales the vector.
 void release()
          Translates the OpenGL Modelview Matrix by the negatives of the x and y values of this vector.
 void removeListener(VectorListener vL)
          Removes a listener from the Vector2f.
 void reset()
          Sets the Vector2f to [0, 0].
 void set(BaseVector2f vector)
          Redefines the vector to have the same coordinates as another.
 void set(float x, float y)
          Redefines the vector.
 void setPolar(float theta, float magnitude)
          Redefines the vector from instructions in polar coordinates.
 void setX(float x)
          Redefines the x coordinate.
 void setY(float y)
          Redefines the y coordinate.
 java.lang.String toString()
          Obtains the Vector2f as a String.
 Vector2f unitVector()
          Creates a scaled version of this vector with a magnitude of 1.
 Vector2f unitVectorTowards(BaseVector2f target)
          Creates a unit vector angled towards the target.
 Vector2f unitVectorTowards(float x, float y)
          Creates a unit vector angled towards the target coordinates.
 void vertex()
          Tells OpenGL to create a vertex at this vector's coordinates.
 float x()
          Learns the x coordinate.
 float y()
          Learns the y coordinate.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

protected float x
The x coordinate.


y

protected float y
The y coordinate.


listeners

private java.util.ArrayList<VectorListener> listeners
The list of listeners to alert whenever changes occur.

Constructor Detail

Vector2f

public Vector2f()
Creates a vector at [0, 0].


Vector2f

public Vector2f(float x,
                float y)
Creates a vector at the indicated coordinates.

Parameters:
x - The initial x coordinate.
y - The initial y coordinate.
Method Detail

copy

public Vector2f copy()
Creates a copy of this Vector2f.

Returns:
A Vector2f with identical coordinates.

addListener

public void addListener(VectorListener vL)
Adds a listener to the Vector2f. This listener will be alerted whenever a change occurs.

Parameters:
listener - The listener to add.

removeListener

public void removeListener(VectorListener vL)
Removes a listener from the Vector2f.

Parameters:
listener - The listener to remove.

alertChange

protected void alertChange()
Alerts all listeners that the Vector2f has been moved.


reset

public void reset()
Sets the Vector2f to [0, 0].


x

public float x()
Learns the x coordinate.

Specified by:
x in interface BaseVector2f
Returns:
The x coordinate of the vector.

setX

public void setX(float x)
Redefines the x coordinate.

Parameters:
x - The new value for the x coordinate.

y

public float y()
Learns the y coordinate.

Specified by:
y in interface BaseVector2f
Returns:
The y coordinate of the vector.

setY

public void setY(float y)
Redefines the y coordinate.

Parameters:
y - The new value for the y coordinate.

set

public void set(float x,
                float y)
Redefines the vector.

Parameters:
x - The new x coordinate.
y - The new y coordinate.

set

public void set(BaseVector2f vector)
Redefines the vector to have the same coordinates as another.

Parameters:
vector - The vector to copy.

setPolar

public void setPolar(float theta,
                     float magnitude)
Redefines the vector from instructions in polar coordinates.

Parameters:
theta - The radians of the polar angle.
magnitude - The length of the polar vector.

addX

public void addX(float shiftX)
Shifts the x coordinate.

Parameters:
shiftX - The amount to add.

addY

public void addY(float shiftY)
Shifts the y coordinate.

Parameters:
shiftY - The amount to add.

add

public void add(float shiftX,
                float shiftY)
Shifts the coordinates.

Parameters:
shiftX - The amount to add to the x coordinate.
shiftY - The amount to add to the y coordinate.

addPolar

public void addPolar(float magnitude,
                     float theta)
Shifts the vector via polar coordinates.

Parameters:
magnitude - The magnitude of the shift.
theta - The angle of the shift.

add

public void add(BaseVector2f vector)
Adds another vector's coordinates.

Parameters:
vector - The vector to add.

add

public void add(BaseVector2f vector,
                float multiplier)
Adds another vector's coordinates, scaled by the supplied multiplier.

Parameters:
vector - The vector to add.
multiplier - The amount to multiply the vector's values.

moveTowards

public void moveTowards(float targetX,
                        float targetY,
                        float distance)
Moves the vector's coordinates closer to the target coordinates by a certain amount. Obviously, this is intended to make generic AI simpler.

Parameters:
targetX - The x coordinate to move towards.
targetY - The y coordinate to move towards.
distance - The maximum distance to move.

moveTowards

public void moveTowards(BaseVector2f target,
                        float amount)
Moves the vector's coordinates closer to the target vector by a certain amount.

Parameters:
target - The vector to move towards.
distance - The maximum distance to move.

decrease

public void decrease(float amount)
"Shrinks" the vector by the indicated amount. The vector keeps its proportional x/y ratio, but its magnitude is lessened by the indicated amount. NOTE: If the amount to decrease is greater than the vector's current magnitude, the vector is set to [0, 0].

Parameters:
amount - The amount to decrease.

decreaseX

public void decreaseX(float amount)
"Shrinks" the x coordinate of the vector. If the amount to shrink is larger than the magnitude of the x coordinate, the x coordinate is set to 0.

Parameters:
amount - The amount to shrink the x coordinate.

decreaseY

public void decreaseY(float amount)
"Shrinks" the y coordinate of the vector. If the amount to shrink is larger than the magnitude of the y coordinate, the y coordinate is set to 0.

Parameters:
amount - The amount to shrink the y coordinate.

multiply

public void multiply(float amount)
Scales the vector.

Parameters:
amount - The amount to multiply the coordinates by.

divide

public void divide(float amount)
Divides the vector.

Parameters:
amount - The amount to divide the coordinates by.

magnitude

public float magnitude()
Learns the size of the vector.

Returns:
The magnitude of this Vector2f.

cap

public void cap(float magnitude)
Ensures that the vector does not have a magnitude greater than the one supplied. If the magnitude is greater, it is scaled to the appropriate size.

Parameters:
magnitude - The maximum magnitude allowed to the vector.

difference

public Vector2f difference(BaseVector2f vector)
Creates a vector that represents the difference between this vector and the other.

Parameters:
vector - The vector to check against.
Returns:
The difference between the two vectors.

distanceSquared

public float distanceSquared(Vector2f vector)
Calculates the squared distance between this and another vector.

Parameters:
vector - The vector to check against.
Returns:
The squared distance from this vector to the other.

distanceSquared

public float distanceSquared(float x,
                             float y)
Calculates the squared distance between this and the indicated coordinates.

Parameters:
x - The target x coordinate.
y - The target y coordinate.
Returns:
The squared distance from this vector to the target coordinates.

distanceTo

public float distanceTo(BaseVector2f vector)
Calculates the distance between this and another vector.

Parameters:
vector - The vector to check against.
Returns:
The distance from this vector to the other.

distanceTo

public float distanceTo(float x,
                        float y)
Calculates the distance between this and the indicated coordinates.

Parameters:
x - The target x coordinate.
y - The target y coordinate.
Returns:
The distance from this vector to the target coordinates.

unitVector

public Vector2f unitVector()
Creates a scaled version of this vector with a magnitude of 1.

Returns:
A unit vector with the same x/y ratio as this one.

unitVectorTowards

public Vector2f unitVectorTowards(BaseVector2f target)
Creates a unit vector angled towards the target.

Parameters:
target - The vector that the unit vector should be in the direction of.
Returns:
A vector of magnitude 1 that extends from this vector towards the target.

unitVectorTowards

public Vector2f unitVectorTowards(float x,
                                  float y)
Creates a unit vector angled towards the target coordinates.

Parameters:
x - The target x coordinate.
y - The target y coordinate.
Returns:
A vector of magnitude 1 that extends from this vector towards the target coordinates.

angle

public float angle()
Returns the degrees of rotation this vector has when converted to polar coordinates.

Returns:
The angle of direction of this vector, in clockwise degrees.

angleTowards

public float angleTowards(BaseVector2f vector)
Calculates the polar angle from this vector to the target.

Parameters:
vector - The target vector.
Returns:
The angle, in degrees, towards the target.

angleTowards

public float angleTowards(float x,
                          float y)
Calculates the polar angle from this vector to the target coordinates.

Parameters:
x - The target x coordinate.
y - The target y coordinate.
Returns:
The angle, in degrees, towards the target.

bind

public void bind()
Translates the OpenGL Modelview Matrix by the x and y values of this vector.

Specified by:
bind in interface GLTransform

release

public void release()
Translates the OpenGL Modelview Matrix by the negatives of the x and y values of this vector.

Specified by:
release in interface GLTransform

vertex

public void vertex()
Tells OpenGL to create a vertex at this vector's coordinates. This should be called between a glBegin() and a glEnd().


equals

public boolean equals(java.lang.Object other)
As equals() in Object, but if the supplied object is a Vector2f, it checks to see if their coordinates are 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()
Obtains the Vector2f as a String.

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