Defold already has Box2D as a physics engine, however it's very limited. Therefore to unleash its full potential this extension was made. It doesn't cover the full Box2D API yet, but it's easy to add particular features you are missing. PRs are welcome.
In this extension Box2D is not tied to the Defold world so to speak, it can run fully independent. This for instance allows you to run a quick simulation of a flying projectile when a player is aiming and show the computed projectile path. Yes, like in Angry Birds.
You can have as many physics worlds as you want and it's up to you how you would like to tie physics objects and graphical objects on screen together. Normally you would just set your game object position and rotation in the update method to its physics body's position and rotation.
Keep in mind that Box2D works in meters, not in pixels. Either scale each physics value to match your game coordinate system, or scale down your game world to match the physics coordinate system. Some games are better coded with the first option, others with another.
box2d.dynamic_body
box2d.static_body
box2d.kinematic_body
box2d.distance_joint
box2d.friction_joint
box2d.gear_joint
box2d.motor_joint
box2d.mouse_joint
box2d.prismatic_joint
box2d.pulley_joint
box2d.revolute_joint
box2d.rope_joint
box2d.weld_joint
box2d.wheel_joint
Open game.project
and a new entry to the dependencies
property:
https://github.com/Lerg/extension-box2d/archive/master.zip
Then select Project -> Fetch Libraries
to download the extension in your project.
After that you have to disable Defold's physics engine otherwise the inner libraries would clash and your game won't build. To do so you can open game.project
and set App Manifest
to /box2d/physics_null.appmanifest
.
Initializes the extension and displays Box2D version.
Crates and returns a new physics world.
The params
table
Vector3. Sets the gravity vector for the world. Default is (0, -10).
box2d.init()
local world = box2d.new_world{
gravity = vmath.vector3(0, -20, 0)
}
Creates a new physics body. Please refer to the official Box2D documentation for body descriptions and their parameters.
The params
table
Body type. box2d.static_body
, box2d.dynamic_body
(default) or box2d.kinematic_body
.
Boolean.
Vector3.
Boolean.
Float.
Float.
Float. Bouncyness.
Float. If width and height are set a box is created.
Float.
Float. If radius is set and width/height are missing a circle is created.
Function. Receives collision event.
Function. Receives collision event.
Function. Receives collision event.
Function. Receives collision event.
self.world = box2d.new_world{
gravity = self.gravity
}
local ground_body = self.world:new_body{
type = box2d.static_body,
position = vmath.vector(0, -500),
width = 1000,
height = 10,
friction = 1,
on_enter_collision = function(event)
print('event.other.position', event.other.position)
end
}
Creates a new joint between physics bodies. Please refer to the official Box2D documentation for joint descriptions and their parameters.
The params
table
Joint type.
Boolean.
Float.
Float.
Float.
Float.
Float.
Float.
Float.
Float.
Physics body.
Physics body.
Physics joint.
Physics joint.
Vector3.
Vector3.
Vector3.
Vector3.
Vector3.
local joint = self.world:new_joint{
type = box2d.weld_joint,
body = player_body,
other_body = wall_body,
anchor = player_body.position
}
Progresses the simulation.
Float.
Integer.
Integer.
function update(dt)
self.world:step(dt, 6, 4)
end
Destroys physics world and all physics bodies in it.
Occurs when physics bodies are colliding.
Physics body.
Physics body.
Applies force to the body.
Vector3.
Vector3.
Destroys the body.
Boolean. Read/Write.
Float. Read/Write.
Vector3. Read/Write.
Body type. Read/Write.
Float. Read.
Create a world using box2d.new_world()
, add bodies calling world:new_body()
with the newly created world. Run physics simulation calling world:step()
, usually in your update()
method. Play with physics bodies by applying force to them or changing their velocities, update visual representation of physics bodies with position and angle of the bodies.
If you like this extension please consider supporting me on Patreon https://patreon.com/Lerg