com.brackeen.javagamebook.game
Class Physics

java.lang.Object
  extended bycom.brackeen.javagamebook.game.Physics

public class Physics
extends java.lang.Object

The Physics class is a singleton that represents various attributes (like gravity) and the functions to manipulate objects based on those physical attributes. Currently, only gravity and scoot-up (acceleration when traveling up stairs) are supported.


Field Summary
static float DEFAULT_GRAVITY_ACCEL
          Default gravity in units per millisecond squared
static float DEFAULT_SCOOT_ACCEL
          Default scoot-up (acceleration traveling up stairs) in units per millisecond squared.
 
Method Summary
 void applyGravity(GameObject object, long elapsedTime)
          Applies gravity to the specified GameObject according to the amount of time that has passed.
 float getGravityAccel()
          Gets the gravity acceleration in units per millisecond squared.
static Physics getInstance()
          Gets the Physics instance.
 float getJumpVelocity(float jumpHeight)
          Returns the vertical velocity needed to jump the specified height (based on current gravity).
 float getScootAccel()
          Gets the scoot-up acceleration in units per millisecond squared.
 void jump(GameObject object, float jumpVelocity)
          Sets the specified GameObject's vertical velocity to the specified jump velocity.
 void jumpToHeight(GameObject object, float jumpHeight)
          Sets the specified GameObject's vertical velocity to jump to the specified height.
 void scootDown(GameObject object, long elapsedTime)
          Applies the negative scoot-up acceleration to the specified GameObject according to the amount of time that has passed.
 void scootUp(GameObject object, long elapsedTime)
          Applies the scoot-up acceleration to the specified GameObject according to the amount of time that has passed.
 void setGravityAccel(float gravityAccel)
          Sets the gravity acceleration in units per millisecond squared.
 void setScootAccel(float scootAccel)
          Sets the scoot-up acceleration in units per millisecond squared.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_GRAVITY_ACCEL

public static final float DEFAULT_GRAVITY_ACCEL
Default gravity in units per millisecond squared

See Also:
Constant Field Values

DEFAULT_SCOOT_ACCEL

public static final float DEFAULT_SCOOT_ACCEL
Default scoot-up (acceleration traveling up stairs) in units per millisecond squared.

See Also:
Constant Field Values
Method Detail

getInstance

public static Physics getInstance()
Gets the Physics instance. If a Physics instance does not yet exist, one is created with the default attributes.


getGravityAccel

public float getGravityAccel()
Gets the gravity acceleration in units per millisecond squared.


setGravityAccel

public void setGravityAccel(float gravityAccel)
Sets the gravity acceleration in units per millisecond squared.


getScootAccel

public float getScootAccel()
Gets the scoot-up acceleration in units per millisecond squared. The scoot up acceleration can be used for smoothly traveling up stairs.


setScootAccel

public void setScootAccel(float scootAccel)
Sets the scoot-up acceleration in units per millisecond squared. The scoot up acceleration can be used for smoothly traveling up stairs.


applyGravity

public void applyGravity(GameObject object,
                         long elapsedTime)
Applies gravity to the specified GameObject according to the amount of time that has passed.


scootUp

public void scootUp(GameObject object,
                    long elapsedTime)
Applies the scoot-up acceleration to the specified GameObject according to the amount of time that has passed.


scootDown

public void scootDown(GameObject object,
                      long elapsedTime)
Applies the negative scoot-up acceleration to the specified GameObject according to the amount of time that has passed.


jumpToHeight

public void jumpToHeight(GameObject object,
                         float jumpHeight)
Sets the specified GameObject's vertical velocity to jump to the specified height. Calls getJumpVelocity() to calculate the velocity, which uses the Math.sqrt() function.


jump

public void jump(GameObject object,
                 float jumpVelocity)
Sets the specified GameObject's vertical velocity to the specified jump velocity.


getJumpVelocity

public float getJumpVelocity(float jumpHeight)
Returns the vertical velocity needed to jump the specified height (based on current gravity). Uses the Math.sqrt() function.