# Tutorial:Physics (简体中文)

## Contents

```function love.load()
love.physics.setMeter(64) --我们这个世界里的一米等于64像素。
world = love.physics.newWorld(0, 9.81*64, true) --这个世界的重力方向是水平方向0、垂直方向9.81。
```

```   objects = {} -- 一个容纳所有我们物理对象的列表。table to hold all our physical objects

--创建地面
objects.ground = {}
objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2) --记住，形状(我们接下来要创建的矩形)在物体上的锚点是从其中心开始计算的，所以我们要把它移动到(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) --画一个宽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

--创建小球
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) --小球半径为20
--the ball's shape has a radius of 20
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- 把形状和物体组合起来，并设置其密度为1
-- Attach fixture to body and give it a density of 1.
objects.ball.fixture:setRestitution(0.9) --让球会弹跳--let the ball bounce

--创建几个场景中的方块
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)
```

```  --初始图形设置
love.graphics.setBackgroundColor(104, 136, 248) --背景色设置成漂亮的蓝色--set the background color to a nice blue
love.graphics.setMode(650, 650, false, true, 0) --窗口大小设置成650x650，不全屏，垂直同步开启，无抗锯齿。--set the window dimensions to 650 by 650 with no fullscreen, vsync on, and no antialiasing
end
```

OK，初始化这个物理引擎的部分完成了，我们再来写love.update()方法。

### love.update()

```function love.update(dt)
world:update(dt) --让世界动起来--this puts the world into motion

--加入一些键盘事件
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)
end
end
```

### love.draw()

```function love.draw()
love.graphics.setColor(72, 160, 14) -- 地面画成绿色的-- 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
```

```  love.graphics.setColor(193, 47, 14) --小球画成红色的--set the drawing color to red for the ball

love.graphics.setColor(50, 50, 50) -- 方块画成灰色的 -- 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
```

## The main.lua

```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

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)

--initial graphics setup
love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
love.graphics.setMode(650, 650, false, true, 0) --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
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)
end
end

function love.draw()
love.graphics.setColor(72, 160, 14) -- 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

love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball