#fixed timestep while loop

82 messages · Page 1 of 1 (latest)

vivid flameBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

icy maple
#

you can use time

#

figure out how fast you want the loop to run

#

alright so you set a time at the start of the loop

#

wrong channel then

#

all good

#

you can use <chrono>

#

the high resolution clock

#

if you dont want anything to happen while waiting

#

the simplest solution is to sleep for n milliseconds

#
using steady_clock = std::chrono::high_resolution_clock;

while(some_condition) {
    auto start = steady_clock::now();
    // stuff
    auto end = steady_clock::now();
    auto offset = 17 - std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
    Sleep(offset);
}
#

you can also write a timer class to do this for you

#

which is what i ended up doing for one of my projects

#

its a chrono::time_point

#

auto just means the type is deduced

#

why do you want to?

#

you dont need to know it

#

well

#

its gonna be std::chrono::time_point<std::chrono::steady_clock>

#

not as nice as auto eh

icy maple
#

you didnt do the cast

#

the duration_cast i did earlier

#

casts the time point to a usable value

#

like milliseconds

#

std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()

#

a time_point

#

also please work on formatting 😭

#

ur code isnt readable

#

add spaces

#

are you on windows

#

its Sleep

#

did you #include <windows.h>

#

also are you not using an ide?

#

like visual studio 2022

#

vscode

#

thats an ide then

#

not a good one

#

ide means integrated development environment

#

yea because its probably a linux backend

#

linux isnt windows

#

you can use usleep on linux

#

which takes microseconds

#

so just multiply ur value by 1000

#

the cast converts it to a size_t

#

why did you pick 2000 as the value btw

#

thats gonna be 2 seconds

#

its not running on your computer

#

anyway thats a different problem

#

you should make it lower tho

#

high precision clock usually uses nanoseconds

#

but its an internal

#

so you convert it to whatever unit you need

#

and get the value

#

try smth like

auto val = 100000 - std::chrono::duration_cast<std::chrono::microseconds>(endTime-startTime).count();
usleep(val);
#

endTime - startTime gets the difference
std::chrono::duration_cast<std::chrono::milliseconds> converts the difference to milliseconds
.count() gets the value

carmine gorge
#

std::this_thread::sleep_for (1s); will sleep for one second (this also works for ms millisecond, ns nanosecond, etc.). #include <thread> and #include <chrono> and

using namespace std::literals;
icy maple
#

works as well

#

but that would require converting back

#

idk whats the main function doing

#

mainFunctions()

#

because its fast

#

its not really doing anything

#

yea

#

change it to std::chrono::nanoseconds

#

youll see it changes

#

yea

#

sweet

#

gl

#

thats a way to do it

#

but you can also use usleep

#

which is higher precision

#

keep in mind sleep functions arent portable

#

you cant use the same functions on windows

#

no its fine

#

if you just want a consistent rate

#

generally you wanna do it in the gpu

#

you would want to run it on ur system

#

not on the browser

#

its complicated

#

but you can use opengl/cl

vivid flameBOT
#

@fair mist Has your question been resolved? If so, run !solved :)