[Solved] Integrating Box3D into Defold

Hello,

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 (462.1 KB). You can just open this example and run it - you do not need to set up anything.

ext.manifext:

name: "Box3D"
platforms:
x86_64-win32:
context:
flags: [
"-fpermissive",
"-Wno-write-strings",
"-Wno-invalid-offsetof",
"-Wno-pointer-arith",
"-Wno-missing-braces",
"-Wno-deprecated",
"-Wno-c++-compat" # Suppress C vs C++ compatibility warnings
]
defines: [
"DM_PLATFORM_WINDOWS",
"DDM_DEBUG"
]

Errors:

/extensions/box3d/src/box3d/arena_allocator.c
Line 148: assigning to 'struct b3OverflowBlock *' from incompatible type 'void *'
148 | b3Array_Push( shared->overflows, block );
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/extensions/box3d/src/box3d/block_allocator.c
Line 23: assigning to 'struct b3Block *' from incompatible type 'void *'
23 | b3Array_Resize( allocator.blocks, count );
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Line 27: assigning to 'char *' from incompatible type 'void *'
27 | allocator.blocks.data[i].memory = b3Alloc( B3_BLOCK_SIZE * elemenclang++ -DDLIB_LOG_DOMAIN="BOX3D" -DDDF_EXPOSE_DESCRIPTORS -DDM_PLATFORM_WINDOWS -D_CRT_SECURE_NO_WARNINGS -D_CRT_USE_BUILTIN_OFFSETOF -D_WINSOCK_DEPRECATED_NO_WARNINGS -D__STDC_LIMIT_MACROS -DWINVER=0x0600 -DWIN32 -DNOMINMAX -DDM_DEBUG -DDM_PLATFORM_WINDOWS -DDDM_DEBUG -target x86_64-pc-win32-msvc -m64 -O2 -g -gcodeview -Wall -Werror=format -fvisibility=hidden -nostdinc++ -fno-exceptions -fpermissive -Wno-write-strings -Wno-invalid-offsetof -Wno-pointer-arith -Wno-missing-braces -Wno-deprecated -Wno-c++-compat -nostdinc++ -isystem /usr/lib/llvm-20/lib/clang/20/include -isystem /opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/include -isystem /opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/atlmfc/include -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/ucrt -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/winrt -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/um -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/shared -Iupload/extensions/box3d/include/ -Ibuild/box3d/ -Iupload/ -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//include -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//sdk/include -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//ext/include upload/extensions/box3d/src/box3d/contact.c -c -o build/contact.c_9.o
tSize );
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Line 65: assigning to 'struct b3Block *' from incompatible type 'void *'
65 | b3Array_Resize( allocator->blocks, requiredBlockCount );
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Line 69: assigning to 'char *' from incompatible type 'void *'
69 | allocator->blocks.data[i].memory = b3Alloc( B3_BLOCK_SIZE * allocator->elementSize );
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 Like

I just started a discussion on the Box3D repo about integrating it with Defold:

1 Like

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.

There might be some clues in the old box2d extension: defold-box2d
See if it still works, compare and contrast.

1 Like

Jolt Physics works well too when I added it to `extensions` folder of Defold by source. This old Box2D v2 works well too because it was written in C++. I wanted to add Box3D as source code but I forgot that C++ compiler cannot compile C code.

I should try to build Box3D with Clang (instead of VS) to use the libbox3d.a with Defold.

1 Like

No, Clang does not help because Defold requires MSVC libraries.

Defold build system (linker) cannot find the Box3D’s functions when I use Box3D Clang build:

box3d-defold-extension.zip (490.9 KB)

/extensions/box3d/ext.manifest
lld-link: error: undefined symbol: b3DestroyWorld

referenced by /tmp/job9669121312352376612/upload/extensions/box3d/src/extension.cpp:65
Box3D_1.lib(extension.cpp_0.o):(enum dmExtension::Result __cdecl FinalizeBox3D(struct ExtensionParams *))
lld-link: error: undefined symbol: b3DefaultWorldDef
referenced by /tmp/job9669121312352376612/upload/extensions/box3d/src/extension.cpp:44
Box3D_1.lib(extension.cpp_0.o):(enum dmExtension::Result __cdecl InitializeBox3D(struct ExtensionParams *))
lld-link: error: undefined symbol: b3CreateWorld
referenced by /tmp/job9669121312352376612/upload/extensions/box3d/src/extension.cpp:46
Box3D_1.lib(extension.cpp_0.o):(enum dmExtension::Result __cdecl InitializeBox3D(struct ExtensionParams *))
lld-link: error: undefined symbol: b3World_GetGravity
referenced by /tmp/job9669121312352376612/upload/extensions/box3d/src/extension.cpp:14
Box3D_1.lib(extension.cpp_0.o):(int __cdecl Lua_GetGravity(struct lua_State *))
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
com.defold.extender.ExtenderException: java.io.IOException: clang++ -DDLIB_LOG_DOMAIN="BOX3D" -DDDF_EXPOSE_DESCRIPTORS -DDM_PLATFORM_WINDOWS -D_CRT_SECURE_NO_WARNINGS -D_CRT_USE_BUILTIN_OFFSETOF -D_WINSOCK_DEPRECATED_NO_WARNINGS -D__STDC_LIMIT_MACROS -DWINVER=0x0600 -DWIN32 -DNOMINMAX -DDM_DEBUG -DDM_PLATFORM_WINDOWS -DDDM_DEBUG -target x86_64-pc-win32-msvc -m64 -O2 -g -gcodeview -Wall -Werror=format -fvisibility=hidden -nostdinc++ -fno-exceptions -fpermissive -Wno-write-strings -Wno-pointer-arith -Wno-missing-braces -Wno-c++-compat -Wno-unused-private-field -nostdinc++ -isystem /usr/lib/llvm-20/lib/clang/20/include -isystem /opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/include -isystem /opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/atlmfc/include -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/ucrt -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/winrt -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/um -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/shared -Iupload/extensions/box3d/include/ -Ibuild/box3d/ -Iupload/ -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//include -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//sdk/include -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//ext/include upload/extensions/box3d/src/extension.cpp -c -o build/extension.cpp_0.o
/usr/bin/ar rcs /tmp/job9669121312352376612/build/Box3D_1.lib build/extension.cpp_0.o
windres -i /var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//lib/x86_64-win32/engine.rc -O coff -o build/dmengine.res
clang++ -DDLIB_LOG_DOMAIN="UNKNOWN" -DDDF_EXPOSE_DESCRIPTORS -DDM_PLATFORM_WINDOWS -D_CRT_SECURE_NO_WARNINGS -D_CRT_USE_BUILTIN_OFFSETOF -D_WINSOCK_DEPRECATED_NO_WARNINGS -D__STDC_LIMIT_MACROS -DWINVER=0x0600 -DWIN32 -DNOMINMAX -DDM_DEBUG -DDM_PLATFORM_WINDOWS -DDDM_DEBUG -target x86_64-pc-win32-msvc -m64 -O2 -g -gcodeview -Wall -Werror=format -fvisibility=hidden -nostdinc++ -fno-exceptions -fpermissive -Wno-write-strings -Wno-pointer-arith -Wno-missing-braces -Wno-c++-compat -Wno-unused-private-field -nostdinc++ -isystem /usr/lib/llvm-20/lib/clang/20/include -isystem /opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/include -isystem /opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/atlmfc/include -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/ucrt -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/winrt -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/um -isystem /opt/platformsdk/Win32/WindowsKits/10/Include/10.0.28000.0/shared -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//include -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//sdk/include -I/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//ext/include build/main.cpp -c -o build/main_tmp2.o
clang++ -o build/dmengine.exe -target x86_64-pc-win32-msvc -m64 -O2 -g -fuse-ld=lld -Wl,/entry:mainCRTStartup -Wl,/safeseh:no -L/tmp/job9669121312352376612/build -L/tmp/job9669121312352376612/upload/extensions/box3d/lib/x86_64-win32 -lOpenGL32 -ldelayimp -lUser32 -lshell32 -lXinput9_1_0 -ldinput8 -ldxguid -lWS2_32 -liphlpapi -lDbgHelp -lAdvAPI32 -lbcrypt -lPsapi -lGdi32 -lole32 -lBox3D_1 -L/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//lib/x86_64-win32 -L/var/extender/sdk/f735c12192bf95684e6ae1ae27c400b8170fc6d8/defoldsdk//ext/lib/x86_64-win32 -L/opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/lib/x64 -L/opt/platformsdk/Win32/MicrosoftVisualStudio2026/VC/Tools/MSVC/14.51.36231/atlmfc/lib/x64 -L/opt/platformsdk/Win32/WindowsKits/10/Lib/10.0.28000.0/ucrt/x64 -L/opt/platformsdk/Win32/WindowsKits/10/Lib/10.0.28000.0/um/x64 -llibengine -llibengine_service -llibdmbedtls -llibzip -llibprofile -llibprofilerext -llibprofiler_remotery -llibrecord -llibgameobject -llibddf -llibfont -llibresource -lgamesys -lgamesys_model -lgamesys_rig -lscript_box2d_defold -llibgraphics -llibgraphics_transcoder_basisu -llibbasis_transcoder -llibphysics -llibBulletDynamics -llibBulletCollision -lplatform -llibLinearMath -llibbox2d_defold -llibrender -llibrender_font_default -llibscript -llibluajit-5.1 -llibextension -lhid -linput -llibparticle -llibrig -llibdlib -llibimage -llibglfw3 -llibgui -llibcrashext -llibliveupdate -llibsound -lvpx -llibdecoder_wav -llibdecoder_ogg build/main_tmp2.o build/dmengine.res


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
end

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=OFF
cmake --build dist -j8 --config Release

To run this example (zip is attached below) on your computer at first you should open the extensions/box3d/link_lib.bat and edit path to the include and lib folders to yours:

:: link_libs.bat

:: First, remove old folders/links if any remain from previous attempts
rmdir /S /Q include
rmdir /S /Q lib\x86_64-win32

:: Create the folder structure
mkdir lib\x86_64-win32

:: Create symbolic links
:: This line mirrors the include folder from your built Box3D MSVC directory
mklink /D include "C:\libs\box3d-0.1.0-release-msvc\include"

:: This line links the compiled Box3D MSVC static library (.lib) for Defold's Windows target
mklink lib\x86_64-win32\box3d.lib "C:\libs\box3d-0.1.0-release-msvc\lib\box3d.lib"

Save and run (by double click) the link_libs.bat script. Now you can run the example in Defold: box3d-defold-extension-msvc.zip (6.3 KB)

4 Likes

Example: debug-drawer-box3d-defold-v1.1.zip (989.1 KB)

I’ve included box3d.lib in the extension:
image

And the Box3D’s include/box3d folder:

So, you can open and run the example above in Defold without any actions.

debug-drawer-box3d-defold-v1.1

9 Likes

I tried to run your demo using the most recent .zip file, but box3d.h seems to be missing :frowning: :

1 Like

Thanks a lot! I’ve forgotten to include include/box3d folder. I’ve updated the archive above. You can try it again.

2 Likes

Creating cuboids from Lua script for Windows

Example: cuboids-from-lua-box3d-defold.zip (989.4 KB)
GitHub repository

cuboids-from-lua-box3d-defold

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!")
11 Likes

Pre-built Box3D v0.1.0 libraries for Windows, Android, and WebAssembly

This 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:

  • VS 2026
  • NDK 23.1.7779620
  • emsdk 6.0.3
6 Likes