Physics2D
Worlds & Bodies
Physics2D makes flat game objects fall, roll, bump, and bounce. It can also make a squishy slime. You can use the easy slime tools first and learn the detailed body tools later.
New to coding?
Copy the examples exactly first. Run them. Then change one number or word and run them again. You are not expected to memorize the command lists.
1
A small rigid-body world
A world is the invisible place where physics happens. Gravity pulls things down. The floor stays still. The ball can fall.
Try this code
using Physics2D; # Turn on the 2D physics tools
world = Physics2D.world(0, 980); # Make gravity pull downward
floor = Physics2D.box(world, "static", 400, 580, 800, 40); # A floor that cannot move
ball = Physics2D.circle(world, "dynamic", 400, 100, 24); # A ball that can fall
Physics2D.step(world, 1 / 60); # Move physics forward one frame
info = Physics2D.body_info(world, ball); # Ask where the ball is2
World API
step moves the physics forward. Call it once every game frame. MAKO automatically takes smaller steps when something moves very fast.
Try this code
world(gx=0, gy=980, fixed_step=1/60, substeps=4)
step(world, delta=fixed_step)
body_count(world)
clear(world)
destroy_world(world)3
Body API
dynamic means an object can fall and bounce. static means it never moves. kinematic means your code moves it.
Try this code
circle(world, type, x, y, radius, mass=1, bounce=0.2, friction=0.4)
box(world, type, x, y, width, height, mass=1, bounce=0.2, friction=0.4, rotation=0)
set_position(world, body, x, y)
set_velocity(world, body, vx, vy)
set_rotation(world, body, degrees)
apply_force(world, body, fx, fy)
apply_impulse(world, body, ix, iy)
apply_impulse_at(world, body, ix, iy, world_x, world_y)
apply_torque(world, body, torque)
lock_rotation(world, body, locked=true)
set_damping(world, body, linear, angular)
remove_body(world, body)