Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sythical

Member Since 06 Jul 2013
Offline Last Active Apr 25 2014 05:29 PM

Topics I've Started

Drawing symmetric equilateral triangles

25 April 2014 - 12:27 PM

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.

 

WrgKTTN.png

 

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!


Trouble making my animation smooth

28 July 2013 - 01:56 PM

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!


Few questions about fix timestep for physics

12 July 2013 - 09:55 PM

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 smile.png

 

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.


Timer class not working properly

06 July 2013 - 01:13 AM

Hello, I've written a timer class in C++ but it's not working properly. The trouble I'm having is that after a couple of minutes the animation becomes choppy. It sometimes fixes automatically after about 5 ~ 60 seconds.

#include <windows.h>

class Timer {

	int sleep_length_ms;
	double ratio;
	LONGLONG duration, next_mark;
	LARGE_INTEGER counter, frequency;

public:
	Timer(int frame_rate) {
		QueryPerformanceCounter(&counter);
		QueryPerformanceFrequency(&frequency);

		duration = frequency.QuadPart / frame_rate;
		next_mark = counter.QuadPart + duration;
		ratio = 1000.0 / frequency.QuadPart;
	}

	bool wait() {
		QueryPerformanceCounter(&counter);
		sleep_length_ms = (int) (ratio * (next_mark - counter.QuadPart));

		if(sleep_length_ms > 0) {
			Sleep(sleep_length_ms);
			next_mark += duration;					
		} else {
			// if running behind
			QueryPerformanceCounter(&counter);
			next_mark = counter.QuadPart + duration;
		}

		return true;
	}
};

This is how the class if used in a game loop:

Timer timer(60);

while(run_game) {
	update_game();
	render_game();

	timer.wait();
}

I'm testing this class using a simple Allegro program where a circle bounces off the edges. I tried using the timer class provided by Allegro and the issue resolves so I'm quite sure that the problem lies within my timer class. I can't seem to figure out what I'm doing wrong and would be very grateful if someone can have a read through the code. Thank you!


PARTNERS