Defold 1.10.0 BETA

I tried to analyse the difference between the two versions of Box2D and it turns out that setting the linear velocity (and other params) in Box2D 3.0 requires that the body must be in the physical world and not sleeping (in legacy version it was not required). And it seems that when creating a body with the NewCollisionObject2D function this condition should be satisfied, but for some reason it is not.

By setting the enabled flag (in Defold it is called active) the body can be forced to be placed in the world and wake up. So this code works the same in both versions:

	local body = b2d.get_body(url)
	print("is active: ", b2d.body.is_active(body))
	b2d.body.set_active(body, true)
	print("get velocity (1): ", b2d.body.get_linear_velocity(body))
	b2d.body.set_linear_velocity(body, vmath.vector3(0, 10, 0))
	print("get velocity (2): ", b2d.body.get_linear_velocity(body))

-- DEBUG:SCRIPT: is active: 	false
-- DEBUG:SCRIPT: get velocity (1): 	vmath.vector3(0, 0, 0)
-- DEBUG:SCRIPT: get velocity (2): 	vmath.vector3(0, 9.9999990463257, 0)

But it seems to me that the body is supposed to be enabled (active) by default…? Not sure, I never used the b2d api. We were happy with the “old” physics api, where settinglinear_velocity property with go.set enables the body, then sets the velocity - there is no difference for us in the behavior.

3 Likes