Defold-box2d Box2D extension

Box2D lua bindings for Defold Game Engine.
Try to keep lua api same as c++ api when it possible.

demo
github
asset portal

20 Likes

Thanks @sergey.lerg for his box2d extenstion. I use it as base for that extension

3 Likes

I try to make extension as much production ready as possible.
I use test to make sure, that all worked.
195 tests 195 passed 4074 assertions 0 failed 0 errors 0 unassertive 0 pending

But i think it need some iterations with fixes, polishing and new bindings.

Any code review, pull requests, experiments are welcome :grinning:
Let’s make using the full power of box2d in defold real. :grinning: :hugs:

9 Likes

Hello d954mas

It’s really wonderful what you have done! Thanks for sharing.

I tried before to implement “Conveyor belts” (tank) in Defold but couldn’t… I’ll give it a try with your extension very soon I hope. I’ll let you know…

Thanks again and don’t forget to add it as Defold Asset.

3 Likes

Thanks, i forget about it. :man_facepalming:
Make a request to add asset.

Amazing work @d954mas
I’m not an expert so sorry for my next question but this shouldn’t be within the Defold already? Like out of box?
I know extensions are a great feature of defold but idk just a question.

1 Like

I test extension in windows and html.
It should work on other platforms too.

Test in my android, and it crashed( :frowning_face:
Will make investigation later

Can you please check that it worked in:
1)ios
2)linux
3)mac

Defold alredy use box2d for 2d physics.
But it used somewhere in engine, and we have very limit control on how it worked.

With extension you will get full control.
Here a great answer about defold box2d and full box2d.

2 Likes

Oh interesting!
it makes sense. Thank you!

1 Like

Awesome!
The demo project works fine on Linux here. I tested debug and release builds as well as build-and-run from the editor.

195 tests 195 passed 4074 assertions 0 failed 0 errors 0 unassertive 0 pending

2 Likes

The local varaible BOX2D_UTILS in dominos_scene.script is empty. Do you intend to keep it empty?

1 Like

Yes, BOX2D_UTILS .debug_draw variable was created in other place.
Create get_debug_draw function. Now it looks better.

Thanks:)

Fixed android build.
The problem was with dump function.
It try to create file but can’t.
Also disable it for ios, i think it can have same problem.

static int Dump(lua_State *L){//void Dump()
    utils::check_arg_count(L, 1);

    World *lua_world = World_get_userdata_safe(L, 1);
    #if !defined(DM_PLATFORM_ANDROID) && !defined(DM_PLATFORM_IOS)
        lua_world->world->Dump();
    #else
        dmLogWarning("Dump not supported on mobiles");
    #endif
    return 0;
}
1 Like

Windows, linux, html, android worked. :smiley:

Mac and ios need tests. Need help with that.

Thanks

2 Likes

This is amazing - v2.4.1, the latest version!

I haven’t yet played with it, but I’m guessing this doesn’t tie in with any of the built in Defold components? Ie, a world from the Box2D extension interacting with a world from a Defold collection proxy?

To tie game objects to a Box2D body, would you have to set the position manually? Would this lag a frame behind?

Sorry for the barrage of questions.

1 Like

I dont know how to make tests but I ran it on the editor and made a build. It seems ok. MacOS


3 Likes

macOS Big Sur v11.4, tests passed.

2 Likes

It can’t work together with defold 2d physics.
You should remove defold engine box2d in appmanifest.

So this extension can’t interacting with defold components.
All physics you set manualy in code.

Yes.
You get position from body.
Then set position to go.

go.set_position(body:GetPosition())
1 Like

About frame lag.
This topic?
Dealing with the one frame lag when visualising dynamic bodies - #6 by Mathias_Westerdahl
if i understand correctly problem happened because script update is called before physics update?

In this extenstion you update physics manualy in script.

function update(self, dt)
    self.world:Step(1 / 60, 8, 3)
    self.world:DebugDraw()
end

Also you always get actual possition by body:GetPosition()

2 Likes