Problem with entering timestep variable

I am currently engaged in computer games programming at the university, in the process of creating a physics mechanism in C ++.

I was asked to enter timestep using the GetTickCount () method, despite the inaccuracies, because it is suitable for the current depth that we go to during the step

My problem is that I now introduced a timestep based movement, objects that painted on the screen using two different overrides of the update function do not seem to work.

By adding breakpoints in the code, it looks like mElapsedTime doesn't seem to get any value, and I'm at a dead end.

Sorry if this post is too verbose or someone is off topic, I tried to provide as much context as possible, and this is my first post, any help is appreciated.

(the frameStartTime parameter simply takes the number of samples before the frame is updated and drawn)

Edit: mElapsed time is of type float, for easier multiplication in particle class updates (e.g. pos.x = velocity.x * timeStep), and this is the reason for casting from unsigned long to float, is it still redundant?

void Simulation::gameLoopDelay(DWORD frameStartTime) { DWORD presetFrameInterval = 16; DWORD frameProcessingTime = GetTickCount() - frameStartTime; if (frameProcessingTime < presetFrameInterval) { Sleep(presetFrameInterval - frameProcessingTime); } mElapsedTime = (float)frameProcessingTime / 1000.0f; } 
+7
source share
2 answers

I am not sure (because you did not provide enough information), but the first problem I see is that you are doing mElapsedTime = (float)frameProcessingTime / 1000.0f; regardless of Sleep(...) . In other words, you forgot to consider sleeping. To do this, you will need to re-select GetTickCount() after calling Sleep(...) . But even then, your code becomes too boilerplate and error prone.

Start approaching your problem by writing small, flexible, and reusable classes that perform simple tasks:

 class Stopwatch { public: Stopwatch(): _start(0), _elapsed(0), _running(false) {} void start() { if (_running) return; _running = true; _start = GetTickCount(); } void stop() { if (!_running) return; _elapsed += GetTickCount() - _start; _running = false; } void reset() { _running = false; _elapsed = 0; } void restart() { _running = true; _elapsed = 0; _start = GetTickCount(); } bool isRunning() const { return _running; } DWORD elapsed() const { return _running ? _elapsed + (GetTickCount() - _start) : _elapsed); } bool hasExpired(DWORD interval) const { return elapsed() > interval; } private: DWORD _start; DWORD _elapsed; bool _running; }; 

Just how to use this class for your needs.

Secondly, I really don’t understand why you are using Sleep . The last time I saw an example of frame delay was around 5 years ago in some shitty toy-writing tutorials. This material was really popular in old games, but in modern games it makes no sense. Of course, if this is a requirement for your assignment, then I apologize, but this remark still applies.

Finally, I would like to give you additional tips on my own experience in real-time visualization. Never write time-dependent code with source features such as GetTickCount or other popular crap among Windows API programmers (or Linux APIs, it doesn't matter), because it smells like outdated tutorials on creating toy games again.

What to use, you ask? Well, these days we are lucky because there are well-designed, robust cross-platform libraries like Boost . You should probably be familiar with this, however, if you do not, you must do so. For your specific problem, there is a Boost.Chrono module that has been carefully designed to work properly over time. I have used it in several projects related to real-time rendering, and I must admit that it is very reliable and worth exploring.

Another library - Boost.Units - is a must for writing a stable and accurate physics engine. This library provides support for types for units (force, mass, speed, acceleration, etc.) and quantities for various models of unit systems (for example, SI or CGS). It is based on a metaprogramming pattern and, therefore, does not create unnecessary time consuming.

Once again, I understand that your assignment probably does not allow this. However, I believe that everything I mentioned here will be valuable to you in the future. By the way, this is a good example of how much more you should know than offering university assignments in order to be able to solve real-life problems.

If you still need more help on your issue, in particular, I need more information. For example, start by displaying the entire code for your game loop.

+1
source

I think, basically, you need to change your code to this:

 void Simulation::gameLoopDelay(DWORD frameStartTime) { DWORD presetFrameInterval = 16; DWORD frameProcessingTime = GetTickCount() - frameStartTime; if (frameProcessingTime < presetFrameInterval) { Sleep(presetFrameInterval - frameProcessingTime); } frameProcessingTime = GetTickCount() - frameStartTime; mElapsedTime = (float)frameProcessingTime / 1000.0f; } 

This is not the most recommended way to do this, since Sleep here probably blocks the entire thread. It is probably best to take a shot in fractional time, but I think this will fix your code.

+1
source

All Articles