[SOLVED] The b2d APIs can only be used on `Box2d (Legacy Defold version)`?

At first, I tried to use the b2d APIs on Box2D Version3 but I found the b2d APIs didn’t work until I switched to Box2D (Legacy Defold version)

local ball_id = factory.create(“#ball_factory”, start_pos)

local collision_url = msg.url(nil,ball_id,"collisionobject")
local body = b2d.get_body(collision_url)
local pos = b2d.body.get_position(body)

b2d.body.dump(body)
if body then
	b2d.body.apply_linear_impulse(body, vmath.vector3(0,-600,0), pos)
else
	print("Error: 无法获取物理 body,请检查组件命名或物理引擎状态")
end 

The b2d APIs can only be used on Box2d (Legacy Defold version)?

No, the box2d.* api’s work for both Box2d v2 and v3.
Note however, that their api’s aren’t exactly the same, as the underlying code is not the same!
In particular, the b2d.body.dump() does not exist in Box2d v3, and so we didn’t implement a Lua wrapper for it. The intention of the b2d.* api’s is to follow the underlying C api’s.

Here is an example app (:construction: Note, it’s updated to use Defold 1.13.0!) that showcases b2d.body.apply_linear_impulse()
*Press “Many” button, and when running the sample, press Space to apply an impulse on all bodies:

2 Likes

Thanks ! :grinning_face:

1 Like