Game Development Reference
In-Depth Information
First we need to set up the physics world, in which we define its boundaries. A good place to start is
to use the screen dimensions for the world.
world = love.physics.newWorld(0,0,_W,_H)
Note In LÖVE 0.8.0, the function love.physics.newWorld has been changed to take the x, y components
of gravity and if the bodies are allowed to sleep or not: love.physics.newWorld( xg, yg, sleep ) .
Next we set the scaling factor of the bodies (something that is not possible in Corona SDK, which
gives it some bugs regarding physics bodies):
world:setMeter( number )
All the coordinates in this physics world are divided by this number value. This is a convenient way to
draw on the screen without the need for graphic transformations.
Next we create a physics body:
local body = love.physics.newBody(world, 10, 10, 0, 0)
The parameters that we pass are the world object, the x- and y-coordinates, the mass of the object,
and the rotational inertia of the object.
The physics world does not automatically update itself; it is up to us to do the updating. We can
ensure that it updates via the love.update method, as follows:
function love.update(dt)
world:update (dt)
end
Note With LÖVE 0.8.0, this function has been modified, and it does not take the mass and inertia.
Instead it takes a string that defines the body type, which can be static, dynamic, or kinematic and is
defined as love.physics.newBody( world, x, y, type ) .
After having created the body, we need to give it a shape. This is the bounding box on the physics
object, which can be a rectangle, circle, polygon, edge shape, or chain shape. An edge shape is a
line segment, while a chain shape is made up of multiple line segments.
local bodyshape = local.physics.newRectangleShape(body, 0, 0, 20, 40, 0)
This code gives the body a shape that is a rectangle starting at 0, 0 (relative to the body) that is 20
units in width and 40 units in height.
Search WWH ::




Custom Search