Sythical

Members
  • Content count

    16
  • Joined

  • Last visited

Community Reputation

144 Neutral

About Sythical

  • Rank
    Member
  1. I'm rendering equilateral triangles of different sizes and I'm having trouble making them symmetric along the vertical axis. I'm aware of the diamond exit rule but I don't think that's the cause of the problem.   Here I have rendered two triangles. The triangle on the left (rendered correctly) has the coordinates (0.0, 0.0), (-7.0, 14.0) and (7.0, 14.0). The triangle on the right, on the other hand, has the coordinates (0.0, 0.0), (-7.0, 15.0) and (7.0, 15.0) and is not symmetric along the vertical axis.     Here are the matrices I'm using: glm::mat4 model_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(90.0f, 90.0f, 0.0f)); glm::mat4 view_matrix = glm::mat4(1.0f); glm::mat4 projection_matrix = glm::ortho(0.0f, w * 1.0f, h * 1.0f, 0.0f); glm::mat4 mvp_matrix = projection_matrix * view_matrix * model_matrix; . I don't see why the coordinates should make a difference as long as they're symmetrical. This problem also exists when I try to make triangles symmetric in the horizontal axis. Thanks for reading!
  2.   I get the current time at the start of each frame and use it for doing the physics. The FPS counter is completely seperate so I don't think it has an effect on the physics.     When trying to figure out how to use vsync in SDL, I was told that vsync doesn't work in windowed mode since the draws are managed by the OS. If I modify the program to use SFML instead of Allegro, vsync doesn't work and the animation is not smooth so I feel that using Allegro's vsync is kind of cheating and not an actual solution (since if I decide to switch to SDL and SFML the problem will show up again).     Yes, that's what I'm thinking as well so I'll try and figure out if there is a way to solve this.
  3. Sorry, I tried recording the bug but it introduced extra lag and the actual issue wasn't clearly visible in the video. Changing step duration to 2.0 makes the animation very slow and it's a little bit smoother.   I may have discovered the issue: after swapping the buffers, I started printing the time difference between the swap that just happened and the previous swap. It normally prints 0.008s and 0.009s but 0.01s are quite frequent so maybe this is causing the animation to not look as smooth? I found out that Allegro has vsync option and enabling that made the animation very smooth. From the output, I got 0.016s with occasional 0.0017s which is a lot more even.
  4. My framerate varied between 58 and 62 and I was told on the Allegro forum that this is normal and I should  calculate the average instead. When playing games, I notice that the FPS changes a lot and so I assumed that games still look smooth even if the FPS isn't completely steady.   And... I thought I was already using frame rate independent timing? :(
  5.   delta_time is in seconds and it's usually about 0.00-something so making that change just makes the circle stay still. Changing the type of the position variables to double makes the circle move ever so slightly.
  6. Thank you for the suggestion. Just tried that and it made no difference. With the pause, my frame rate is around 125 so the physics is updated once for every 2 to 3 renders.
  7. Hello, I'm trying to implement the game loop where the physics is independent from rendering but my animation isn't as smooth as I would like it to be. The animation periodically jumps forward and I have no idea why this is happening. Here is my code: . // alpha is used for interpolation // counter_old_time is to do with displaying the fps double alpha = 0, counter_old_time = 0; double accumulator = 0, delta_time = 0, current_time = 0, previous_time = 0; unsigned frame_counter = 0, current_fps = 0; // also used for displaying the fps const unsigned physics_rate = 40, max_step_count = 5; // information about the magic ball (position and velocity) int old_pos_x = 100, new_pos_x = 100, render_pos_x = 100, velocity_x = 60; const double step_duration = 1.0 / 40.0, accumulator_max = step_duration * 5; previous_time = al_get_time(); while(true) { current_time = al_get_time(); delta_time = current_time - previous_time; previous_time = current_time; accumulator += delta_time; if(accumulator > accumulator_max) { accumulator = accumulator_max; } while(accumulator >= step_duration) { if(new_pos_x > 1330) velocity_x = -15; else if(new_pos_x < 70) velocity_x = 15; old_pos_x = new_pos_x; new_pos_x += velocity_x; accumulator -= step_duration; } alpha = accumulator / static_cast<double>(step_duration); render_pos_x = old_pos_x + (new_pos_x - old_pos_x) * alpha; al_clear_to_color(al_map_rgb(20, 20, 40)); // clears the screen al_draw_textf(font, al_map_rgb(255, 255, 255), 20, 20, 0, "current_fps: %i", current_fps); // print fps al_draw_filled_circle(render_pos_x, 400, 15, al_map_rgb(255, 255, 255)); // draw circle // I've added this to test how the program will behave when rendering takes // considerably longer than updating the game. al_rest(0.008); al_flip_display(); // swaps the buffers frame_counter++; if(al_get_time() - counter_old_time >= 1) { current_fps = frame_counter; frame_counter = 0; counter_old_time = al_get_time(); } } . I have added a pause during the rendering part because I wanted to see how the code would behave when a lot of rendering is involved. Removing it makes the animation smooth but then I'll have to make sure that I don't let the frame rate drop too much and that doesn't seem like a good solution.   I've tried remove the clamp (that avoids the spiral of death) and checked my interpolation method but had no luck so I'd be very grateful if someone can have a look at this. Thank you!
  8. Few questions about fix timestep for physics

    Thank you for the reply   I tried my code with Allegro (made a couple of small changes) and it's now very smooth. Here is the code so that others may find it useful, I haven't refactored it: #include <allegro5\allegro.h> #include <allegro5\allegro_font.h> #include <allegro5\allegro_primitives.h> #include <allegro5\allegro_ttf.h> #include <iostream> #include <windows.h> using namespace std; int main(int argc, char *argv[]) { if(!al_init()) return -1; ALLEGRO_DISPLAY *display = al_create_display(1400, 800); if(!display) return -1; al_init_font_addon(); al_init_primitives_addon(); al_init_ttf_addon(); ALLEGRO_FONT *font = al_load_font("terminalscope.ttf", 16, 0); // ------------------------------------------------------------ LARGE_INTEGER counter, frequency; double interpolation_val = 0, counter_old_time = al_get_time();; QueryPerformanceFrequency(&frequency); unsigned screen_width = 1200, screen_height = 800, frame_counter = 0, current_fps = 0; int old_pos_x = 100, new_pos_x = 100, velocity_x = 15; LONGLONG accumulator = 0, delta_time = 0, previous_time; const unsigned physics_rate = 80, max_step_count = 5; const LONGLONG step_duration = frequency.QuadPart / physics_rate, accumulator_max = step_duration * max_step_count; // ------------------------------------------------------------ QueryPerformanceCounter(&counter); previous_time = counter.QuadPart; // ------------------------------------------------------------ ALLEGRO_EVENT event; ALLEGRO_EVENT_QUEUE *event_queue; ALLEGRO_TIMER *timer; event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60.0); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); while(true) { QueryPerformanceCounter(&counter); delta_time = counter.QuadPart - previous_time; previous_time = counter.QuadPart; accumulator += delta_time; // ------------------------------------------------------------ if(accumulator > accumulator_max) { accumulator = accumulator_max; } while(accumulator >= step_duration) { if(new_pos_x > 1330) velocity_x = -15; else if(new_pos_x < 70) velocity_x = 15; old_pos_x = new_pos_x; new_pos_x += velocity_x; accumulator -= step_duration; } interpolation_val = accumulator / (double) step_duration; double render_pos_x = old_pos_x + (new_pos_x - old_pos_x) * interpolation_val; // ------------------------------------------------------------ al_clear_to_color(al_map_rgb(20, 20, 40)); al_draw_textf(font, al_map_rgb(255, 255, 255), 20, 20, 0, "current_fps: %i", current_fps); al_draw_filled_circle(render_pos_x, 400, 15, al_map_rgb(255, 255, 255)); al_wait_for_event(event_queue, &event); al_flip_display(); frame_counter++; if(al_get_time() - counter_old_time >= 1) { current_fps = frame_counter; frame_counter = 0; counter_old_time = al_get_time(); } } return 0; }
  9. Hello, I've been trying to implement the fix time-step technique and I'm having trouble understanding a couple of thing. I've listed the two issues separately so that they make more sense. i) To interpolate between the old and the new state, I'm using two variables called old_pos_x and new_pos_x. Originally, I would copy new_pos_x to old_pos_x after rendering each time. This makes more sense to me since I want to interpolate between the circle on the screen and the one I'll be drawing next. However, this makes the animation stutter if I don't update the physics at 60 or 120 hertz (multiples of my refresh rate). When I moved the copy statement inside the physics loop, the stutter goes away and I have no idea why. I would have thought that this will make the game stutter a lot more, especially when the the frame rate is lower than the update rate. Why does this work? ii) I added Sleep(5) after the render bit in order to decrease the FPS to about 200 so that I can see how it works at low rates. The animation becomes very jumpy and not smooth at all. I understand that Sleep() isn't accurate but surely that shouldn't matter as long as the frame rate is above 60 FPS because of the monitor's refresh rate. I've written a smaller version of the program which replicates the issue(s) so that it's easier to read. It's just a circle that moves horizontally. The code is still a bit long, sorry about that.   unsigned screen_width = 1200, screen_height = 800; int old_pos_x = 100, new_pos_x = 100, velocity_x = 0; LARGE_INTEGER counter, frequency; LONGLONG accumulator = 0, accumulator_max, physics_delta, time_difference, new_time, previous_time; sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Example", sf::Style::Titlebar | sf::Style::Close); sf::CircleShape graphics(15.f); graphics.setFillColor(sf::Color(255, 255, 255)); QueryPerformanceFrequency(&frequency); physics_delta = frequency.QuadPart / 50; accumulator_max = physics_delta * 5; QueryPerformanceCounter(&counter); previous_time = counter.QuadPart; while(window.isOpen()) { QueryPerformanceCounter(&counter); new_time = counter.QuadPart; time_difference = new_time - previous_time; previous_time = new_time; accumulator += time_difference; if(accumulator > accumulator_max) { accumulator = accumulator_max; } while(accumulator >= physics_delta) { if(new_pos_x > 1000) velocity_x = -8; else if(new_pos_x < 200) velocity_x = 8; // if I copy the new state (position) to the old state here instead the animation is much more smooth // but I don't understand why this is the case, I would have thought that this is more likely to cause stutter // especially if the frame rate is lower than the game update rate // old_pos_x = new_pos_x; // copy the current state to the old one new_pos_x += velocity_x; // update the current state accumulator -= physics_delta; } double interpolation_val = accumulator / (double) physics_delta; graphics.setPosition(new_pos_x * interpolation_val + old_pos_x * (1 - interpolation_val), 400.f); window.clear(); window.draw(graphics); window.display(); // this is where I was originally copying the new state (position) to the old state // this makes more sense to be because I want to interpolate between the last drawn circle // and the current circle old_pos_x = new_pos_x; // Sleep(5); }   I'd be very grateful if someone can have a read through the code. Thank you   Edit: I think I've figured out the reason for the first part. The game renders quicker than it updates so if I copy the states after rendering, the position will not be interpolated properly until it updates again. Still not sure about the second part.
  10. Timer class not working properly

      Yup, I've tested that and the else block is only executed once at the start because of the way I'm using the class.     Yes you're right, my top priority is to get a smooth animation but I got distracted by trying to minimise CPU usage and wanted to know if I'm doing something wrong in my timer class.   Thank you everyone for the suggestions, I should stop reinventing the wheel and focus on the actual game instead
  11. Timer class not working properly

    Yea, I'm using SFML and it has a setVerticalSyncEnabled() function.
  12. Timer class not working properly

    I considered using Sleep(1) but I believe it'll make thread oversleep fairly often. Another thing I tried was make the thread sleep for 70% of the required time and then rely on a do while loop for the remainder of the duration. That was fairly accurate and the dropped the usage to 10%. I'll follow your advice and decouple the physics/logic and rendering, I have read the fix your timestep article and implement that but my GPU's fan started making an unpleasant noise (the whole reason why I got obsessed with CPU/GPU usage).
  13. Timer class not working properly

    I was using something similar to the code you posted but the CPU usage was around 25%. I know that there isn't exactly anything wrong with that and it's normal for games but I wanted to try and write a CPU efficient timer.
  14. Timer class not working properly

    Thank you for the suggestion. I just tried that and sleep_length_ms fluctuates between 16 and 17 so that seems fine. When the animation finally starts to stutter, sleep_length_ms is still 16 or 17. I also added a check to let me know when Sleep() makes the thread oversleep and that doesn't seem to be the problem either. So confusing, it's probably a silly mistake somewhere that I can't spot.
  15. Timer class not working properly

    I know that Sleep provides no guarantee that the thread will "wake up" after the specified duration but I also don't want to rely on a do while loop for timing. I read through the relavent source files of Allegro and it also uses Sleep in the timer thread and it seems to work perfectly.