howto, teaching

Getting Started with Lua + LÖVE: Part 3


Notes on this guide:

This part is written under the assumption of using  ZeroBrane Studio, an IDE introduced in the first part. However, and assuming LÖVE is installed on the system, all examples will work when run from within the current working directory of the “main.lua” file using “love ./” to execute on the entire current directory.

Box2D Physics

LÖVE uses Box2D as the underlining physics system. This means that LÖVE supports rigid body calculations and can support multiple body shapes.

The first step toward changing the initial pixel-based movement to incorporating a physics systems is to create a world and set the unit measurement.

GitHb Gist:


  world = love.physics.newWorld( 0, 0, true )

Once a world has been created, bodies can be added to the world. From bodies, shapes can be added through fixtures.

In LÖVE, a body can dictate where one or more shapes start. Through using fixtures, shapes and bodies are combined.

In order to move, a body needs to be set to the type “dynamic”.

Changing the “player” table into this new model results in the following:

GitHub Gist:


  player = {}
  player.width = 20
  player.height = 20
  player.body = love.physics.newBody( world, 100, 100, "dynamic" )
  player.shape = love.physics.newRectangleShape(player.body:getX(), player.body:getY(), player.width, player.height) 
  player.fixture = love.physics.newFixture(player.body, player.shape, 1)

With the love.load section changed, the love.update function also needs to use the new physics system. Instead of moving by pixels like in the second part, forces will be applied to the new body.

GitHub Gist:


function love.update(dt)
  if love.keyboard.isDown("up") then
    player.body:applyForce(0, -40)
  if love.keyboard.isDown("down") then
    player.body:applyForce(0, 40)
  if love.keyboard.isDown("left") then
    player.body:applyForce(-40, 0)
  if love.keyboard.isDown("right") then
    player.body:applyForce(40, 0)

Finally, the love.draw function needs to be changed. As pixel-by-pixel moving is no longer used, the draw functions cannot use the player.x and player.y coordinates. Instead, the position of the physics body will be used.

GitHub Gist:


function love.draw()"fill", player.body:getX(), player.body:getY(), player.width, player.height )


Updated “main.lua” Example