vooga.physicsEngine
Class PhysicsEngine

java.lang.Object
  extended by vooga.physicsEngine.PhysicsEngine

public class PhysicsEngine
extends java.lang.Object

This is the main API class of the physics engine. To use, one make a list (say bodies) of PhysicalParameters that we want to update, and and call update(bodies, dt, handleCollision) at each game loop to update the physics for all these bodies.

 PhysicalEngine engine=new PhysicalEngine();
 List<PhysicalParameters> bodies=new ArrayList<PhysicalParameters>();
 
 PhysicalParameters body1=new PhysicalParameter(new Circle(10));        //creates a circle of radius 10
 body1.setCentroidPosition(new Vector2f(100,100));
 body1.setMass(10);
 body1.setVelocity(new Vector2f(1,0));               //specify initial velocity
 bodies.add(body1)                                            //add to the list of bodies to pass to update (note there could be several disjoint lists)
 

Note that body1 does not represent a game item, but is an encapsulation held by a PhysicalItem. To make an immutable object (like a wall), one can simply call body1.setMovable(false) (which essentially sets the mass to infinity). One can also control physical properties of the body by calling:

 body1.setElasticity(0.2);
 body1.setFrictionCoefficient(0.3);
 
During game play, due to human or AI control, one can increment (add to current) bodies location or change positions by calling:
 body1.incrementPosition(new Vector2f(dx, dy));
 body1.incrementVelocity(new Vector2f(dvx, dvy));
 
At each iteration of the game loop, the game engine should call engine.processPhysics(bodies, dt, true) for each list of bodies that may have collided with one another. dt represents the time elapsed since the last update. For bodies that definitely have not collided, the game engine will call engine.processPhysics(otherBodies, dt, false)

At the very end of the game loop one should call engine.updatePositions(otherBodies,dt) to actually move the bodies.

Author:
Peng Shi

Constructor Summary
PhysicsEngine()
          Constructor for initialization
 
Method Summary
 double getAirDamping()
           
static double getDebugHeight()
          Return the height of the debugging window.
 Vector2f getGravity()
           
 void processPhysics(java.util.List<PhysicalItem> items, double dt, boolean possiblyTouching)
          Updates the physics engine, given that time elapsed since last update is dt.
 void processPhysicsForParameters(java.util.List<PhysicalParameters> bodies, double dt, boolean possiblyTouching)
          Code used for interacting with the physics engine without the game engine framework (for internal testing by the physics engine).
 void setAirDamping(double damping)
          Set the air damping (drag) of the physics world.
static void setDebug(boolean debug)
          Turn on the debug window if this is set to true.
static void setDebugWindowSize(int windowWidth, int windowHeight)
          Set the size of the window for debugging.
 void setGravity(Vector2f acceleration)
          Set the gravity of the physics engine (for example, use setGlobalForce(new Vector2f(0,-9.8)) to set g=9.8, pointed down.) This is a constant force that is applied to all moveable bodies.
 void updatePositions(java.util.List<PhysicalItem> items, double dt)
          Move the bodies according to previously done calculations.
 void updatePositionsForParameters(java.util.List<PhysicalParameters> bodies, double dt)
          Code used for interacting with the physics engine without the game engine framework (for internal testing by the physics engine).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PhysicsEngine

public PhysicsEngine()
Constructor for initialization

Method Detail

setGravity

public void setGravity(Vector2f acceleration)
Set the gravity of the physics engine (for example, use setGlobalForce(new Vector2f(0,-9.8)) to set g=9.8, pointed down.) This is a constant force that is applied to all moveable bodies.

Parameters:
acceleration -

getGravity

public Vector2f getGravity()
Returns:
the gravitational acceleration of the system

setDebugWindowSize

public static void setDebugWindowSize(int windowWidth,
                                      int windowHeight)
Set the size of the window for debugging.

Parameters:
windowWidth -
windowHeight -

setDebug

public static void setDebug(boolean debug)
Turn on the debug window if this is set to true. This will draw a secondary window with the outlines of all the PhysicalParameters currently being processed. This can help one debug if one suspects a discrepancy between the game representation and the underlying physics representation, causing collisions not being handled correctly. One can manipulate the size of the debug window using setDebugWindowSize.

Parameters:
debug -
windowWidth -
windowHeight -

setAirDamping

public void setAirDamping(double damping)
Set the air damping (drag) of the physics world. 0 denotes no damping. Essentially acceleration/damping is the terminal velocity of an object.

Parameters:
damping -

getAirDamping

public double getAirDamping()
Returns:
the airDamping coefficient

processPhysics

public void processPhysics(java.util.List<PhysicalItem> items,
                           double dt,
                           boolean possiblyTouching)
Updates the physics engine, given that time elapsed since last update is dt. This methods updates the positions, rotations, and other properties of all the PhysicalItems that is passed in the first parameter. If possiblyTouching=false, then we do not check for collisions (which may result in overlapping objects). This is where most of the work is done.

Collision handling: Whenever some pair of physical objects touch, and either momentum or some force is pressing the objects against one another (conflicting normal force), we will create an Arbiter object, which adds the forces to find the resultant, and re-exert the force back to the centroid of the bodies, possibly causing impulses or torques. The Arbiter update procedure is repeated for iterations number of times, in order to handle transitivity of forces (i.e., several billiard balls in a line passing impulse to a billiard ball at the end.)

Parameters:
dt - time elapsed since last call

updatePositions

public void updatePositions(java.util.List<PhysicalItem> items,
                            double dt)
Move the bodies according to previously done calculations. This should be called at the end of the game loop passing in all physical objects that the physics engine handled. Each of the PhysicalParameters passed in the bodies list should have been previously passed to a processPhysics method in the same game loop.

Parameters:
bodies - list of bodies to update positions and rotations on
dt - time elapsed since last call

processPhysicsForParameters

public void processPhysicsForParameters(java.util.List<PhysicalParameters> bodies,
                                        double dt,
                                        boolean possiblyTouching)
Code used for interacting with the physics engine without the game engine framework (for internal testing by the physics engine). This is the same as processPhysics excepts one would pass a list of PhysicalParameters directly.

Parameters:
myBodies -
dt -
collisionsOn -

updatePositionsForParameters

public void updatePositionsForParameters(java.util.List<PhysicalParameters> bodies,
                                         double dt)
Code used for interacting with the physics engine without the game engine framework (for internal testing by the physics engine). This is the same as processPhysics excepts one would pass a list of PhysicalParameters directly.

Parameters:
myBodies -
dt -

getDebugHeight

public static double getDebugHeight()
Return the height of the debugging window.

Returns:
debugHeight