Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm building a 2D engine based on 32x32 tiles with OpenGL. OpenGL draws from the top left, so Y coordinates go down the screen as they increase. Obviously this is different than a standard graph where Y coordinates move up as they increase.

I'm having trouble determining how I want to track positions for both sprites and tile objects (objects that are collections of tiles). My brain wants to set the world position as the bottom left of the object and track every object this way. The problem with this is I would have to translate it to an on screen position on rendering. The positive with this is I could easily visualize (especially in the case of objects made of multiple tiles) how something is structured and needs to be built.

Are there standard ways for doing this? Should I just suck it up and get used to positions beginning in the top left?

Here are the OpenGL calls to start rendering:

// enable textures since we're going to use these for our sprites
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// enable alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// disable the OpenGL depth test since we're rendering 2D graphics
glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);

I assume I need to change:

glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);

to:

glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
share|improve this question
1  
OpenGL lets you set up the coordinate system just how you want it. If you look at glOrtho it should be quite straightforward how to do it. –  API-Beast Jun 12 '12 at 20:03
    
Edited to include that part of the code. If you want to supply the answer I'll mark it as correct (if I can... not sure if I have enough rep yet). –  Magicked Jun 12 '12 at 20:27

1 Answer 1

There isn't a standard way to do this; each game or engine will (and should) elect to use the coordinate model that makes the most sense for it.

That said, it's generally standard practice to normalize the coordinate model, so everything works the same where viable (it makes it easier to think about). That means it's generally better to have your logical world origin and rendering origins align (in the lower left or upper left, for example).

Most 3D APIs provide mechanisms to support this through the configuration of the projection matrices or similar. As you speculated, if you are using orthographic projection in OpenGL, you can just flip the parameters to glOrtho accordingly to adjust how your coordinate system appears.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.