|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectvooga.physicsEngine.PhysicsEngine
public class PhysicsEngine
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.
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 |
---|
public PhysicsEngine()
Method Detail |
---|
public void setGravity(Vector2f acceleration)
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.
acceleration
- public Vector2f getGravity()
public static void setDebugWindowSize(int windowWidth, int windowHeight)
windowWidth
- windowHeight
- public static void setDebug(boolean debug)
setDebugWindowSize
.
debug
- windowWidth
- windowHeight
- public void setAirDamping(double damping)
damping
- public double getAirDamping()
public void processPhysics(java.util.List<PhysicalItem> items, double dt, boolean possiblyTouching)
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.)
dt
- time elapsed since last callpublic void updatePositions(java.util.List<PhysicalItem> items, double dt)
bodies
- list of bodies to update positions and rotations ondt
- time elapsed since last callpublic void processPhysicsForParameters(java.util.List<PhysicalParameters> bodies, double dt, boolean possiblyTouching)
PhysicalParameters
directly.
myBodies
- dt
- collisionsOn
- public void updatePositionsForParameters(java.util.List<PhysicalParameters> bodies, double dt)
PhysicalParameters
directly.
myBodies
- dt
- public static double getDebugHeight()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |