Help with setRestitution -- weird bugs?

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.
Clash
Prole
Posts: 6
Joined: Wed Jun 01, 2011 6:08 pm

Help with setRestitution -- weird bugs?

Post by Clash »

So, I just followed the physics tutorial: http://love2d.org/wiki/Tutorial:Physics

I have three questions

1. Why does the ball bounce when it touches the ground if the default restitution is 0?

2. The code says

Code: Select all

  --let's create a ball
  bodies[1] = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
  shapes[1] = love.physics.newCircleShape(bodies[1], 0, 0, 20) --the ball has a radius of 20
But the ball appears from the top center of the screen. Actually at the beginning the ball isn't even showing up and I don't know why either.

3. Why does setting restitution of the ball shape to a value bigger than 0.2 results in the ball not appearing in the screen at all. (I believe this is related to #2)

Thanks in advance!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Help with setRestitution -- weird bugs?

Post by kikito »

Please upload a sample .love file. Otherwise it's very difficult to test exactly what is wrong.
When I write def I mean function.
Clash
Prole
Posts: 6
Joined: Wed Jun 01, 2011 6:08 pm

Re: Help with setRestitution -- weird bugs?

Post by Clash »

I didn't change anything in the tutorial, this is the exact code I'm using

Code: Select all

function love.load()
  world = love.physics.newWorld(-650, -650, 650, 650) --create a world for the bodies to exist in with width and height of 650
  world:setGravity(0, 700) --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
  
  bodies = {} --create tables for the bodies and shapes so that the garbage collector doesn't delete them
  shapes = {}
  
  --let's create the ground
  --we need to give the ground a mass of zero so that the ground wont move
  bodies[0] = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the body anchors from the center of the shape
  shapes[0] = love.physics.newRectangleShape(bodies[0], 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50
  
  --let's create a ball
  bodies[1] = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
  shapes[1] = love.physics.newCircleShape(bodies[1], 0, 0, 20) --the ball has a radius of 20

  --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
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
    bodies[1]:applyForce(400, 0)
  elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
    bodies[1]:applyForce(-400, 0)
  elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
    bodies[1]:setY(650/2)
  end
end

function love.draw()
  local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox() --get the x,y coordinates of all 4 corners of the box.
  --x1, y1 represent the bottom left corner of the bounding box
  --x2, y2 represent the top left corner of the bounding box
  --x3, y3 represent the top right corner of the bounding box
  --x4, y4 represent the top right corner of the boudning box
  local boxwidth = x3 - x2 --calculate the width of the box
  local boxheight = y2 - y1 --calculate the height of the box
  love.graphics.setColor(72, 160, 14) --set the drawing color to green for the ground
  --the rectangle is drawing from the top left corner
  --so we need to compensate for that
  love.graphics.rectangle("fill", bodies[0]:getX() - boxwidth/2, bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
  love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
  --the circle is drawing from the center
  --so we do not need to compensate
  love.graphics.circle("fill", bodies[1]:getX(), bodies[1]:getY(), shapes[1]:getRadius(), 20)
end
To test #3, just add at love.load()

Code: Select all

shapes[1]:setRestitution(0.3);
and the circle won't even appear

Thanks in advance!
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Help with setRestitution -- weird bugs?

Post by vrld »

I cannot reproduce bug 2 and 3. As for those bugs, try to cap dt in love.update, like so:

Code: Select all

function love.update(dt)
    dt = math.min(dt, 1 / 30)
    ...
end
dt can become very big in the first frame, which will in turn update the physics system with a way to big timestep.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Clash
Prole
Posts: 6
Joined: Wed Jun 01, 2011 6:08 pm

Re: Help with setRestitution -- weird bugs?

Post by Clash »

vrld wrote:I cannot reproduce bug 2 and 3. As for those bugs, try to cap dt in love.update, like so:

Code: Select all

function love.update(dt)
    dt = math.min(dt, 1 / 30)
    ...
end
dt can become very big in the first frame, which will in turn update the physics system with a way to big timestep.
Thanks! That did fix #2 and #3

Any idea why #1 happens? I also noticed something weird, when setting the restitution to high values like 0.9, when the ball is near the ground, it bounces a million times before stopping, very weird! I believe this is related with #1. Any idea?

Thanks in advance!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with setRestitution -- weird bugs?

Post by Robin »

#1 is because the default restitution is not zero, it seems to be 0.1. So when you set it to 0.9, it bounces back nine times as strong as normally.
Help us help you: attach a .love.
Clash
Prole
Posts: 6
Joined: Wed Jun 01, 2011 6:08 pm

Re: Help with setRestitution -- weird bugs?

Post by Clash »

Robin wrote:#1 is because the default restitution is not zero, it seems to be 0.1. So when you set it to 0.9, it bounces back nine times as strong as normally.
If I set it to 0 the same thing happens. I believe the default is 0.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Help with setRestitution -- weird bugs?

Post by BlackBulletIV »

Make sure inertia is 0. That's just off the top of my head, but I think inertia would probably make a difference.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with setRestitution -- weird bugs?

Post by Robin »

Clash wrote:I believe the default is 0.
I tested, the default is 0.1.

I also tried your code, and after limiting dt, I found no problem. It didn't bounce when I set restitution to 0 and it didn't fly off the world for 0.9.
Help us help you: attach a .love.
Clash
Prole
Posts: 6
Joined: Wed Jun 01, 2011 6:08 pm

Re: Help with setRestitution -- weird bugs?

Post by Clash »

Robin wrote:
Clash wrote:I believe the default is 0.
I tested, the default is 0.1.

I also tried your code, and after limiting dt, I found no problem. It didn't bounce when I set restitution to 0 and it didn't fly off the world for 0.9.
You're right, the default is 0.1.
About 0.9, I didn't say it flies off the world, it just bounces a million times close to the ground before stopping. Could you please try? Thanks for the help!

Edit: Oh about flying off the world, you're right, when limitting dt that fixed it. But it still bounces a million times on the ground
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests