howto, teaching

Getting Started with Lua + LÖVE: Part 3

Series:

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:

Pre-Formatted:

  love.physics.setMeter(64)
  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:

Pre-Formatted:

  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:

Pre-formatted:

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

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:

Pre-formatted:

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

end

Updated “main.lua” Example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s