Z Ordering
I'm trying to find an efficient way to use Z ordering for a 2D game. My idea is this: anything with a Z != 1 will be drawn from lowest Z value to highest Z value. anything with a Z == 1 will be drawn from lowest to highest (Y-H) value (bottom of object). I thought to create an array of CObject but some of the things are of class CPlayer, others CObstacle, etc (many many classes). Is there any ideas of how to accomplish this or something close to this?
Edit:
To help make sense, I have the screen set up with (0,0) in the top left with the X increasing towards the right and the Y increasing towards the bottom.
Edit:
To help make sense, I have the screen set up with (0,0) in the top left with the X increasing towards the right and the Y increasing towards the bottom.
You can use the depth buffer of OpenGL when drawing your quads. I use another method though. I assign a locZ to all my sprites, create a linked list with all the sprites, and then sort the list out with a method I found on the web.
Look for SortLinkedList() on FunctionsUtil.c at http://webpages.charter.net/utopiaplanet...Struct.dmg
Look for SortLinkedList() on FunctionsUtil.c at http://webpages.charter.net/utopiaplanet...Struct.dmg
It just occurred to me that Nick is asking a lot of questions that are probably common to a lot of beginner developers- if anyone has some spare time, it might be nice to add all his questions to a faq or what have you.
Anyway, if you're using STL (and in an earlier post you used a vector, so I assume you are), there is no need to write your own linked list structure, or sorting routine. You can have STL sort for you, for example based on the < operator of your class. Here's a page that shows a simple example: http://www.msoe.edu/eecs/ce/courseinfo/stl/sort.htm
For your case you might have something like this (forgive me if it's a little off, i'm not going to test it):
Anyway, if you're using STL (and in an earlier post you used a vector, so I assume you are), there is no need to write your own linked list structure, or sorting routine. You can have STL sort for you, for example based on the < operator of your class. Here's a page that shows a simple example: http://www.msoe.edu/eecs/ce/courseinfo/stl/sort.htm
For your case you might have something like this (forgive me if it's a little off, i'm not going to test it):
Code:
class CObject {
int y, z;
bool operator<(CObject& x){
if(x.z<z) return 1;
if(x.z>z) return 0;
if(x.y<y) return 1;
return 0;
}
}
typedef vector<CObject> vecCObject;
vecCObject v;
... add stuff to v ...
sort(v.begin(), v.end());
for( vecCObject::iterator i = v.begin(); i!=v.end(); i++)
... draw (*i) ...
Your other question: I thought to create an array of CObject but some of the things are of class CPlayer, others CObstacle... you can solve with inheritance, and still use the code above, except using pointers (i.e. vector<CObject*> instead of vector<CObject>.) You can have CPlayer, CObstacle, CEnemy, etc. inherit CObject, and then you can add anything you want that inherits CObject to your vector, and they will all sort the same way.
Ok thanks for the help. Yes if somebody could make this a FAQ I'm sure it will help many people in the future. Actually, I'm planning on writing on online tutorial/help/FAQ on everything I'm learning sometime because I have found it impossible to find one site that teaches all the beginning aspects to creating a 2D game with OpenGL. But I need to learn enough to make a game before I can write about making a game.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| graphic image ordering question | WhatMeWorry | 12 | 4,368 |
Mar 2, 2007 01:03 PM Last Post: akb825 |
|

