Using Box2D with Lua

The Box2D library is a physical simulation engine developed in the C++ language. Fortunately, there's a binding to the Lua language called LuaBox2D. This recipe shows you how to prepare the LuaBox2D library for use in case there's no binary package available for your platform.

Getting ready

The LuaBox2D library uses the CMake building system to accommodate different platform needs. This also makes the preparation process almost painless and automatic. However, there are certain requirements to make this possible.

You'll will need the following:

  • A C++11 standard compliant C++ compiler
  • The CMake build system
  • The Git versioning system
  • The Lua 5.1 development package with header files and linkable libraries

After these requirements are satisfied, you can start building the LuaBox2D library.

How to do it…

First, you'll have to download the LuaBox2D repository content to your computer with the following Git command:

git clone --recursive https://github.com/soulik/LuaBox2D.git

This will create the LuaBox2D directory with the source files. In the next step, you'll need to prepare a working directory that will contain project files, as well as compiled binaries. This directory will be called build and can be created with the mkdir command:

mkdir LuaBox2D/build

You can move into the working directory with the following command:

cd LuaBox2D/build

Now, it's time to use CMake to prepare project building files. The easiest way to use CMake is to run the following command in the current directory:

cmake ..

Depending on the currently used platform, this will locate the Lua 5.1 header files and libraries and set up the building environment without the use of an intervention. This is mostly true on Unix-based systems where every file has its place. In case you're using the Windows operating system, you'll need to set the correct paths for the Lua 5.1 header and library files.

To make this easier, there's a graphical frontend for the CMake building system called cmake-gui. You can use it by issuing a similar command to the LuaBox2D directory:

cmake-gui ..

This will open up a CMake application window with the dialog window asking for your building environment type. Unix-based systems usually provide the cmake tool with a similar interface to cmake-gui, but it uses the gui console instead. The usage of this tool is exactly the same as in previous cases.

On the Windows operating system, you'll probably want to use Microsoft Visual Studio. The only settings you'll need to change are LUA_INCLUDE_DIR, LUA_LIBRARIES, and LUA_LIBRARY. The first one should point to the directory with header files from the Lua development package ending with .h. The other two should point to the Lua 5.1 library file ending with the .lib or .a extensions.

You can validate the settings by pressing the Configure button. If requirements are satisfied, all parts marked in the color red will turn white.

Clicking on the Generate button will complete the preparation for building the environment:

How to do it…

Now, you can use the building tool to actually build the LuaBox2D library. On the Windows operating system, you'll need to open the Microsoft Visual Studio solution file ending with the .sln extension and build it. On the Unix-based system, you can use the make command in the LuaBox2D directory.

If everything went well, you should be left with the LuaBox2D library file ending with the .dll or .so extensions inside the build/bin directory.

From this point, you can use the LuaBox2D library in your Lua scripts with the following line:

local box2d = require 'LuaBox2D'

How it works...

LuaBox2D presents an interface between the Lua language and the Box2D library, which is written in the C++ language.

The main issue is correct memory management of the Box2D objects to prevent application crashes and other unexpected behavior. LuaBox2D tries to elevate those concerns by doing all the hard work behind the scenes. Lua developers are presented with an easy-to-use interface and all the features that the Box2D library provides.

Most of this work is done with the next generation of the Lutok2 middleware library that connects the C++ language with Lua language environment. There are many other alternatives to this middleware library. Each one has its pros, cons, and limitations. This one tries to offer a full set of functions that the Lua language provides with certain perks the newest C++ language standard provides.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.213.212