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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.