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