views:

966

answers:

5

Hi, I was wondering if anyone can get me started on how to build like a maze using openGL. Would i need to write code to draw the maze or is it like painting a picture in a separate window? would i need to create a game engine to start with this? I'm doing this for iphone development so any suggestions for that would be a great start for me. Thanks for your responses everybody.

+1  A: 

Use a map editor. Get started with something like QuArK.

Also check out Game Level Builder. This may be more than what you are looking for, but I thought I'd reference it here anyway.

bobobobo
+2  A: 

The code to generate the maze should be independent from the drawing. You could try to generate the visual in a console first and then using OpenGL.

This is the kind of layout you could use in the console, where S == Start et E == End.

+=+=+=+
|S|   |
+ + + +
|   |E|
+-+-+-+

The underlying structure could be a two-dimensional array of structs which tell where you can move from a specific position in the maze :

struct {
  bool Left;
  bool Right;
  bool Up;
  bool Down;
}

Now to generate the maze, I'm sure there are plenty of ways to do it. A simple solution: Initialize the maze with walls (every boolean to false) and then make a walkable path between S and E. Then randomly set the booleans without affecting your path.

Have fun :)

Jodi
A: 

You may not even need to use OpenGL for this. John Blackburn has a post where he shows how to make a maze-like structure using only Core Animation. If the performance of such a solution was good enough for you, it could save a lot of code.

Brad Larson
A: 

Hello, I have done this, and what I recommend you to do, is a mix of what has been said before.

1) Write an algorithm capable of actually creating a maze, there are several options here, some pretty simple ones ( IMHO ) , are Kruskal and Primm. You should take a look Here

2) If you are planning to do some sprite work, or game where things actually move, I would recommend to use a pre-made game engine, just to save some work to you. If you want to get deeper into game engine techniques, you will be able to do so later. An excellent OpenSource 2d game engine called Cocos2D is what I would recommend.

Good luck with that, maze generation was the way I got into C++ ( and then out of it :P ), a few years ago.

Mr.Gando
+2  A: 

A very simple method, which will get you some results similar to Wolfenstein 3D (but with OpenGL's goodness is much easier :-P) is to have the world made of a 2D grid like:

+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+

Each cell of the should contain information about the cell such as if the cell is empty or solid, if it is solid what kind of material it has or if it is empty what material the floor and the ceiling have. If it is empty you can also add additional information such as light casting, reference to some entity (for adding game models or other stuff), logic flags (such as flag for "exit" or "start" or "hazard"), etc. A simple structure would be something like (in C):

#define SOLID 0x0001
#define START 0x0002
#define EXIT 0x0004
#define HAZARD 0x0008

struct _cell_t
{
    unsigned int flags;
    GLuint texture;
    entity_t* entity;
} cell_t;

In the above structure, flags is the cell's flags. If the SOLID flag is set, the cell is solid (wall), if the START flag is set, the cell is the player's start position, if the EXIT flag is set the cell is the maze's exit position and finally if the HAZARD is set the cell causes damage to the player. Of course you can add more flags there (like a LIGHT flag that casts light, SECRET flag that marks the cell as part of a secret area, etc), but you get the idea :-).

The texture field is an OpenGL ES texture name (generated with glGenTextures and filled with glTexImage2D). This is used for rendering the maze (here i use only a single texture for simplicity but if you want to show more interesting worlds you should specify four textures for solid cells and two textures for empty cells - the four textures will be textures for each side of a cell/wall and the two textures will be the cell's floor and ceiling).

The entity field is just a reference to an entity, if you want to add entities. A static entity (like a vase, lamp, table, etc) usually is made of a mesh and a material. If you want to add dynamic entities of course (such as monsters, pick up items, etc) you'll need to add animation information to the entity structure and keep a list of the "dynamic" entities separate from the cell because these will move around the world and thus won't "belong" to a cell (for optimization reasons you might want to assign a "temporary" entity in each cell though, but optimization is something you should bother with after you have the engine/game running).

Now to render the maze, just render all the cells (later you might want to do some flood fill or raycasting tests to render only what is visible, but again don't worry about that now). For each cell check if the cell's flags include the SOLID flag and if so render the four quads that make up the cell (imagine a cube without a top and a bottom). If the cell has not the SOLID flag, render the two quads for the cell's floor and ceiling (imagine the same cube but now with only a top and a bottom). If the cell you are rendering has an entity field that is not null, then also render that entity.

If you do that, you'll have yourself a maze :-). Some optimization tips:

  • You'll quickly realize that you are rendering many quads which will never be seen. If this does have a speed impact (and after you tried to render only the visible cells), you might want to extend the structure containing flags for which walls of a cell are actually there. For example imagine these three cells in a row: ABC. the cells A and C have three walls (left, up, down for A and right, up, down for B) and the cell B has two walls (up and down). To mark walls as being there or not, for each cell check the surrounding cells.
    • If a cell has a SOLID cell above it, its "up" wall does not exist. Similarly for its left cell, right cell and cell below.
    • If a cell is at the edge of the map, one wall is always missing. If it is at a corner, two walls are always missing.
    • Do this calculation when you are initializing the game, not when rendering!
    • Do these tests after you have figured out which cells are visible. Before you start writing the code for this, keep a backup (hopefully you'll use some version control system like Subversion, but even if not just copy the code somewhere) and compare the results of both versions. It might actually be faster -especially on iPhone 3GS- to not remove empty walls but use the same mesh for all walls using a VBO.
  • If you add dynamic entities and have the "temporary entity" i mentioned above, you can actually discard invisible dynamic entities automatically once you have a method to figure out which cells are visible and which not: if a cell is invisible, its associated dynamic entity will also be invisible (but beware: an entity can touch more than one cell).
  • A very simple but very crude method to not render all cells, is to only render the cells around the camera, for example:

.

+-+-+-+-+-+-+-+
| |X|X|X| | | |
+-+-+-+-+-+-+-+
| |X|C|X| | | |
+-+-+-+-+-+-+-+
| |X|X|X| | | |
+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+

(here C is the cell where the camera is and X are the cells around the camera - of course i used only cells which are only once cell apart from the camera for illustration purposes but you should use more than that - like 16 cells for example. Just make sure your maps will never have an area with empty space that can see more than 16 cells or whatever you decide)

Of course casting rays will probably produce better results :-).

Well, i think that is enough information to give you a starting point :-). Of course Stackoverflow is here for any other question you might have :-P

Bad Sector