2

I am kinda new to all this but I am trying to make myself a simple 2D game in c++. I have decided to do a kind of a maze type game and what I am doing for this is drawing out the maze as a texture and then having another texture as my character move around inside this maze.

However I am hugely struggling with the collision detection so that my character doesn't just walk through the walls. I have been told that I can use glReadPixels to find the colour of the background but whenever I try this it just ignores the colour and still continues on through the walls.

Can anybody please help me with this and tell me how I can do it as I cannot find anything anywhere which can help. Thanks in advance.

user1324894
  • 35
  • 1
  • 6

1 Answers1

1

Depending on the maze type, if you only have vertical and horizontal walls of unit length, you could reprezent the maze and current position in a 2D array/matrix and decide whether the new position is OK to move into based on the content of the new position in the maze matrix.

You will have to do some translation to/from matrix coordinates and screen coordinates

Advantages:

  • you don't need to read from the screen
  • maze can be larger than what fits on the screen -- draw the relevant portion only

Disadvantages:

  • you can only have "bolck" type terrain (e.g. vertical/horizontal walls)
  • if you want to add movign enemies, the collision detection can be too coarse (you cannot avoid the monster "just by a hair/pixel")
Attila
  • 28,265
  • 3
  • 46
  • 55
  • They are just vertical and horizontal walls but it is all on one big texture. How would I go about creating the array/matrix and updating it? I'm really bad at this I know sorry. – user1324894 Apr 10 '12 at 19:31
  • I would break the large texture into small uniform size tiles (e.g. wall, pathway, door, etc) then assable it dynamically onto the screen based on the values in the maze matrix (e.g. the value 0 in the matrix represents path -- walkable, 1 represents wall -- blocked). Then you can do all your calculations on the matrix and render the state of the maze onto the screen -- the advantage is that it is more flexible than one large texture. YOu can reuse the same tiles, (saving disk space); dynamically change the maze (e.g. closed door vs. open door); etc. – Attila Apr 10 '12 at 19:39
  • Ah I think I'm starting to understand where you are coming from so it would be a square array of smaller textures that would all fit together to make the map? Because what I am doing at the moment is drawing it as one texture and to move the character around I move it in one direction and at the same time move the map in the opposite direction to be able to move around it. Would that still be the case if I changed it to the array of tiles? – user1324894 Apr 10 '12 at 19:51
  • Bit cheeky to keep asking questions but how would I go about doing that? All this is just getting me so confused and need someone to explain it in simple terms if possible haha – user1324894 Apr 10 '12 at 19:56
  • Which parts do you have problems understanding? I suggest you try to implement something outlined here bit-by-bit -- for example, create a maze matrix of 0s and 1s, then draw that on the screen. Once done, associate tiles to the 0/1s, draw those. Once done, implement movement/collision checking outlined in my answer. etc. If you are stuck or have a specific question I suggest you post another question targeting that specific problem. – Attila Apr 10 '12 at 20:05
  • Ah I think I have found something that outlines how to do what I need bit by bit now. Thank you so much you have been great at helping me work this out – user1324894 Apr 10 '12 at 20:13