Time for action – running Box2D physics engine
Let's rewrite the DroidBlaster physics engine with Box2D with the following steps:
- Open the
jni/PhysicsManager.hpp
header and insert the Box2Dinclude
file.Define a constant
PHYSICS_SCALE
to convert the body position from physics to game coordinates. Indeed, Box2D uses its own scale for a better precision.Then, replace
PhysicsBody
with a new structure,PhysicsCollision
, that will indicate which bodies entered in collision, as shown in the following:#ifndef PACKT_PHYSICSMANAGER_HPP #define PACKT_PHYSICSMANAGER_HPP #include "GraphicsManager.hpp" #include "TimeManager.hpp" #include "Types.hpp" #include <Box2D/Box2D.h> #include <vector> #define PHYSICS_SCALE 32.0f struct PhysicsCollision { bool collide; PhysicsCollision(): collide(false) {} }; ...
- Then, make
PhysicsManager
inherit fromb2ContactListener
. A contact listener gets notified about new collisions each time the...