[Solved] Loss of energy?

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
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

[Solved] Loss of energy?

Post by pgimeno »

To train myself in love.physics, I've made a restricted version of an old (93-ish) DOS toy called Impact. It works, but the problem I'm having is that the system seems to keep losing energy slowly, until eventually everything comes to a stop. Is that a known limitation of the engine?

Here's my main.lua (g to enable/disable gravity):

Code: Select all

lg = love.graphics
lp = love.physics
le = love.event
lma = love.math

local world
local shapes

local function newShape(n, x, y, r, ang)
  local pts = {}
  local a
  for i = 1, n do
    a = (i-1)/n*math.pi*2
    pts[i*2-1] = math.sin(a)*r
    pts[i*2] = math.cos(a)*r
  end
  local shape = lp.newPolygonShape(unpack(pts))
  local body = lp.newBody(world, 0, 0, "dynamic")
  local fixture = lp.newFixture(body, shape)
  body:setPosition(x, y)
  body:setAngle(ang)
  body:setLinearDamping(0)
  fixture:setRestitution(1)
  fixture:setFriction(0.001)
--  local colour = { lma.random(64, 255), lma.random(64, 255), lma.random(64, 255) }
  local colour = {
    math.sqrt(lma.random())*255,
    math.sqrt(lma.random())*255,
    math.sqrt(lma.random())*255,
  }
  return { body = body, shape = shape, fixture = fixture, colour = colour }
end

function love.load(cmdlineargs)
  lp.setMeter(10)
  world = lp.newWorld(0, 100, false)

  shapes = {}
  local r
  for i = 1, 15 do
    r = lma.random()*20+30
    shapes[i] = newShape(lma.random(3, 6),
      lma.random()*(lg.getWidth()-(r+r)-8)+r+4,
      lma.random()*(lg.getHeight()-(r+r)-8)+r+4,
      r,
      lma.random()*math.pi*2)
    shapes[i].body:setLinearVelocity(lma.random()*300-150, lma.random()*300-150)
  end

  -- Make a box

  local w, h = lg.getDimensions()

  local body = lp.newBody(world, w/2, h/2)
  lp.newFixture(body, lp.newEdgeShape(4-w, 4-h/2, w-4, 4-h/2))
  lp.newFixture(body, lp.newEdgeShape(4-w, h/2-4, w-4, h/2-4))
  lp.newFixture(body, lp.newEdgeShape(4-w/2, 4-h, 4-w/2, h-4))
  lp.newFixture(body, lp.newEdgeShape(w/2-4, 4-h, w/2-4, h-4))
end

function love.update(dt)
  world:update(dt)
end

function love.draw()
  lg.setColor(255,255,255)
  lg.rectangle("line", 4, 4, lg.getWidth()-8, lg.getHeight()-8)
  local pts
  for k, v in ipairs(shapes) do
    lg.setColor(unpack(v.colour))
    lg.polygon("fill", v.body:getWorldPoints(v.shape:getPoints()))
  end
end

function love.keypressed(k, r)
  if k == "escape" then le.quit() end
  if k == "g" then
    local x,y = world:getGravity()
    world:setGravity(x, 100-y)
  end
end
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Loss of energy?

Post by pgimeno »

Operator error, sorry :oops:

This line was the culprit:

Code: Select all

  fixture:setFriction(0.001)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 147 guests