love.physics does not fall the boxes

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
kbups
Prole
Posts: 3
Joined: Mon Feb 20, 2012 2:21 pm

love.physics does not fall the boxes

Post by kbups »

Hello,

Do you know why the following code, that uses love.physics, does not fall the rectangular boxes?

Thanks in advance for your reply!

Code: Select all

function love.load()
    world = love.physics.newWorld(0, 0, 650, 650) --create a world for the bodies to exist in with width and height of 650
    world:setGravity(0, 10) --the x component of the gravity will be 0, and the y component of the gravity will be 700
    world:setMeter(64) --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 = {}
    --we need to give the ground a mass of zero so that the ground wont move
    objects.ground.body = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the shape anchors to the body from its center
    objects.ground.shape = love.physics.newRectangleShape(objects.ground.body, 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50
    objects.ground.shape:setFriction(0.5)
    objects.ground.shape:setDensity(1)
    objects.ground.shape:setRestitution(0.2)
    objects.ground.shape:setData("Ground")
    
    print("Is Ground dynamic? "..tostring(objects.ground.body:isDynamic()).." / istatic: "..tostring(objects.ground.body:isStatic()))

    --let's create a ball
    objects.ball = {}
    objects.ball.body = love.physics.newBody(world, 650/2, 650/2, 30, 0) --place the body in the center of the world, with a mass of 15
    objects.ball.shape = love.physics.newCircleShape(objects.ball.body, 0, 0, 20) --the ball's shape has no offset from it's body and has a radius of 20
    objects.ball.shape:setFriction(0.5)
    objects.ball.shape:setDensity(1)
    objects.ball.shape:setRestitution(0.2)
    objects.ball.shape:setData("Ball")
    
    objects.boxes = {}
    for i=1,10 do
        objects.boxes[i] = {}
        objects.boxes[i].body  = love.physics.newBody(world, 60 * i, 400, 8, 0)
        objects.boxes[i].shape = love.physics.newRectangleShape(objects.boxes[i].body, 0, 0, 5, 40)
        objects.boxes[i].shape:setFriction(0.2)
        objects.boxes[i].shape:setDensity(1)
        objects.boxes[i].shape:setRestitution(0.4)
        objects.boxes[i].shape:setData("Block")
        
        print("Is Box #"..i.." dynamic? "..tostring(objects.boxes[i].body:isDynamic()).." / istatic: "..tostring(objects.boxes[i].body:isStatic()))
    end    
    
    --let's create a ball
    objects.ball1 = {}
    objects.ball1.body = love.physics.newBody(world, 310, 50, 30, 0) --place the body in the center of the world, with a mass of 15
    objects.ball1.shape = love.physics.newCircleShape(objects.ball1.body, 0, 0, 20) --the ball's shape has no offset from it's body and has a radius of 20
    objects.ball1.shape:setFriction(0.2)
    objects.ball1.shape:setDensity(1)
    objects.ball1.shape:setRestitution(1)
    objects.ball1.shape:setData("Ball")

    --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
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.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.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius(), 20) -- we want 20 line segments to form the "circle"
  
  love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
  love.graphics.circle("fill", objects.ball1.body:getX(), objects.ball1.body:getY(), objects.ball1.shape:getRadius(), 20) -- we want 20 line segments to form the "circle"
  
  love.graphics.setColor(100, 100, 100) --set the drawing color for the box
  for i=1,10 do
    love.graphics.polygon("fill", objects.boxes[i].shape:getPoints())
  end
end
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: love.physics does not fall the boxes

Post by tentus »

The gray boxes fall when I run it.
Kurosuke needs beta testers
kbups
Prole
Posts: 3
Joined: Mon Feb 20, 2012 2:21 pm

Re: love.physics does not fall the boxes

Post by kbups »

Sorry, yes the boxes fall on the ground but they do not fall down when the ball collides with some of them (like a domino for example)

Do you have an idea why?

Thanks in advance for you reply.

PS: In fact, I am not able to reproduce the same behavior as the live demo located on this page: http://code.google.com/p/box2dweb/
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: love.physics does not fall the boxes

Post by tentus »

Ah, I gotcha.

I'm pretty sure it's because the last parameter in love.physics.newBody is rotational inertia, which you explicitly set to 0. Change it to 1 and all your smaller rectangles will fall over.
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 12 guests