tentus wrote:it hadn't really sunk in how different it all was until I started trying to make the physics tutorial work again.
It's not really that bad, here's my take on it:
Code: Select all
function love.load()
world = love.physics.newWorld(0, 700,true,64) --the x component of the gravity will be 0, and the y component of the gravity will be 700, sleeping is allowed, and the height of a meter in this world will be 64px
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) --remember, the shape anchors to the body from its center
objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a shape with a width of 650 and a height of 50
objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape, 0) --anchor the shape to the body, give it a density of 0 (infinite)
--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 screen, it must be declared dynamic or it will be assumed to be immovable
objects.ball.shape = love.physics.newCircleShape( 20) --the ball's shape has no offset from it's body and has a radius of 20
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 5 ) --the ball needs to be linked to the body and given a density
objects.ball.body:resetMassData() --this automatically recalculates the mass based on the shape and density values
--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 with no fullscreen, vsync on, and no antialiasing
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:setY(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 shape points, offset by the ground's body position
love.graphics.setColor(193, 47, 14) --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(), 20) -- we want 20 line segments to form the "circle"
end
for most projects it's a fairly small change, the biggest difference is more things are explicitly defined (which is a good thing). Heres some of the changes that will effect most projects:
-newWorld now looks like this: newWorld(gravX, gravY, allowsleep, setMeter).
-When creating a body, you must declare it "dynamic" or it will be static, use "kinematic" if the object is to be manually moved only (like a elevator or moving platform).
-When defining a shape, don't include a body parameter, and declare coordinates clockwise instead of counter-clockwise, otherwise this is the same.
-After making a shape, use a fixture to attach it to a body, and many of the former shape properties are now held by the fixture (density, masking, friction, restitution, etc.) so make sure you're using them with the right component.
If anyone knows how to use the raytracing from within Löve an example would be cool. I'd like to start playing with it as it seems like it would be a nice performance boost over the old testSegment.