Defold-box2d Box2D extension

That’s brilliant! Great to have direct access to the step function.

1 Like

iOS14.6 on iPad6 all tests are passed.
Works great!

3 Likes

Thanks.

All platforms worked :partying_face:

7 Likes

Great job!

4 Likes

Hi @britzl
The asset was added but I can’t see it in assets portal.

Need wait? :slight_smile:

2 Likes

I believe that once an asset is updated in the “awsome-assets” repo, it should trigger a rebuild of the static webpage which is defold.com.
Ofc, sometimes we accidentally change something that makes it not build.
I think we’ll ping @britzl and I think he might have an idea what’s up.

4 Likes

Thanks. I’ll check why it doesn’t show up!

1 Like
5 Likes

Thanks:) :partying_face:

I’ve released a new version 0.85

  1. Refactor example scene get debug draw.
  2. Fixed android/ios builds. The dump function can’t create a file on mobile.
  3. [Breaking change] Refactor jointDef initialize method
    • Make initializeJoint method for every joint in box2d. This method return joint def lua table.
      function box2d.InitializePrismaticJointDef(bodyA, bodyB, anchor, axis) end
    • Remove initialize call when create joint from table. In world:CreateJoint()
    • So you can use initialize method if you need it. Or not use it, and create jointDef manually.
8 Likes

Released a new version 0.89 :partying_face:
Adding new usefull functions. :smiley:

On next release will add binding for b2Manifold. After that ne will have all basic functionality.

[CHANGE LOG]
1)add world:RayCast

--- Ray-cast the world for all fixtures in the path of the ray. Your callback
--- controls whether you get the closest point, any point, or n-points.
--- The ray-cast ignores shapes that contain the starting point.
--- @param callback function(Box2dFixture fixture, vector3 point, vector3 normal, float fraction)
--- @param point1 vector3 ray starting point
--- @param point2 vector3 ray ending point
function Box2dWorld:RayCast(callback, point1, point2) end

2)add world:QueryAABB

--- Query the world for all fixtures that potentially overlap the
--- provided AABB.
---@param callback function(Box2dFixture fixture)
---@param aabb table the query box. {lowerBound = vmath.vector3(0), upperBound = vmath.vector3(0)}
function Box2dWorld:QueryAABB(callback, aabb) end

3)add world:SetContactListener
no old_manifold in PreSolve
no impulse in PostSolve

--- Register a contact event listener.
--- listener = {
---    BeginContact = function (contact) end,
---    EndContact = function(contact) end,
---    PreSolve = function(contact, old_manifold) end,
---    PostSolve = function(contact,impulse) end
--- }
---@param listener table|nil
function Box2dWorld:SetContactListener(listener) end

4)add binding for b2Contact.
18 functions

8 Likes

Released a new version 0.9 :partying_face:
Now extension contains all basic functional.
It ready for using in real projects.

Next step is testing it on real projects.
I will try to make some small games, after finish my current game.

Any review, feedback and using are welcome:)

1)add Box2dContact:GetManifold()

--- Get the local manifold.
---@return Box2dManifold
function Box2dContact:GetManifold() end

2)add Box2dContact:GetWorldManifold()

--- Get the world manifold.
---@return Box2dWorldManifold
function Box2dContact:GetWorldManifold() end

3)move all native code to namespace box2dDefoldNE

9 Likes

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?)