One more update… I’m set on creating a separate component that will take care of adjustingFPS for you (and eventually allowing for slow-mode and etc.). Here’s what I’ve made so far, based on the code from mathias:
GameScreen.lua: (somewhere in your game code)
[lua]local TimeMachine = require(“TimeMachine”)
TimeMachine.start()[/lua]
TimeMachine.lua:
[lua]local M = {}
M.fps = 30
local mFloor= math.floor
local prevTime, frameCounter = 0, 0
local baseTimeStep, curTimeStep = M.fps, M.fps
M.updateInterval = 1
physics.setTimeStep(1/baseTimeStep)
M.adjustFPS = function()
local curTime = system.getTimer()
local dt = curTime - prevTime
prevTime = curTime
local fps = mFloor(1000/dt)
– CHECK EVERY N FRAMES
if frameCounter == M.updateInterval then
if fps < baseTimeStep then
– DROP IN FPS, SET NEW TIME STEP
physics.setTimeStep(1/fps)
curTimeStep = fps
else
– REVERT BACK TO BASE TIME STEP
if curTimeStep < baseTimeStep then
physics.setTimeStep(1/baseTimeStep)
curTimeStep = baseTimeStep
end
end
frameCounter = 0
–print(fps)
end
frameCounter = frameCounter + 1
end
M.start = function()
Runtime:addEventListener(“enterFrame”, M.adjustFPS)
end
M.stop = function()
Runtime:removeEventListener(“enterFrame”, M.adjustFPS)
end
return M[/lua]
Obviously there is plenty of room for improvement, but just a start. This way there could be a single “TimeMachine” class that handles all things FPS related (within physics projects), and which can be used in multiple projects. Feel free to add on or make any suggestions. [import]uid: 49447 topic_id: 15627 reply_id: 78313[/import]