[Solved] Integrating Box3D into Defold #102
|
Hello, Here is the original topic on the official Defold forum: https://forum.defold.com/t/wip-integrating-box3d-into-defold/83038 I try to integrate Box3D to Defold as a native extension. I added the Box3D source code to the extensions folder. It is required only 15 seconds to recompile a project in Defold. Box3D is written in the pure C language. It looks like it is impossible to integrate it without changing of Box3D source code from C to C++. But Box2D was integrated to Defold and maybe it is possible for Box3D too? Simple example that includes Box3D source code: box3d-defold-extension.zip. You can just open this example and run it - you do not need to set up anything. ext.manifext: Errors: |
Replies: 4 comments 4 replies
|
I see now my mistake. It is impossible to compile C code with C++ compiler. So, it is impossible to integrate Box3D to Defold using a source code of Box3D. It could be possible if to build Box3D to .lib but I do not know what flags Defold uses to configure a build. |
|
It works now with MSVC build of Box3D! It prints a gravity value of the Box3D physics world from a Lua script: function init(self)
if box3d then
local gravity = box3d.get_gravity()
if gravity then
print(string.format("Box3D Gravity -> x: %.1f, y: %.1f, z: %.1f", gravity.x, gravity.y, gravity.z))
else
print("Box3D world is not initialized yet!")
end
else
print("Error: box3d module not found!")
end
endext.manifestextension.cpp#include <dmsdk/sdk.h>
#define LIB_NAME "Box3D"
#define MODULE_NAME "box3d"
#include <box3d/box3d.h>
// Global Box3D world pointer/identifier
static b3WorldId g_WorldId = { 0 };
// 1. Function to call from Lua to get the gravity
static int Lua_GetGravity(lua_State* L) {
if (!B3_IS_NULL(g_WorldId)) {
b3Vec3 g = b3World_GetGravity(g_WorldId);
lua_newtable(L);
lua_pushnumber(L, g.x); lua_setfield(L, -2, "x");
lua_pushnumber(L, g.y); lua_setfield(L, -2, "y");
lua_pushnumber(L, g.z); lua_setfield(L, -2, "z");
} else {
lua_pushnil(L);
}
return 1;
}
// 2. Registration of module methods
static const luaL_reg Module_methods[] = {
{"get_gravity", Lua_GetGravity},
{NULL, NULL}
};
// 3. Lua initialization
static void LuaInit(lua_State* L) {
luaL_register(L, MODULE_NAME, Module_methods);
lua_pop(L, 1);
}
// 4. Extension lifecycle functions
static dmExtension::Result AppInitializeBox3D(dmExtension::AppParams* params) {
return dmExtension::RESULT_OK;
}
static dmExtension::Result InitializeBox3D(dmExtension::Params* params) {
// Create Box3D world with default settings
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.gravity = (b3Vec3){ 0.0f, -9.8f, 0.0f };
g_WorldId = b3CreateWorld(&worldDef);
LuaInit(params->m_L);
dmLogInfo("Box3D Extension Initialized!");
lua_pushboolean(params->m_L, 1);
lua_setglobal(params->m_L, "BOX3D_READY");
return dmExtension::RESULT_OK;
}
static dmExtension::Result AppFinalizeBox3D(dmExtension::AppParams* params) {
return dmExtension::RESULT_OK;
}
static dmExtension::Result FinalizeBox3D(dmExtension::Params* params) {
// Clean up Box3D world
if (!B3_IS_NULL(g_WorldId)) {
b3DestroyWorld(g_WorldId);
g_WorldId = (b3WorldId){ 0 };
}
return dmExtension::RESULT_OK;
}
static dmExtension::Result OnUpdateBox3D(dmExtension::Params* params) {
// Optional: Step the physics world here if you want automatic stepping per frame
// float timeStep = 1.0f / 60.0f;
// int subStepCount = 4;
// b3World_Step(g_WorldId, timeStep, subStepCount);
return dmExtension::RESULT_OK;
}
static void OnEventBox3D(dmExtension::Params* params, const dmExtension::Event* event) {
// Handle system events if needed
}
// 5. Extension entry point
DM_DECLARE_EXTENSION(Box3D, LIB_NAME, AppInitializeBox3D, AppFinalizeBox3D, InitializeBox3D, OnUpdateBox3D, OnEventBox3D, FinalizeBox3D)I've built Box3D using MSVC from PowerShell using this commands: cmake -G "Visual Studio 18 2026" -A x64 -S . -B dist `
-DCMAKE_INSTALL_PREFIX="C:/libs/box3d-0.1.0-release-msvc" `
-DBOX3D_SAMPLES=OFF `
-DBOX3D_UNIT_TESTS=OFF `
-DBUILD_SHARED_LIBS=OFF `
-DBOX3D_BENCHMARKS=OFF `
-DBOX3D_DOCS=OFFcmake --build dist -j8 --config ReleaseTo run this example (zip is attached below) on your computer at first you should open the Save and run (by double click) the |
|
Example: cuboids-from-lua-box3d-defold.zip (989.4 KB)
It looks the same like the example above but it creates cuboids from Lua: -- Create Static Ground (Rotated 10 degrees around Z axis)
-- Parameters: type, x, y, z, hx, hy, hz, rotation_degrees
box3d.create_box("static", 0.0, -2.0, 0.0, 5.0, 0.2, 5.0, 10.0)
-- Create First Dynamic Falling Box
-- Parameters: type, x, y, z, hx, hy, hz, rotation (nil), density
box3d.create_box("dynamic", 0.0, 5.0, 0.0, 0.5, 0.5, 0.5, nil, 1.0)
-- Create Second Dynamic Falling Box (Offset position)
box3d.create_box("dynamic", 0.75, 7.0, 0.0, 0.5, 0.5, 0.5, nil, 1.0)
print("Successfully spawned ground and boxes from Lua!") |
Pre-built Box3D 0.1.0 libraries for Windows, Android, and WebAssemblyThis project includes pre-built Box3D libraries for Windows, Android, and WebAssembly:
You can download and open it in Defold without setting anything up:
View Wasm Demo: https://pre-built-box3d-defold.vercel.app Tested on Android device:
As you can see, I've added the creation of spheres in Lua: -- Spawn a few falling balls (spheres)
-- Parameters: type ("dynamic"), x, y, z, radius, density
box3d.create_sphere("dynamic", -0.5, 6.0, 0.0, 0.6, 1.0)
box3d.create_sphere("dynamic", 0.5, 7.5, 0.0, 0.8, 2.0)
print("Spawned simple demo with spheres and boxes!")Tools:
|





Example: cuboids-from-lua-box3d-defold.zip (989.4 KB)
It looks the same like the example above but it creates cuboids from Lua: