Defold-box2d Box2D extension

Fantastic. You’re a hero.
I finally decided to shirk my real work and try this out.

Fortunately I’ve got a pretty good idea how Box2D works from using Love2D, but wading through the documentation is still a bit painful. Your example project is very helpful to have. I might submit a PR later to make the documentation more pretty and possibly split it up into multiple documents, if you’re open to that?

Question 1:
Would it be a pain to shorten the names of the constants? Or are you committed to having the C+±style prefixes on everything?
It’s pretty annoying to type: “box2d.b2BodyType.b2_dynamicBody” every time (not to mention it’s hard to read.)
I would prefer: “box2d.BodyType.dynamic”, or maybe: “box2d.TYPE_DYNAMIC” to make it fit with the Defold API and the naming conventions for constants that I’m familiar with.

And all the others, like:
box2d.b2Shape.e_circle → box2d.SHAPE_CIRCLE
box2d.b2Draw.e_shapeBit → box2d.DRAW_SHAPE_BIT

[Edit] Maybe even shorten the common ones more, since there’s no overlap, i.e: box2d.CIRCLE, box2d.DYNAMIC

Of course the end user can always make their own aliases, so it’s not a huge deal.

Question 2:
I think I know this one, but just to check: All the units for making shapes and body positions are in Box2D “meters” right? And the physics_scale for the debug draw is only to scale the drawing?

Question 3:
Can the velocity iterations and position iterations for Box2dWorld:Step() have default values? I imagine 99% of users will just use the recommended values of 8 and 3 (but they’ll have to look them up too).

5 Likes

Thanks for your feedback:)

About documentation:
Yes i am open)
Documentation is very big, and painful.
2155 lines in raw edit=)
It was fast to make it. And
it is better than no documentation.

Github have support for wiki pages.
Most of documentation must be there,
not in readme))

For now, the best documentation is box2d documentation)) It is splitted, and contains a lot of explanations and examples)

About example:
Box2d have good testbed app;
https://box2d.org/documentation/md__d_1__git_hub_box2d_docs_testbed.html
I want to make example scene same as it.
It contains a lot of example scenes, and some helping ui.

3 Likes

Question 1:
I try to keep api same as box2d api.
1)So i don’t want to change constants names
2)I use CameCase instead of underscore

---CameCase
world:CreateBody(def)
---underscore
world:create_body(def)

3)I keep api same.
Box2dWorld:GetBodyList()
return single body not list.
It is easy to return list of bodies, but api return single body, so i return single body too

---Get the world body list. With the returned body, use b2Body:GetNext() to get the next body in the world list.
---A nil body indicates the end of the list.
---@return Box2dBody|nil the head of the world body list.
function Box2dWorld:GetBodyList() end

I use Idea and emmylua plugin for code. So i have autocomple when use constants in code. And it is easy to use them))

In defold editor there no autocomplete. But it can be added. Editor have special format for it.
I will not add such autocomplete , but someone can add it=)

2 Likes

Question 2:
Yes it worked with Box2D “meters”.
You need manually convert your game positions to box2d meters.

Yes debug draw convert meters to pixels.
Also debug draw is slow. It use draw_line message.

It will be good, to convert it to openGL calls. But i do not know it api. I try to make it.
I take gl lib from defold imgui and gl code from box2d/DebugDraw.cpp at master · behdad/box2d · GitHub

It compiled, but not working.
Maybe later i will return to it.

2 Likes

Question 3:
It is easy to do that.
But box2d api not do that:)

I think when extension will have better documentation it will be easy to find recommended values

2 Likes

Add autobuilder CI script for extension.

5 Likes

Yeah, much better than no documentation! :slight_smile:

Ahh, okay. I think that same answer will probably cover most other questions I’d have in the future. :slight_smile:

If you really want to keep the same api as closely as possible I think you would need more classes and a few global variables.

-- https://box2d.org/documentation/md__d_1__git_hub_box2d_docs_hello.html
b2PolygonShape groundBox;
groundBox.SetAsBox(50.0f, 10.0f);
-- ...
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(0.0f, 4.0f);
b2Body* body = world.CreateBody(&bodyDef);

…but forget I said that. :stuck_out_tongue:


What is your plan for filtering. Is there some trouble with it, or you just haven’t had time for it yet?


Anyway, I haven’t tested all that much, but really awesome extension so far! So nice to have full access to Box2D. Continuous collision detection…chain shapes…preSolve… :partying_face: …control over update order (which is a two-edged sword…)

4 Likes

I never worked with filtering:)

What is it?)

I can manually select, collide objects or not?)

Setting & getting the category and mask bitmasks. You have it in the limitations in the Readme:

5)No binding some b2Fixture functions.

not support filter in fixture def.
...
void SetFilterData(const b2Filter& filter);
const b2Filter& GetFilterData() const;

Also Box2D: Dynamics Module

2 Likes

Oh I was sure that I add it)) sorry):joy:
I will add it in next release

2 Likes

I think that you ask about that.

void SetContactFilter(b2ContactFilter* filter);
1 Like

Hmm, that’s the one one b2World? Hmm, I don’t actually know about that one, I don’t think I would need that if there is a default filter function set.

What I mean is:

  1. In the b2FixtureDef you should be able to set the filter - which should have ‘categoryBits’, ‘maskBits’, and ‘groupIndex’ fields.

  2. After you create it, the b2Fixture should have:
    void SetFilterData (const b2Filter &filter)
    constb2Filter& GetFilterData () const

1 Like

:drooling_face:

2 Likes

Thanks for your reply) @ross.grams

I’ve released a new version 0.9.1
1)Add binding for b2Filter.

1 Like

Try to make this extenstion more user friendly.

1)Split documentation to different pages. Thanks to @ross.grams
2)Update ui for example scene.
3)Enable high dpi for example project.
4)Add --verify-graphics-calls=false to engine arguments.Fixed performance on chrome.

9 Likes

I’ve released a new version 0.9.2
The main feature is b2Shape binding. In prev version use table to describe shape. Now you can create b2Shapes, or continue to use tables.

[CHANGELOG]

  1. Add full binding for b2Shape.(Now you can create and manipulate box2d shapes)

  2. body:GetTransform() return b2Transform table. Prev was position and angle.

  3. Fixed debug render not draw transform(center of mass)

  4. Add Box2dFixture functions.(Box2dFixture full binding done)

    • Box2dFixture:GetAABB()
    • Box2dFixture:GetShape()
    • Box2dFixture:RayCast()
4 Likes

Hi @Mathias_Westerdahl
I need some help with ne. Can you please help me with it:)

How can i get lua_State *L from anywhere?

1)I can save lua_State *L to global variable when any native function called from lua.

Maybe you know easy way?

I need it because:

box2d is use assert function.

#define b2Assert(A) assert(A)
b2Assert(m_vertices == nullptr && m_count == 0);
b2Assert(count >= 2);

So if b2assert happened it crashed the engine.
I want to change assert to lua_error(L);

But lua_error need lua_State *L

You get the Lua state in the extension init function (where you register the Lua module)

2 Likes

Thanks.
I can save that luaState? it will not be changed?

Correct, that Lua state is valid until the matching extension ext function is called.

1 Like