robosim.utils.clock
clock.py - clock used for physics with fixed DT in main loops
1"""clock.py - clock used for physics with fixed DT in main loops""" 2import raylib as rl 3 4class Clock: 5 """clock used for physics with fixed DT in main loops""" 6 7 def __init__(self, dt: float, max_ticks: int): 8 self.dt = dt 9 self.max_ticks = max_ticks 10 self.prev_time = rl.GetTime() 11 self.accumulator = 0 12 13 def tick(self): 14 """tick once by adding the delta between prev frame and now""" 15 now = rl.GetTime() 16 frame_time = now - self.prev_time 17 self.prev_time = now 18 self.accumulator += frame_time 19 20 def drain(self): 21 """drain the accumulator. in main loop: for _ in clock.drain(): ...""" 22 n_ticks = 0 23 while self.accumulator >= self.dt and n_ticks < self.max_ticks: 24 yield 25 self.accumulator -= self.dt 26 n_ticks += 1 27 self.accumulator = min(self.accumulator, self.dt) # Drop residual debt instead of it piling up across frames 28 29 def wait(self): 30 """waits the leftover time in case the previous tick ran too fast to maintain consistent FPS""" 31 rl.WaitTime(max(self.dt - (rl.GetTime() - self.prev_time), 0)) 32 33 def wait_and_tick(self): 34 """calls wait() then tick(). Put this at the beginning of the main loop :)""" 35 self.wait() 36 self.tick()
class
Clock:
5class Clock: 6 """clock used for physics with fixed DT in main loops""" 7 8 def __init__(self, dt: float, max_ticks: int): 9 self.dt = dt 10 self.max_ticks = max_ticks 11 self.prev_time = rl.GetTime() 12 self.accumulator = 0 13 14 def tick(self): 15 """tick once by adding the delta between prev frame and now""" 16 now = rl.GetTime() 17 frame_time = now - self.prev_time 18 self.prev_time = now 19 self.accumulator += frame_time 20 21 def drain(self): 22 """drain the accumulator. in main loop: for _ in clock.drain(): ...""" 23 n_ticks = 0 24 while self.accumulator >= self.dt and n_ticks < self.max_ticks: 25 yield 26 self.accumulator -= self.dt 27 n_ticks += 1 28 self.accumulator = min(self.accumulator, self.dt) # Drop residual debt instead of it piling up across frames 29 30 def wait(self): 31 """waits the leftover time in case the previous tick ran too fast to maintain consistent FPS""" 32 rl.WaitTime(max(self.dt - (rl.GetTime() - self.prev_time), 0)) 33 34 def wait_and_tick(self): 35 """calls wait() then tick(). Put this at the beginning of the main loop :)""" 36 self.wait() 37 self.tick()
clock used for physics with fixed DT in main loops
def
tick(self):
14 def tick(self): 15 """tick once by adding the delta between prev frame and now""" 16 now = rl.GetTime() 17 frame_time = now - self.prev_time 18 self.prev_time = now 19 self.accumulator += frame_time
tick once by adding the delta between prev frame and now
def
drain(self):
21 def drain(self): 22 """drain the accumulator. in main loop: for _ in clock.drain(): ...""" 23 n_ticks = 0 24 while self.accumulator >= self.dt and n_ticks < self.max_ticks: 25 yield 26 self.accumulator -= self.dt 27 n_ticks += 1 28 self.accumulator = min(self.accumulator, self.dt) # Drop residual debt instead of it piling up across frames
drain the accumulator. in main loop: for _ in clock.drain(): ...