Page 1 of 1

Optimization issues -- How do I keep my game from lagging?

Posted: Thu Jul 01, 2021 5:22 pm
by ThatCodingGuy78
To be specific, I'm creating a TON of objects, and I know rendering isn't the issue as I have it rendering only in a very small area at a time. And GC goes insane when it launches. How would I fix this?

Also, here's my .love file if it's relevant:
Game Name.love
(471.35 KiB) Downloaded 302 times
Also, I'm using the G3D library.

Re: Optimization issues -- How do I keep my game from lagging?

Posted: Fri Jul 02, 2021 11:45 am
by pgimeno
I can't tell about the performance; maybe check with a profiler. I've seen trigonometry used where none is necessary (it's sadly usual to see how people take the atan2 of a vector then the sine and cosine in order to just normalize it, spending three trigonometric functions where just two multiplications and a square root would suffice).

As for garbage, I don't think you should worry too much about it. The main sources of garbage I've found are these two lines:

Code: Select all

    lg.print("GC: " .. collectgarbage("count"), 0, 80)
    lg.print("FPS: " .. tostring(love.timer.getFPS()))
You can check by moving them to e.g. love.keypressed, updating the window title instead of drawing.

Re: Optimization issues -- How do I keep my game from lagging?

Posted: Fri Jul 02, 2021 1:54 pm
by ThatCodingGuy78
pgimeno wrote: Fri Jul 02, 2021 11:45 am I can't tell about the performance; maybe check with a profiler. I've seen trigonometry used where none is necessary (it's sadly usual to see how people take the atan2 of a vector then the sine and cosine in order to just normalize it, spending three trigonometric functions where just two multiplications and a square root would suffice).

As for garbage, I don't think you should worry too much about it. The main sources of garbage I've found are these two lines:

Code: Select all

    lg.print("GC: " .. collectgarbage("count"), 0, 80)
    lg.print("FPS: " .. tostring(love.timer.getFPS()))
You can check by moving them to e.g. love.keypressed, updating the window title instead of drawing.
I tried a profiler, it was too laggy to even get it working without instantly crashing.

For the moving that bit of code, I'll do that, but there's still the issue of massive lag.

Re: Optimization issues -- How do I keep my game from lagging?

Posted: Fri Jul 02, 2021 11:00 pm
by Xugro
I used ProFi to profile your game. The report is attached. It is not much, because I only ever get three frames before your fixed timestamp accumulator updates itself to death.

Here are some timings around player:update(dt):

Code: Select all

accumulator: 0.00076811399776489
[ProFi]  Report written to MyProfilingReport59666.562357438.txt
accumulator: 0.020212903495121
Time for one update: 3.4169694750017
[ProFi]  Report written to MyProfilingReport59670.017422816.txt
accumulator: 1.1642815161598
time for one player:update(dt): 3.4132694939981
time for one player:update(dt): 3.4624316769987
time for one player:update(dt): 3.3923586650053
time for one player:update(dt): 3.4114579500019
time for one player:update(dt): 3.5470767929946
time for one player:update(dt): 3.4052011500025
time for one player:update(dt): 3.3857838770055
time for one player:update(dt): 3.4755725930008
time for one player:update(dt): 3.476247818995
The third frame takes ~4 minutes to render. The fourth frame would take ~3.5 hours. I think you version of fixed timestamp accumulator only works if player:update(dt) takes less time than frametime. This can be problematic if the player moves the window around.

The biggest problem is that your code takes about 98% of its time in calculating capsuleIntersection (for 2560 collisionModels in your Player - on a 16x16 map). With a 4x4 map the game is running smooth. So do not check collision with every block but only those near the player (e.g. only one or two blocks in each direction).

Edit:
Change your Player:collisionTest function to something like this and the game will run much smoother:

Code: Select all

-- collide against all models in collision list near the player
-- and return the collision against the closest one
function Player:collisionTest(mx,my,mz)

    local shrinkedCollisionModels = {}
    for _,model in ipairs(self.collisionModels) do
        local distanceSquared = (self.position[1] - model.translation[1])^2 + (self.position[2] - model.translation[2])^2 + (self.position[3] - model.translation[3])^2
        if distanceSquared <= 2 then
            table.insert(shrinkedCollisionModels, model)
        end
    end

    local bestLength, bx,by,bz, bnx,bny,bnz

    for _,model in ipairs(shrinkedCollisionModels) do
        local len, x,y,z, nx,ny,nz = model:capsuleIntersection(
            self.position[1] + mx,
            self.position[2] + my - 0.15,
            self.position[3] + mz,
            self.position[1] + mx,
            self.position[2] + my + 0.5,
            self.position[3] + mz,
            0.2
        )

        if len and (not bestLength or len < bestLength) then
            bestLength, bx,by,bz, bnx,bny,bnz = len, x,y,z, nx,ny,nz
        end
    end

    return bestLength, bx,by,bz, bnx,bny,bnz
end

Re: Optimization issues -- How do I keep my game from lagging?

Posted: Sat Jul 03, 2021 12:57 pm
by ThatCodingGuy78
Ohhh. That makes sense, I had optimized it for 1 model, the map one that you would've seen commented out, so that made sense for just 1 model, but not tons of individual ones.