Search code examples
javaopengllwjgl

OpenGL translate object base upon window size


I'm trying to create a window where all objects are moved the same relative distance regardless of resolution. I'm setting the viewport to the bounds of -1, 1, 1, -1 and i'm calculating a pixel distance by

int number_of_pixels = 10;
float x_pixel_value = (1f / (window.getWidth() / 2)); 
float move_x_value = x_pixel_value * number_of_pixels;

float y_pixel_value = (1f / (window.getHeight() / 2)); 
float move_y_value = y_pixel_value * number_of_pixels;

The distance seems to scale correctly but when I move the object in a tiny resolution, it moves much faster compared to if the resolution was bigger. e.g the object would take 2 seconds to move all the way down the screen at 720p but at 100x100 it would take .3 seconds.

I've tried implementing a frame limiter to make sure that it wasn't lag causing it to move slower but that did nothing.

Larger resolution

Smaller resolution


Solution

  • Use a constant value that does not depend on the window dimensions.

    The clipping volume does not care about how many pixels there is in each direction; an object at (-1,-1) will always be at the bottom left corner, and (1,1) will always be at the top right corner. The same applies for velocity. If you really want to use window dimensions, call glOrtho to change the clipping volume.