Difference between revisions of "Tutorial:Physics"

m (The main.lua: ... preferably without introducing bugs)
 
(39 intermediate revisions by 19 users not shown)
Line 1: Line 1:
 
In this example we will create a red ball that rolls around on a green ground.
 
In this example we will create a red ball that rolls around on a green ground.
  
We'll start in the love.load function.
+
The finished example is at the end of this page. All of these functions may be placed in one file: main.lua
 +
 
 +
We'll start in the [[love.load]]() function.
  
 
=== love.load() ===
 
=== love.load() ===
  
First we need to set up a world for the physics bodies to exist in.
+
First we need to set up a [[love.physics.newWorld|world]] for the physics bodies to exist in.
 
<source lang="lua">
 
<source lang="lua">
 
function love.load()
 
function love.load()
   world = love.physics.newWorld(650, 650) --create a world for the bodies to exist in with width and height of 2,000
+
  love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700
+
   world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81
  world:setMeter(64) --the height of a meter in this world will be 64px
 
 
</source>
 
</source>
  
Now that a world has been created, we can add bodies to it.
+
Now that a [[love.physics.newWorld|world]] has been created, we can add [[Body|bodies]], [[Shape|shapes]], and [[Fixture|fixture]] to it.
  
 
<source lang="lua">
 
<source lang="lua">
  bodies = {} --create tables for the bodies and shapes so that the garbage collector doesn't delete them
+
  objects = {} -- table to hold all our physical objects
  shapes = {}
 
 
    
 
    
 
   --let's create the ground
 
   --let's create the ground
   --we need to give the ground a mass of zero so that the ground wont move
+
   objects.ground = {}
   bodies[0] = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the body anchors from the center of the shape
+
   objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2)
   shapes[0] = love.physics.newRectangleShape(bodies[0], 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50
+
   objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a rectangle with a width of 650 and a height of 50
 +
  objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape) --attach shape to body
 
    
 
    
 
   --let's create a ball
 
   --let's create a ball
   bodies[1] = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
+
   objects.ball = {}
   shapes[1] = love.physics.newCircleShape(bodies[1], 0, 0, 20) --the ball has a shape of 20
+
  objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
 +
  objects.ball.shape = love.physics.newCircleShape( 20) --the ball's shape has a radius of 20
 +
   objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.
 +
  objects.ball.fixture:setRestitution(0.9) --let the ball bounce
 +
 
 +
  --let's create a couple blocks to play around with
 +
  objects.block1 = {}
 +
  objects.block1.body = love.physics.newBody(world, 200, 550, "dynamic")
 +
  objects.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
 +
  objects.block1.fixture = love.physics.newFixture(objects.block1.body, objects.block1.shape, 5) -- A higher density gives it more mass.
 +
 
 +
  objects.block2 = {}
 +
  objects.block2.body = love.physics.newBody(world, 200, 400, "dynamic")
 +
  objects.block2.shape = love.physics.newRectangleShape(0, 0, 100, 50)
 +
  objects.block2.fixture = love.physics.newFixture(objects.block2.body, objects.block2.shape, 2)
 
</source>
 
</source>
  
Now to wrap up the love.load function, let's set up the screen size and background color.
+
Now to wrap up the [[love.load]]() function, let's set up the screen size and background color.
  
 
<source lang="lua">
 
<source lang="lua">
 
   --initial graphics setup
 
   --initial graphics setup
   love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
+
   love.graphics.setBackgroundColor(0.41, 0.53, 0.97) --set the background color to a nice blue
   love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
+
   love.window.setMode(650, 650) --set the window dimensions to 650 by 650 with no fullscreen, vsync on, and no antialiasing
 
end
 
end
 
</source>
 
</source>
  
 
Okay, that's enough for the initial set up of the physics engine.
 
Okay, that's enough for the initial set up of the physics engine.
Now we need do edit the love.update() function.
+
Now we need to edit the [[love.update]]() function.  
  
 
=== love.update() ===
 
=== love.update() ===
Line 49: Line 64:
 
   --here we are going to create some keyboard events
 
   --here we are going to create some keyboard events
 
   if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
 
   if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
     bodies[1]:applyForce(400, 0)
+
     objects.ball.body:applyForce(400, 0)
 
   elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
 
   elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
     bodies[1]:applyForce(-400, 0)
+
     objects.ball.body:applyForce(-400, 0)
 
   elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
 
   elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
     bodies[1]:setY(650/2)
+
     objects.ball.body:setPosition(650/2, 650/2)
 +
    objects.ball.body:setLinearVelocity(0, 0) --we must set the velocity to zero to prevent a potentially large velocity generated by the change in position
 
   end
 
   end
 
end
 
end
Line 61: Line 77:
  
 
=== love.draw() ===
 
=== love.draw() ===
 +
 +
First, the ground.
  
 
<source lang="lua">
 
<source lang="lua">
 
function love.draw()
 
function love.draw()
   local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox() --get the x,y coordinates of all 4 corners of the box.
+
   love.graphics.setColor(0.28, 0.63, 0.05) -- set the drawing color to green for the ground
 +
  love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
 
</source>
 
</source>
  
Here we grab the coordinates of the four corners of the box that represents the ground to be able to draw the ground. In the segment above...
+
And finally, we can draw the circle that represents the ball and the blocks.
  x1, y1 represent the bottom left corner of the bounding box
 
  x2, y2 represent the top left corner of the bounding box
 
  x3, y3 represent the top right corner of the bounding box
 
  x4, y4 represent the top right corner of the boudning box
 
  
Now we will do a little math to calculate the height and width of the box that represents the ground.
+
<source lang="lua">
 +
  love.graphics.setColor(0.76, 0.18, 0.05) --set the drawing color to red for the ball
 +
  love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())
  
<source lang="lua">
+
   love.graphics.setColor(0.20, 0.20, 0.20) -- set the drawing color to grey for the blocks
   local boxwidth = x3 - x2 --calculate the width of the box
+
  love.graphics.polygon("fill", objects.block1.body:getWorldPoints(objects.block1.shape:getPoints()))
   local boxheight = y2 - y1 --calculate the height of the box
+
   love.graphics.polygon("fill", objects.block2.body:getWorldPoints(objects.block2.shape:getPoints()))
 +
end
 
</source>
 
</source>
  
Pretty simple. Now that we are ready to draw the box for the ground, we must remember that love.graphics.rectangle() draws from the upper-left hand corner, and that the coordinates love.physics.Body() is anchored at the center. So we need to compensate so that the two will match up.
+
There you have it! Put this file in a [[zip|zip file]], rename it to physics.love (or whatever), run it. And you'll have a ball rolling around in a lush green environment like I promised.
 +
 
 +
[[Image:Tutorial-PhysicsBall.jpg|thumb|Screenshot of the mostly finished product.]]
 +
 
  
 +
== The main.lua ==
 
<source lang="lua">
 
<source lang="lua">
   love.graphics.setColor(72, 160, 14) --set the drawing color to green for the ground
+
function love.load()
   --the rectangle is drawing from the top left corner
+
  -- the height of a meter our worlds will be 64px
   --so we need to compensate for that
+
  love.physics.setMeter(64)
   love.graphics.rectangle("fill", bodies[0]:getX() - boxwidth/2, bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
+
  -- create a world for the bodies to exist in with horizontal gravity
</source>
+
   -- of 0 and vertical gravity of 9.81
 +
  world = love.physics.newWorld(0, 9.81*64, true)
 +
 
 +
  objects = {} -- table to hold all our physical objects
 +
 
 +
  -- let's create the ground
 +
  objects.ground = {}
 +
   -- remember, the shape (the rectangle we create next) anchors to the
 +
   -- body from its center, so we have to move it to (650/2, 650-50/2)
 +
   objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2)
 +
  -- make a rectangle with a width of 650 and a height of 50
 +
  objects.ground.shape = love.physics.newRectangleShape(650, 50)
 +
  -- attach shape to body
 +
  objects.ground.fixture = love.physics.newFixture(objects.ground.body,
 +
                                                  objects.ground.shape)
 +
 
 +
  -- let's create a ball
 +
  objects.ball = {}
 +
  -- place the body in the center of the world and make it dynamic, so
 +
  -- it can move around
 +
  objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic")
 +
  -- the ball's shape has a radius of 20
 +
  objects.ball.shape = love.physics.newCircleShape(20)
 +
  -- Attach fixture to body and give it a density of 1.
 +
  objects.ball.fixture = love.physics.newFixture(objects.ball.body,
 +
                                                objects.ball.shape, 1)
 +
  objects.ball.fixture:setRestitution(0.9) -- let the ball bounce
 +
 
 +
  -- let's create a couple blocks to play around with
 +
  objects.block1 = {}
 +
  objects.block1.body = love.physics.newBody(world, 200, 550, "dynamic")
 +
  objects.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
 +
  -- A higher density gives it more mass.
 +
  objects.block1.fixture = love.physics.newFixture(objects.block1.body,
 +
                                                  objects.block1.shape, 5)
 +
 
 +
  objects.block2 = {}
 +
  objects.block2.body = love.physics.newBody(world, 200, 400, "dynamic")
 +
  objects.block2.shape = love.physics.newRectangleShape(0, 0, 100, 50)
 +
  objects.block2.fixture = love.physics.newFixture(objects.block2.body,
 +
                                                  objects.block2.shape, 2)
 +
 
 +
  -- initial graphics setup
 +
  -- set the background color to a nice blue
 +
  love.graphics.setBackgroundColor(0.41, 0.53, 0.97)
 +
  love.window.setMode(650, 650) -- set the window dimensions to 650 by 650
 +
end
 +
 
  
And finally, we can draw the circle that represents the ball. Since love.graphics.circle() has coordinates that are anchored at the center of the circle, we do not need to compensate here like we did for the rectangle.
+
function love.update(dt)
 +
  world:update(dt) -- this puts the world into motion
  
<source lang="lua">
+
  -- here we are going to create some keyboard events
  love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
+
  -- press the right arrow key to push the ball to the right
   --the circle is drawing from the center
+
  if love.keyboard.isDown("right") then
   --so we do not need to compensate
+
    objects.ball.body:applyForce(400, 0)
   love.graphics.circle("fill", bodies[1]:getX(), bodies[1]:getY(), shapes[1]:getRadius(), 20)
+
  -- press the left arrow key to push the ball to the left
 +
   elseif love.keyboard.isDown("left") then
 +
    objects.ball.body:applyForce(-400, 0)
 +
   -- press the up arrow key to set the ball in the air
 +
   elseif love.keyboard.isDown("up") then
 +
    objects.ball.body:setPosition(650/2, 650/2)
 +
    -- we must set the velocity to zero to prevent a potentially large
 +
    -- velocity generated by the change in position
 +
    objects.ball.body:setLinearVelocity(0, 0)
 +
  end
 
end
 
end
</source>
 
  
There you have it! Put this file in a zip file, rename it to .love, run it. And you'll have a ball rolling around in a lush green environment like I promised.
+
function love.draw()
 +
  -- set the drawing color to green for the ground
 +
  love.graphics.setColor(0.28, 0.63, 0.05)
 +
  -- draw a "filled in" polygon using the ground's coordinates
 +
  love.graphics.polygon("fill", objects.ground.body:getWorldPoints(
 +
                          objects.ground.shape:getPoints()))
 +
 
 +
  -- set the drawing color to red for the ball
 +
  love.graphics.setColor(0.76, 0.18, 0.05)
 +
  love.graphics.circle("fill", objects.ball.body:getX(),
 +
                      objects.ball.body:getY(), objects.ball.shape:getRadius())
  
 +
  -- set the drawing color to grey for the blocks
 +
  love.graphics.setColor(0.20, 0.20, 0.20)
 +
  love.graphics.polygon("fill", objects.block1.body:getWorldPoints(
 +
                          objects.block1.shape:getPoints()))
 +
  love.graphics.polygon("fill", objects.block2.body:getWorldPoints(
 +
                          objects.block2.shape:getPoints()))
 +
end
 +
</source>
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
  
{{#set:LOVE Version=0.6.1}}
+
{{#set:LOVE Version=0.10.0}}
 
{{#set:Description=A tutorial for learning to use [[love.physics]].}}
 
{{#set:Description=A tutorial for learning to use [[love.physics]].}}
 +
 +
== Other languages ==
 +
{{i18n|Tutorial:Physics}}

Latest revision as of 12:19, 25 September 2019

In this example we will create a red ball that rolls around on a green ground.

The finished example is at the end of this page. All of these functions may be placed in one file: main.lua

We'll start in the love.load() function.

love.load()

First we need to set up a world for the physics bodies to exist in.

function love.load()
  love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81

Now that a world has been created, we can add bodies, shapes, and fixture to it.

   objects = {} -- table to hold all our physical objects
  
  --let's create the ground
  objects.ground = {}
  objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2)
  objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a rectangle with a width of 650 and a height of 50
  objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape) --attach shape to body
  
  --let's create a ball
  objects.ball = {}
  objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
  objects.ball.shape = love.physics.newCircleShape( 20) --the ball's shape has a radius of 20
  objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.
  objects.ball.fixture:setRestitution(0.9) --let the ball bounce

  --let's create a couple blocks to play around with
  objects.block1 = {}
  objects.block1.body = love.physics.newBody(world, 200, 550, "dynamic")
  objects.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
  objects.block1.fixture = love.physics.newFixture(objects.block1.body, objects.block1.shape, 5) -- A higher density gives it more mass.

  objects.block2 = {}
  objects.block2.body = love.physics.newBody(world, 200, 400, "dynamic")
  objects.block2.shape = love.physics.newRectangleShape(0, 0, 100, 50)
  objects.block2.fixture = love.physics.newFixture(objects.block2.body, objects.block2.shape, 2)

Now to wrap up the love.load() function, let's set up the screen size and background color.

  --initial graphics setup
  love.graphics.setBackgroundColor(0.41, 0.53, 0.97) --set the background color to a nice blue
  love.window.setMode(650, 650) --set the window dimensions to 650 by 650 with no fullscreen, vsync on, and no antialiasing
end

Okay, that's enough for the initial set up of the physics engine. Now we need to edit the love.update() function.

love.update()

function love.update(dt)
  world:update(dt) --this puts the world into motion
  
  --here we are going to create some keyboard events
  if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
    objects.ball.body:applyForce(400, 0)
  elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
    objects.ball.body:applyForce(-400, 0)
  elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
    objects.ball.body:setPosition(650/2, 650/2)
    objects.ball.body:setLinearVelocity(0, 0) --we must set the velocity to zero to prevent a potentially large velocity generated by the change in position
  end
end

Now that the world is updating, we can draw the ground and ball.

love.draw()

First, the ground.

function love.draw()
  love.graphics.setColor(0.28, 0.63, 0.05) -- set the drawing color to green for the ground
  love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates

And finally, we can draw the circle that represents the ball and the blocks.

  love.graphics.setColor(0.76, 0.18, 0.05) --set the drawing color to red for the ball
  love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())

  love.graphics.setColor(0.20, 0.20, 0.20) -- set the drawing color to grey for the blocks
  love.graphics.polygon("fill", objects.block1.body:getWorldPoints(objects.block1.shape:getPoints()))
  love.graphics.polygon("fill", objects.block2.body:getWorldPoints(objects.block2.shape:getPoints()))
end

There you have it! Put this file in a zip file, rename it to physics.love (or whatever), run it. And you'll have a ball rolling around in a lush green environment like I promised.

Screenshot of the mostly finished product.


The main.lua

function love.load()
  -- the height of a meter our worlds will be 64px
  love.physics.setMeter(64)
  -- create a world for the bodies to exist in with horizontal gravity
  -- of 0 and vertical gravity of 9.81
  world = love.physics.newWorld(0, 9.81*64, true)

  objects = {} -- table to hold all our physical objects

  -- let's create the ground
  objects.ground = {}
  -- remember, the shape (the rectangle we create next) anchors to the
  -- body from its center, so we have to move it to (650/2, 650-50/2)
  objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2)
  -- make a rectangle with a width of 650 and a height of 50
  objects.ground.shape = love.physics.newRectangleShape(650, 50)
  -- attach shape to body
  objects.ground.fixture = love.physics.newFixture(objects.ground.body,
                                                   objects.ground.shape)

  -- let's create a ball
  objects.ball = {}
  -- place the body in the center of the world and make it dynamic, so
  -- it can move around
  objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic")
  -- the ball's shape has a radius of 20
  objects.ball.shape = love.physics.newCircleShape(20)
  -- Attach fixture to body and give it a density of 1.
  objects.ball.fixture = love.physics.newFixture(objects.ball.body,
                                                 objects.ball.shape, 1)
  objects.ball.fixture:setRestitution(0.9) -- let the ball bounce

  -- let's create a couple blocks to play around with
  objects.block1 = {}
  objects.block1.body = love.physics.newBody(world, 200, 550, "dynamic")
  objects.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
  -- A higher density gives it more mass.
  objects.block1.fixture = love.physics.newFixture(objects.block1.body,
                                                   objects.block1.shape, 5)

  objects.block2 = {}
  objects.block2.body = love.physics.newBody(world, 200, 400, "dynamic")
  objects.block2.shape = love.physics.newRectangleShape(0, 0, 100, 50)
  objects.block2.fixture = love.physics.newFixture(objects.block2.body,
                                                   objects.block2.shape, 2)

  -- initial graphics setup
  -- set the background color to a nice blue
  love.graphics.setBackgroundColor(0.41, 0.53, 0.97)
  love.window.setMode(650, 650) -- set the window dimensions to 650 by 650
end


function love.update(dt)
  world:update(dt) -- this puts the world into motion

  -- here we are going to create some keyboard events
  -- press the right arrow key to push the ball to the right
  if love.keyboard.isDown("right") then
    objects.ball.body:applyForce(400, 0)
  -- press the left arrow key to push the ball to the left
  elseif love.keyboard.isDown("left") then
    objects.ball.body:applyForce(-400, 0)
  -- press the up arrow key to set the ball in the air
  elseif love.keyboard.isDown("up") then
    objects.ball.body:setPosition(650/2, 650/2)
    -- we must set the velocity to zero to prevent a potentially large
    -- velocity generated by the change in position
    objects.ball.body:setLinearVelocity(0, 0)
  end
end

function love.draw()
  -- set the drawing color to green for the ground
  love.graphics.setColor(0.28, 0.63, 0.05)
  -- draw a "filled in" polygon using the ground's coordinates
  love.graphics.polygon("fill", objects.ground.body:getWorldPoints(
                           objects.ground.shape:getPoints()))

  -- set the drawing color to red for the ball
  love.graphics.setColor(0.76, 0.18, 0.05)
  love.graphics.circle("fill", objects.ball.body:getX(),
                       objects.ball.body:getY(), objects.ball.shape:getRadius())

  -- set the drawing color to grey for the blocks
  love.graphics.setColor(0.20, 0.20, 0.20)
  love.graphics.polygon("fill", objects.block1.body:getWorldPoints(
                           objects.block1.shape:getPoints()))
  love.graphics.polygon("fill", objects.block2.body:getWorldPoints(
                           objects.block2.shape:getPoints()))
end



Other languages