#include <Physics.h>
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 () |
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
Physics.h also contains one or two macros at the beginning of the file
box2d world in Physics::initLevel()) should be approx. 2 times the size of the game world! 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.
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! | 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.
| 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.
| 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.
| void sonus::Physics::initLevel | ( | Level2D * | level | ) |
| 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.
| 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.
| 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.
| 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.
| 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.
| 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. |
| 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.
| 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(). |
box2d::b2BodyDef containing the maximum amount of shapes allowed for a single body. 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.
1.5.4