ScrollerBlaster
ScrollerBlaster

Reputation: 1598

Using glOrtho for scrolling game. Spaceship "shudders"

I want to draw a spaceship in the centre of a window as it powers through space. To scroll the world window, I compute the new position of the ship and centre a 4000x3000 window around this using glOrtho. My test is to press the forward button and move the ship upwards. On most machines there is no problem. On a slower linux laptop, where I am getting only 30 frames per second, the ship shudders back and forth. Comparing its position relative to the mouse pointer (which does not move), the ship can clearly be seen jumping forward and back by a couple of pixels. The stars are also shown to be blurred into short lines.

I would like to query the pixel value of the centre of the ship to see if it is changing.

Is there an OpenGL way to supply a world point and get back the pixel point it will be transformed to?

Upvotes: 1

Views: 309

Answers (1)

Pete
Pete

Reputation: 4812

I'd consider doing it the other way around. Keep the ship at 0,0 world coordinates, and move the world relative to it. Then you only need the glOrtho call when the size of the window changes (the camera is in a fixed position). Apart from not having to calculate the projection matrix every time, you also have the benefit that if your world ends up being massive in the future then you have the option of using double precision positions, since large offsets on floats results in inaccurate positioning.

To draw your space scene, you then manipulate the modelview matrix before drawing any objects, and use a different matrix when drawing the ship.

To get a pixel coordinate from a world coordinate, take the point and multiply it by the projection matrix multiplied by the modelview matrix (make sure you get the multiplication around the right way). You'll then have a value that has x and y in the ranges -1 to 1. You can add [1,1] and multiply by half the screen size to get the pixel position. (if you wanted, you could add this into the matrix transformation).

Upvotes: 1

Related Questions