Class Velocity

java.lang.Object
  extended byVelocity

public class Velocity
extends Object

This class models a velocity vector (direction and magnitude) for a system of x-y coordinates.


Constructor Summary
Velocity(double deltaX, double deltaY)
          Initialize a new Velocity object, remembering the given (dx,dy) direction.
 
Method Summary
 void bounceHorizontal()
          Bounce the vector as though it were bouncing off of a wall.
 void bounceVertical()
          Bounce the vector as though it were bouncing off of the floor or the ceiling.
 double getDX()
          Get the change in x per unit time represented by this vector.
 double getDY()
          Get the change in y per unit time represented by this vector.
 double magnitude()
          Return the magnitude (the length) of this vector.
 void rotate(double degrees)
          Turn the direction vector through a counter-clockwise angle specified in degrees.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Velocity

public Velocity(double deltaX,
                double deltaY)
Initialize a new Velocity object, remembering the given (dx,dy) direction.

Parameters:
deltaX - the change in x per unit time
deltaY - the change in y per unit time
Method Detail

bounceHorizontal

public void bounceHorizontal()
Bounce the vector as though it were bouncing off of a wall. Reverse the sign of the horizontal direction (dx), but leave the vertical direction (dy) the same.


bounceVertical

public void bounceVertical()
Bounce the vector as though it were bouncing off of the floor or the ceiling. Reverse the sign of the vertical direction (dy), but leave the horizontal direction (dx) the same.


rotate

public void rotate(double degrees)
Turn the direction vector through a counter-clockwise angle specified in degrees. The formulas for doing this are:
    radians = Math.toRadians(degrees);
    rx = Math.cos(radians)*dx - Math.sin(radians)*dy;
    ry = Math.sin(radians)*dx + Math.cos(radians)*dy;
 
Note that since the functions Math.sin(radians) and Math.cos(radians) take their arguments in radians, the conversion from degrees to radians must be performed before calculating the new dx and dy values, as shown above.

Parameters:
degrees - the counter-clockwise angle through which to rotate this direction vector

magnitude

public double magnitude()
Return the magnitude (the length) of this vector. The magnitude of a velocity vector gives the speed with which the object is moving. The magnitude of the vector is calculated with the formula:
   Math.sqrt(dx*dx+dy*dy)
 

Returns:
the magnitude of this velocity vector

getDX

public double getDX()
Get the change in x per unit time represented by this vector.

Returns:
the change in x per unit time

getDY

public double getDY()
Get the change in y per unit time represented by this vector.

Returns:
the change in y per unit time