sonus::Physics Class Reference

#include <Physics.h>

List of all members.

Public Member Functions

 Physics ()
 Physics (double timeStep=0.0166667, int iterations=10)
 ~Physics ()
void cleanForInit ()
void initLevel (Level2D *level)
void update (Level2D *level)
void createPlayer (Player *player)
void createPortal (Vector2D *portal, double radius)
void createBody (Body2D< Vector2D > *body, b2Body **storeBodies, int *bodyCounter)
void addBodyToWorld (Body2D< Vector2D > *body, Vector2D transformationVector, double scaleFactor, b2Body **b2BodyStorage, int *storagePosition)
b2BodyDef * createBodyDef (Body2D< Vector2D > *body, Vector2D transformationVector, double scaleFactor, int *startAt, int *elementsAdded)
void debugToGraphics ()


Detailed Description

sonus::Physics

The physics class. Uses the two dimensional physics engine box2d (version: 1.4.3). During initialization it takes the interpolated version of the corresponding World2D, breaks the border and all obstacles down into boxes (amount of detail depends on the chosen number of interpolation steps while loading the SVG file) and makes the portal and the player a circle. After initializing physics with Physics::initLevel(), the function Physics::update() should be integrated into the main game loop.

filename: Physics.h

Date:
2008/01/26 22:51
Author:
Raphael Estrada
Version:
0.5
See also:
Physics.cpp contains some macros at the beginning for more readable code and direct control of certain limits and values, used for example in the function Physics::createBodyDef().

Physics.h also contains one or two macros at the beginning of the file

Attention:
Physics::LEVEL_DIMENSIONS and Physics::LEVEL_DIMENSIONS_F are used to define the worlds size. The size of the physical world (set when initializing the box2d world in Physics::initLevel()) should be approx. 2 times the size of the game world!
Todo:
Tune force input from player attributes and force input into player after step

There is a memory leak somewhere! check task manager

Check the destructor Physics::~Physics(), it is most probably optimizable

The algorithms and functions are mostly quite unsafe, there should be some more limit checks etc.

Many members, especially functions like Physics::addBodyToWorld(), should better be private. They were made public for quicker prototyping.

Note:
Check Physics::cleanForInit() just in case the re-initialization of the Physics object causes problems
Warning:
The bigger the level and the higher the interpolation steps, the slower the physics will be, slowing down the whole game.
See also:
SvgLoader::calcResultSize() for details about the exponential growth
Bug:
There is a static maximum number of bodies and shapes that has to be set directly in the physics engine (b2settings.h; check box2d documentation for more info). Behaviour is unpredictable when the loaded level gets to big and any of those limits is reached. Take a look at the macros in Physics.cpp where several of those limits are set individually for sonus. They must NOT be larger than the limits set in box2d!

Constructor & Destructor Documentation

sonus::Physics::Physics (  ) 

Consructor.

sonus::Physics::Physics ( double  timeStep = 0.0166667,
int  iterations = 10 
)

Constructor. Set the world timeStep (per second) and amount of iterations for the physics.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters:
timeStep Times per second will the box2d engine will perform a time step. Consult box2d documentation for details. Default is 1/60 = 0.0166667.
iterations Amount of iterations the box2d engine will perform on constraints. Constraints are not used in sonus at this point, so this is rather trivial. Note that the higher the number of iterations, the longer the physics will generally need to be calculated, often resulting in slower game performance. Only modify (increase) if higher accuracy (especialls constraints) is needed. Consult the box2d documentation for details.

sonus::Physics::~Physics (  ) 

Destructor.

Attention:
May not be optimal yet. Check for optimizations.


Member Function Documentation

void sonus::Physics::cleanForInit (  ) 

Cleans up so the Physics class can be re-initialized using Physics:initLevel(). Needed, for example, when loading a new level but reusing the Physics object without having to create a new one.

Attention:
May be unstable since it was created shortly before the sonus codelock in Jan 2008.

void sonus::Physics::initLevel ( Level2D level  ) 

Initializes the physics world by giving a Leve2D pointer including Player, Portal and World2D. Builds the border, obstacles and sets Portal and Player.

Parameters:
level The Level2D object containing all the essential world objects needed for initializing the physical world.

void sonus::Physics::update ( Level2D level  ) 

Updates the physical world. First it reads data from the Level2D object level, where other modules have put their data. Then it takes a world step. Then the new data is fed back into the level.

Parameters:
level The Level2D object containing all the essential world objects which will be updated after the time step within the physical world.

void sonus::Physics::createPlayer ( Player player  ) 

Creates the physical player as a circle body.

Parameters:
player The Player object containing all the data needed to create a physical representation within the physical world.

void sonus::Physics::createPortal ( Vector2D portal,
double  radius 
)

Creates the physical portal as a circle body.

Parameters:
player The Player object containing all the data needed to create a physical representation within the physical world.
radius The radius of the portal.

void sonus::Physics::createBody ( Body2D< Vector2D > *  body,
b2Body **  storeBodies,
int *  bodyCounter 
)

Creates all parts of a Body2D as static geometry in the physical world. Since box2d has limited amounts for shapes per body, this method creates as many shapes as possible per body for maximum efficiency. Note that there also is an adjustable limit for the amount of bodies in box2d, too.

Parameters:
body The Body2D object containing Vector2D points which will be transferred into boxes forming a physical body.
storeBodies box2d::b2Body array in which the box2d b2BoxDef objects are to be stored.
bodyCounter A pointer to an integer where the amount of created b2BoxDef objects is to be saved.

void sonus::Physics::addBodyToWorld ( Body2D< Vector2D > *  body,
Vector2D  transformationVector,
double  scaleFactor,
b2Body **  b2BodyStorage,
int *  storagePosition 
)

Used internally by the Physics::createBody() method. Calls Physics::createBodyDef() as often as necessary to form a game body consisting of several physical box2d bodies.

Parameters:
body The Body2D object containing Vector2D points which will be transferred into boxes forming a physical body.
scaleFactor The factor that will be used to transform the SVG screen coordinates into the Physics own coordinate system, that has been initialized during Physics::initLevel() using Integrator::getTransformAndScaleFactor().
b2BodyStorage box2d::b2Body array in which the box2d b2BoxDef objects are to be stored.
storagePosition Pointer to an integer representing the momentary position within the data structure b2BodyStorage.
See also:
Integrator::getTransformAndScaleFactor() (and other functions within Integrator) for details on transforming coordinates.
Attention:
This method should not be called directly. It is intended to be used internally by Physics::createBody().

b2BodyDef* sonus::Physics::createBodyDef ( Body2D< Vector2D > *  body,
Vector2D  transformationVector,
double  scaleFactor,
int *  startAt,
int *  elementsAdded 
)

Used internally by the Physics::addBodyToWorld() method. Creates as many box2d shapes as possible (as defined in Physics::SHAPES_PER_BODY, which should match the maximum shape number set in box2d) and packs them into a single box2d body. This method is necessary to use the limits of box2d in a most effective way, allowing sonus levels to have the highest possible resolution.

Parameters:
body The Body2D object containing Vector2D points which will be transferred into boxes forming a physical body.
transformationVector A vector created by initialization in Physics::initLevel(), needed for transforming coordinates from SVG values into the Physics own values and back.
scaleFactor The factor that will be used to transform the SVG screen coordinates into the Physics own coordinate system, that has been initialized during Physics::initLevel() using Integrator::getTransformAndScaleFactor().
Returns:
A box2d::b2BodyDef containing the maximum amount of shapes allowed for a single body.
See also:
Integrator::getTransformAndScaleFactor() (and other functions within Integrator) for details on transforming coordinates.
Attention:
This method should not be called directly. It is intended to be used internally by Physics::addBodyToWorld().
Note:
The maximum number of shapes per body is defined within the box2d file b2settings.h and in the macro Physics::SHAPES_PER_BODY

void sonus::Physics::debugToGraphics (  ) 

Method to support debugging. Works as a toggle that must be activated before using the Physics::initLevel() method, or else no info will be shown. Requires sonus::Graphics.h to be included for access to relevant functionality. Displays data in the message box and draws graphical representations of collision vectors, collision normal as well als physical bodies.

Warning:
Activating this option will slow down performance quite a bit, depending on the system somewhere around 15 fps.


The documentation for this class was generated from the following file:
Generated on Tue Aug 26 12:26:54 2008 for sonus by  doxygen 1.5.4