private void Updater() { _lag = TimeSpan.Zero; _tpsCounterTime = TimeSpan.Zero; _stopwatch.Start(); while (IsActive) { _current = _stopwatch.Elapsed; _elapsed = _current - _last; _lag += _elapsed; FixedUpdate?.Invoke(this, new UpdateEventArgs() { DeltaTime = _elapsed }); while (_lag >= _updaterFrequency) { Update?.Invoke(this, new UpdateEventArgs() { DeltaTime = _elapsed + _lag }); _lag -= _updaterFrequency; } _last = _current; } }
// Triggers when the game makes a fixed update internal static void OnGameFixedUpdate() { if (!isInitialized) { return; } FixedUpdate?.Invoke(); }
private void Updater() { _stopwatch.Start(); while (CurrentState == TimerState.Active || CurrentState == TimerState.Paused) { while (CurrentState == TimerState.Paused) { if (CurrentState == TimerState.Active || CurrentState == TimerState.Stopped) { break; } PausedUpdate?.Invoke(null, UpdateEventArgs.Empty); } _current = _stopwatch.Elapsed; _elapsed = _current - _last; _lag += _elapsed; Update?.Invoke(this, new UpdateEventArgs() { DeltaTime = _elapsed }); Debugging.UpdateTime = (float)_elapsed.TotalSeconds; ticks++; Debugging.LagTime = (float)_lag.TotalSeconds * 1000.0f; while (_lag >= _updaterFrequency) { FixedUpdate?.Invoke(this, new UpdateEventArgs() { DeltaTime = _elapsed + _lag }); Debugging.FixedUpdateTime = ((float)_elapsed.TotalSeconds + (float)_lag.TotalSeconds); fixedTicks++; _lag -= _updaterFrequency; } _last = _current; _tpsAccumulator += _elapsed; if (_tpsAccumulator >= TimeSpan.FromSeconds(1.0f)) { Debugging.TPS = ticks; Debugging.FixedTPS = fixedTicks; ticks = 0; fixedTicks = 0; _tpsAccumulator = TimeSpan.Zero; } } if (CurrentState == TimerState.Paused || CurrentState == TimerState.Active) { Debugging.Log(LogEntryType.Warning, "Update timer was stopped unexpectedly!"); } }
public void Step(TimeSpan elapsed) { if (disposed) { throw new ObjectDisposedException("PhysicsWorld"); } accumulatedTime += elapsed.TotalSeconds; while (accumulatedTime > TIMESTEP) { FixedUpdate?.Invoke(TimeSpan.FromSeconds(TIMESTEP)); if (disposed) { return; //Alllow delete within FixedUpdate. Hacky but works } btWorld.StepSimulation(TIMESTEP, 1, TIMESTEP); accumulatedTime -= TIMESTEP; } foreach (var obj in dynamicObjects) { obj.UpdateProperties(); } }
public void Step(TimeSpan elapsed) { if (disposed) { throw new ObjectDisposedException("PhysicsWorld"); } accumulatedTime += elapsed.TotalSeconds; while (accumulatedTime >= TIMESTEP) { FixedUpdate?.Invoke(TimeSpan.FromSeconds(TIMESTEP)); if (disposed) { return; //Alllow delete within FixedUpdate. Hacky but works } btWorld.StepSimulation(TIMESTEP, 0, TIMESTEP); accumulatedTime -= TIMESTEP; //Update C#-side properties after each step. Creates stuttering otherwise foreach (var obj in dynamicObjects) { obj.UpdateProperties(); obj.RigidBody.Activate(true); } } }
internal static void Internal_FixedUpdate() { Time.SyncData(); FixedUpdate?.Invoke(); }
internal static void Internal_FixedUpdate() { FixedUpdate?.Invoke(); }
public void OnFixedUpdate() => FixedUpdate?.Invoke();
public void OnFixedUpdate(int dTime) { Asynced = false; FixedUpdate?.Invoke(dTime); }
public static void Run() { shouldRun = true; Stopwatch watch = new Stopwatch(); Stopwatch frameTimeWatch = new Stopwatch(); watch.Start(); elapsedMs = watch.ElapsedMilliseconds; float fixedUpdateTimer = 0; while (shouldRun) { Profiler.StartFrame(FrameNumber); frameTimeWatch.Restart(); long newTimeMs = watch.ElapsedMilliseconds; float deltaTime = (newTimeMs - elapsedMs) / 1000f; if (deltaTime > 1) { deltaTime = fixedUpdateStep; } elapsedMs = newTimeMs; fixedUpdateTimer += deltaTime; int fixedUpdatesPerformed = 0; while (fixedUpdateTimer > fixedUpdateStep) { fixedUpdatesPerformed++; FixedUpdate?.Invoke(fixedUpdateStep); fixedUpdateTimer -= fixedUpdateStep; if (frameTimeWatch.ElapsedMilliseconds / 1000f > maxFixedUpdateCatchup) { fixedUpdateTimer = 0; deltaTime = fixedUpdateStep * fixedUpdatesPerformed; elapsedMs = watch.ElapsedMilliseconds; } } Update?.Invoke(deltaTime); Render?.Invoke(deltaTime); Profiler.EndFrame(); frameTimeWatch.Stop(); if (targetFps > 0) { float targetFrameTime = 1f / targetFps; float sleepTime = targetFrameTime - (frameTimeWatch.ElapsedMilliseconds / 1000f); if (sleepTime > 0) { Thread.Sleep((int)(sleepTime * 1000f)); } } FrameNumber++; } Update -= World.InvokeUpdate; Render -= World.InvokeRender; FixedUpdate -= World.InvokeFixedUpdate; //Cleanup World.CleanUp(); if (Window.window.Exists) { Window.Close(); } GraphicsContext.DisposeResources(); // Needs to be called after everything else has been cleaned up }