npressfetimg-5216.png

breakout in opengl and c++ – For Beginners – GameDev.net

C++ Tutorials

Using some tutorial surely is no bad idea, because a breakout game is much harder than the questions you ask usually here in the forum.

pbivens67 said:
I am still confused on how to store bricks in an 2d array,

The tutorial a linked above uses a vector, but it’s size is kept constant, with zeros representing empty space. That’s fine and the same i have meant with ‘array’. The ‘bad’ way would be to to have a struct with position for each brick, and deleting bricks from the dynamic vector if hit by the ball, and also needing to iterate all bricks each time we want to know if there is a brick at position (x,y). Just to make that clear.

To create and access a board of bricks, it would be convenient to use an array of char:

char *board =
  "9999999999999999"
  "9000000000000009"
  "9001111111111009"
  "9002222222222009"
  "9000000000000009"
  "9999999999999999"
;

This example creates a board of size 16 x 6 (the compiler should joint multiple lines to just one long string, but not 100% sure this works as expected).

If we want to look up the brick at position (x:3, y:2) we calculate the index into the 1D array accordingly:

char brick = board[x + y*16];

Notice i have put a ‘9’ on the boundary of the board.
If we make 9 an undestructable brick, we do not need extra code to handle the boundary at all, like your code example (the tutorial also missed missed to do this).
But for rendering we likely want to clip that boundary and render only the inner 14 x 4 region of the board.

The tutorial seemingly does no continuous collision detection (skimmed over it just quickly), so it can only handle slowly moving balls. That’s bad because Breakout becomes boring if you need to long wait periods because the slow ball became trapped into some area where it won’t come back to the paddle quickly.

The tutorial also calculates exact circle – aabox collision, which is not needed because we do not want to handle ball-box corner cases, as this would cause unpredictable ball movement. So at this point the tutorial is more complicated than needed, IMO. In this case, introduced ‘inaccuracy’ from approximating the collision representation with just ‘ray vs. horizontal and vertical lines, ignoring ball radius but extruding bricks instead’ is exactly what we want for gameplay.

But somehow i doubt you would find a better tutorial, and if my proposal sounds more confusing than meaningful, i’d go with it.

Source: https://www.gamedev.net/forums/topic/710072-breakout-in-opengl-and-c/5439612/