love.physics in 0.8.0 (and its API documentation)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
pockpock
Prole
Posts: 2
Joined: Sun Apr 01, 2012 10:47 pm

love.physics in 0.8.0 (and its API documentation)

Post 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
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

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

Post 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
Shallow indentations.
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

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

Post 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.
pockpock
Prole
Posts: 2
Joined: Sun Apr 01, 2012 10:47 pm

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

Post by pockpock »

Thanks the code helped a lot and the list will be usefull. Have you thought putting it into the wiki article?
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

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

Post 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.
asmageddon
Prole
Posts: 13
Joined: Mon Apr 09, 2012 1:39 pm

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

Post 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())
User avatar
juno
Citizen
Posts: 85
Joined: Thu May 10, 2012 4:32 pm
Location: London

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

Post 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
wat ya mean she's in another castle!?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 3 guests