Book Image

Android NDK: Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK: Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

Table of Contents (18 chapters)
Android NDK Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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;
    
        PhysicsCollision():
            collide(false)
        {}
    };
    ...
  2. Then, make PhysicsManager inherit from b2ContactListener. A contact listener gets notified about new collisions each time the simulation is updated. Our PhysicsManager...