Strange lag

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
Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Strange lag

Post by Ford_Prefect »

Hi guys,

I am working on a small game with a friend.

First, heres the code:

Code: Select all

--------------------------------------------------------------------------------
-------------------------------LOVE.LOAD----------------------------------------
--------------------------------------------------------------------------------

function love.load()
lphys = love.physics
lgraph = love.graphics

----------------------------BASIC PARAMETERS------------------------------------
dt = 0.02
m = 2
ld = 2
i = 0
g = 0
gegnerrate = 25
schussrate = 0
cann = lgraph.newImage("cann.png")

welt = lphys.newWorld(0, 0, true)
lphys.setMeter(20)

------------------------------CREATING TABLES-----------------------------------
player = {}
enemy = {}
bullet = {}

enemy.testenemy = {}
-----------------------ATTACHING SHAPES AND BODYS-------------------------------
player.shape = lphys.newRectangleShape(30, 10)

player.body = lphys.newBody(welt, 200, 200, "dynamic")

player.body:setLinearDamping(ld)

player.fixture = lphys.newFixture(player.body, player.shape, m)
end

--------------------------------------------------------------------------------
-----------------------------LOVE.UPDATE----------------------------------------
--------------------------------------------------------------------------------


function love.update(dt)
welt:update(dt)
fps = love.timer.getFPS()

------------------------PLAYER INTERACTION--------------------------------------
if love.keyboard.isDown("w") then
	player.body:applyLinearImpulse(0, -15)
end

if love.keyboard.isDown("s") then
	player.body:applyLinearImpulse(0, 15)
end
if love.keyboard.isDown("d") then
	player.body:applyLinearImpulse(10, 0)
end
if love.keyboard.isDown("a") then
	player.body:applyLinearImpulse(-10, 0)
end
--[[
-----------------------GENERATING ENEMIES----------------------------------------
if gegnerrate == 0 then
	table.insert(enemy.testenemy, g)

	enemy[g].shape = lphys.newRectangleShape(5, 5)
	enemy[g].body = lphys.newBody(welt, 650, math.random(800), "dynamic")
	enemy[g].fixture = lphys.newFixture(enemy.testenemy[g].body, enemy.testenemy[g].shape, 1)
	--enemy[g].typ = "testenemy"
end

if gegnerrate > 0 then
	gegnerrate = gegnerrate - 1
end
]]

-----------------------GENERATING BULLETS---------------------------------------
if love.mouse.isDown("l") and schussrate == 0 then
	shoot = true
	i = i + 1
	x = player.body:getX()
	y = player.body:getY()

	deltax = x - love.mouse.getX()
	deltay = y - love.mouse.getY()
	m = deltay/deltax
	rad = math.atan(m)

	table.insert(bullet, i)
	bullet[i] = {}

	bullet[i].body = lphys.newBody(welt, x + 20, y, "dynamic")
	bullet[i].shape = lphys.newRectangleShape(5, 1)
	bullet[i].fixture = lphys.newFixture(bullet[i].body, bullet[i].shape, 1)
	bullet[i].ttl = 100

	rad = math.atan(m)
	bullet[i].body:applyForce(math.cos(rad)*1000, 0)
	bullet[i].body:applyForce(0, math.sin(rad)*1000)
	bullet[i].body:setAngle(rad)
	bullet[i].body:setBullet(true)

	schussrate = 5
end
if schussrate > 0 then
	schussrate = schussrate - 1
end

-------------------------------STUFF--------------------------------------------
i = #bullet
player.body:setAngle(0)
love.draw()
end


--------------------------------------------------------------------------------
-------------------------------------LOVE.DRAW----------------------------------
--------------------------------------------------------------------------------


function love.draw()
lgraph.polygon("fill", player.body:getWorldPoints(player.shape:getPoints()))
if shoot == true then
	for z, b in pairs(bullet) do
	--for z = 1, i do
		lgraph.polygon("fill", bullet[z].body:getWorldPoints(bullet[z].shape:getPoints()))

		if bullet[z].ttl < 0 then
			bullet[z].body:destroy()
			table.remove(bullet, z)
			i = i - 1
		else
			bullet[z].ttl = bullet[z].ttl - 1
		end
	end
end
--[[
for z, b in pairs(enemy.testenemy) do
	lgraph.polygon("fill", enemy.testenemy[z].body:getWorldPoints(enemy.testenemy[z].shape:getPoints()))
end]]

lgraph.draw(cann, player.body:getX() + 15, player.body:getY() - 1, rad, 2, 2, 0, 1)

lgraph.print(fps, 5, 5)
lgraph.print(schussrate, 5, 30)
lgraph.print("Schuss Nummer:", 5, 45)
lgraph.print(i, 120, 45)
end






--[[
if bullets[i][t] = 0 then
	bullets[i]:destroy
bullets[i][t] = ttl

 rad = math.atan(m)
   dy = math.sin(rad)*10
   dx = math.cos(rad)*10

   table.insert(bullet, i)

]]
Sorry if some variables are german.

On my computer, everything works as it should: By clicking i get a nice line of fire, and i can move the Rectangel with wasd. On my friends computer, which isnt as powerful as mine, theres a strange lag appearing after some seconds off firing. Its going well for half a minute, but then its lagging, as if the computer is trying to load something big.
Normally, the shots are flying straight towards the mouse pointer. But on his computer, they are spreading crazily after lag or short after loading the game.

We are not able to fix this.
Any suggestions? Shouldnt we use physics? Is it his computer, bad programming or the wrong use of the engine?

Help D:
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Strange lag

Post by Boolsheet »

It's (somewhat) easy to create some shapes with love.physics and make them collide, but it's much harder to make them behave exactly like you want; this requires a good knowledge of Box2D and all its special rules. I'm not saying you should avoid love.physics because of this, just expect to hit some problems that can only be solved by understanding Box2D.

For example, when you use a variable time step, it's very likely that the physics simulation will behave differently every time. Calling World:update with a small value makes it more accurate, but obviously costs more CPU time. Passing a large value makes it less accurate. If it fluctuates, it will get somewhat unpredictable and unstable. You can test this yourself by turning off vsync (love.graphics.setMode or love.conf) and adding a call to love.timer.sleep in love.draw or love.update. Start with a value of 0.001 (one millisecond) and go up to 0.04. The optimal scenario would be that you can pass any sane value and it still would show the same result. It's usually done with a fixed time step.

I'm not sure why your friend's system is slowing down, but I guess it shows up differently because the code produces different results with variable frame rates. One example is that you decrease the ttl of a bullet always by 1. Systems that can render with 1000 FPS will delete the bullet in 0.1 seconds.

Some other things. You could use math.atan2 that takes care of the issue of dividing by 0.

Code: Select all

   table.insert(bullet, i)
   bullet[i] = {}
Assuming 'i' is '#bullet + 1', this inserts the value of 'i' at the end of the sequence and then overwrites it with a new table. You don't need the insert, basically.

Generally, removing stuff from a table while you are iterating over it is bad (mostly because it will introduce weird bugs). Lua allows you to change and remove (set to nil), but not add. table.remove modifies the table in a way that it may add things and you get undefined behaviour. On way to solve it is to use a 'for' loop that iterates backwards.

Code: Select all

for i = #bullet, 1, -1 do
    if bullet[i].deleteMe then
        table.remove(bullet, i)
    end
end
Shallow indentations.
Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Re: Strange lag

Post by Ford_Prefect »

I dont understand... our framerate is always 60.

Code: Select all

love.load()
dt = 0.02

[...]

love.update(dt)
Btw, what can we do to understand box2d properly?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Strange lag

Post by Nixola »

Ford_Prefect wrote:I dont understand... our framerate is always 60.

Code: Select all

love.load()
dt = 0.02

[...]

love.update(dt)
First, this will only set dt to 0.02 once, and before it's used it will be updated; it won't even be 0.02 the first time it's used. Take a look at love.run to understand how dt works.
Second, making dt a fixed value without any other control or change is a bad thing, it's like not using it at all. The result will always be framerate-dipendent. If someone on a fast pc that doesn't limit the framerate with vsync plays your game it'll be awfully fast, while someone with a slow pc won't be able to enjoy the game 'cause it will be slow. A way to always send fixed timesteps to love.physics without alterin the results too much is this:

Code: Select all

function love.load()
   phys_dt = 0
   --everything else
end

function love.update(dt)
   phys_dt = phys_dt + dt
   while phys_dt > 1/60 do
      world:update(1/60)
      phys_dt = phys_dt - 1/60
   end
   --everything else
end
I'm sure it has some drawbacks on very slow PCs, but I can't remember them right now, sorry ^^'
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Re: Strange lag

Post by Ford_Prefect »

Ok. We think we understood dt and the new way of calculating it plus the backwards-iterating improved performance on my friends computer.

Thank you for your fast answers :)

Just one question: Is there any way to detect collisions easily?
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Strange lag

Post by josefnpat »

Ford_Prefect wrote:Ok. We think we understood dt and the new way of calculating it plus the backwards-iterating improved performance on my friends computer.

Thank you for your fast answers :)

Just one question: Is there any way to detect collisions easily?
I usually suggest just using the distance formula from the center of objects. It's pretty damn easy.

Not to derail this thread, but I like your name ;)
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Re: Strange lag

Post by Ford_Prefect »

Okay, thank you.

And I like my username, too <3

;)

edit: again, we are not capable of fixing these performance issues...
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests