Question

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.

Was it helpful?

Solution

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top