Rotating speed varies

Hello, i have a little problem. When i rotate object in opengl, rotation speed is not the same. When window is 1200x1000 for example, triangle rotates much slower, then when window is for example 800x600. Can you tell me why is that? Thanks for help.

Rotation code:

std::vector<float> vertices = 
    400.0f,  300.0f, 
    700.0f,  300.0f,
    550.0f,  450.0f, 
float centerX = (vertices[0] + vertices[2] + vertices[4]) / 3.0f;
float centerY = (vertices[1] + vertices[3] + vertices[5]) / 3.0f;

for (int i = 0; i < vertices.size(); i += 2)
    vertices[i] -= centerX;
    vertices[i + 1] -= centerY;

    float rotatedX = vertices[i] * cos(angleRadian) - vertices[i + 1] * sin(angleRadian);
    float rotatedY = vertices[i] * sin(angleRadian) + vertices[i + 1] * cos(angleRadian);

    vertices[i] = rotatedX + centerX;
    vertices[i + 1] = rotatedY + centerY;

Shader code:

  #shader vertex
  #version 330 core

  layout(location = 0) in vec2 position;
  uniform float u_Scale; 
  uniform mat4 u_OrthographicMatrix;
  uniform float windowWidth;
  uniform float windowHeight;

  void main()
      vec2 normalizedCoords = position / vec2(windowWidth, windowHeight) * 2.0 - 1.0;
      gl_Position = u_OrthographicMatrix * vec4(normalizedCoords.x, normalizedCoords.y, 0.0, 1.0);

Probably because it takes longer to draw when the resolution is higher. If vsync isn’t enabled (and you aren’t using a software renderer which takes multiple frames to draw a single triangle), taking longer to draw each frame means fewer frames per second. If you’re updating the angle by a fixed amount per rendered frame, fewer frames per second means slower rotation.

Normally you’d advance the animation based upon actual time rather than a fixed amount per rendered frame. Either by measuring the time difference between frames and scaling the change proportionally, or (preferably) by calling the update function a fixed number of times per second regardless of frame rate (see this post ).

1 Like

thank you, it makes sense now… Couldnt figure it out for a long time :slight_smile: