Time for action – running Box2D physics engine

Let's rewrite the DroidBlaster physics engine with Box2D with the following steps:

  1. Open the jni/PhysicsManager.hpp header and insert the Box2D include 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; ...

Get Android NDK Beginner's Guide - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.