Page 1 of 1

love.physics in 0.8.0 (and its API documentation)

Posted: Mon Apr 02, 2012 12:16 am
by pockpock
Hi
I'm having a hard time using the physics library. I started from the example in tutorial and I soon figured that the API has changed. The version I use is 0.8.0 (I think as I just cloned the repository). I looked at löves sourcecode in order to change my function calls and I wrote this:

Code: Select all

function love.load()
    love.graphics.setMode( 800, 600, false, false, 0 )
    
    love.physics.setMeter(48)
    -- gravity x,y and sleep
    world = love.physics.newWorld(0,9.81*48,true)
    
    ball={}
    --a simple body in the middle of the screen
    ball.b = love.physics.newBody(world, 400, 300, 15, 15)
    --here in 0.7.2 you'd have to specify the body the shape gets attached to
    ball.s = love.physics.newCircleShape(0,0,20) 
end

function love.update(dt)
    world:update(dt)
    print(ball.b:getY())
end
As expected it didn't work. The body remained at y=300. Compared to the tutorial it lacks the information of the worlds size and that the shape shoud be attached to the body. I couldn't figure out how to do it by looking at the source code. Is there a more complete documentation of 0.8.0? Did I mess up something by building the binary myself? Should I even use 0.8.0 for now?

Best regards

Re: love.physics in 0.8.0 (and its API documentation)

Posted: Mon Apr 02, 2012 11:13 am
by Boolsheet
The physics module has indeed changed a lot from 0.7.2. The update to Box2D 2.2.1 required a different approach, because the shapes are now used by fixtures instead of bodies. If you haven't already, download the Box2D 2.2.0 manual, it will explain how most of it works.

It's not easy to understand the new API with just the source code. Hopefully, 0.8.0 will be released soon.

Here's a working example:

Code: Select all

function love.load()
	love.graphics.setMode( 800, 600, false, false, 0 )

	love.physics.setMeter(48)
	-- gravity x,y and sleep
	world = love.physics.newWorld(0,9.81*48,true)

	ball={}
	-- A simple body in the middle of the screen with the type dynamic (default is static).
	ball.b = love.physics.newBody(world, 400, 300, "dynamic")
	-- Create a new shape that can be used as a template.
	ball.s = love.physics.newCircleShape(0, 0, 20)
	-- Create a new fixture attached to the body with the circleshape template as its shape. Third argument is the density.
	ball.f = love.physics.newFixture(ball.b, ball.s, 0.1)
	-- Let's bounce!
	ball.f:setRestitution(0.9)

	-- Making a floor.
	floor = {}
	floor.b = love.physics.newBody(world, 400, 500)
	floor.s = love.physics.newEdgeShape(-400, 0, 400, 0)
	floor.f = love.physics.newFixture(floor.b, floor.s)
end

function love.update(dt)
	world:update(dt)
	print(ball.b:getY())
end

function love.draw()
	love.graphics.circle("fill", ball.b:getX(), ball.b:getY(), 20)
	love.graphics.line(floor.b:getWorldPoints(floor.s:getPoints()))
end

Re: love.physics in 0.8.0 (and its API documentation)

Posted: Mon Apr 02, 2012 8:06 pm
by The Burrito
Apart from the major API changes, there are several functions that have been renamed, here's my notes from updating some of my stuff to 0.8.0:
  • -world is now defined as (gravityx,gravityy,allowsleep)
    -shapes no longer have a body param
    -creating a fixture references a shape and body
    -almost all functions associated with shapes are now applied to fixtures
    -bodies should be declared "dynamic" or "static" as needed
    -getBoundingBox only returns 2 coords (AABB)
    -world:setMeter is now love.physics.setMeter
    -setMeter now scales forces too
    -setMassFromShapes is now resetMassData
    -setData is now setUserData
    -to draw physics bodies use body:getWorldPoints(shape:getPoints())
Thats definitely not everything, but it covers pretty much all the obvious changes I came across.

Re: love.physics in 0.8.0 (and its API documentation)

Posted: Tue Apr 03, 2012 4:19 pm
by pockpock
Thanks the code helped a lot and the list will be usefull. Have you thought putting it into the wiki article?

Re: love.physics in 0.8.0 (and its API documentation)

Posted: Tue Apr 03, 2012 10:09 pm
by The Burrito
I'll probably write up something more organized for the wiki once 0.8.0 is officially done, there's still a possibility of things changing before then.

Re: love.physics in 0.8.0 (and its API documentation)

Posted: Tue Apr 10, 2012 10:33 am
by asmageddon
And how can I render other shapes? The following code as taken from tutorial renders the polygon at (0,0):

Code: Select all

love.graphics.polygon("fill", objects.ground.shape:getPoints())

Re: love.physics in 0.8.0 (and its API documentation)

Posted: Thu May 10, 2012 4:43 pm
by juno
Before this release, the setMassFromShapes() function would allow boxes/balls to tumble or roll. 0.8.0 seems to enable this tumbling effect automatically. Do you know how to stop it? I need the objects to have 0 rotational inertia when they collide