How do you use box2d extension?

I read the docs, can’t seem to figure it out. Went to chatgpt and they just told me to use local box2d = require "box2d.box2d" but defold saying /main/main.script The file '/box2d/box2d.lua' could not be found. and I don’t know much on cpp. So, there’s no in the imported extension. There’s no other lua files except utils.lua. I want to use it to draw physics shapes. I’m importing/fetching library https://github.com/d954mas/defold-box2d/archive/refs/tags/1.0.3.zip). I want to create runtime created physics shapes. Should there be like box2d file with lua bindings?

image

No need to use require for native extensions, they’re automatically available in all scripts. Looks like the documentation for that extension starts here:

1 Like

There are two main ways of doing this; either use Defold’s built in implementation of Box2D (simple): Spawn a game object containing a collisionobject component. Read more here: Physics in Defold

Or, if the shapes can’t be predefined, use this Box2D extension (advanced):

1 Like

can someone give simple script to render a shape using debug and apply gravity to a shape? Because, I’m struggling to figure out the box2d extension api based on d954mas and the position of object not changing:


--world (returns Box2dWorld)
--body def
--createbody
--shape
--debug draw

-- main.script

function init(self)
	
	self.world = box2d.NewWorld{
		gravity = vmath.vector3(0, -9.8, 0)
	}

	-- ground — static body
	self.ground = self.world:CreateBody {
		type = box2d.b2BodyType.b2_dynamicBody 
		, position = vmath.vector3(0, -100, 0),
	}
end

function update(self, dt)
	-- step the physics world
	self.world:Step(dt, 8, 3)

	-- update sprite positions from physics bodie
	print(self.ground:GetPosition())

end