title: breakout collision detection using vectors
body: <p>I am working on a breakout game using C++ and OpenGL. I have stubbed out some code using straight C++. I am  using a vector to store objects. </p>
  
  <p>I want to streamline my collision detection code.I am working on breakout game using vectors to store brick values in text based c++ to start with. Well I am attempting to use a vector as a dynamic array. Basically I want to store the values of the bricks in a vector. I then want to iterate through the vector in order to determine if a brick is hit by the ball. I am able to detect if a single brick is hit by the ball but I want to determine if a vector of brick values are hit by a ball. In other words instead of using a collision detection loop for each and every single brick one at a time but I want the vector to store the values for the bricks so that I can iterate through them dynamically.</p>
  
  <p>My code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class Brick
  {
  public:
      float x;
      float y;
      float brick_x;
      float brick_y;
      float brickWidth;
      float brickHeight;
  };
  
  int main()
  {
      vector&lt;Brick&gt; brick;
      Brick playerBrick;
      Brick playerBrick_two;
  
      playerBrick.x = 30.0f;
      playerBrick.y = 20.0f;
      playerBrick.brick_x = 20.0f;
      playerBrick.brick_y = 10.0f;
      playerBrick.brickWidth = 60.0f;
      playerBrick.brickHeight = 20.0f;
  
      playerBrick_two.x = 40.0f;
      playerBrick_two.y = 30.0f;
      playerBrick_two.brick_x = 30.0f;
      playerBrick_two.brick_y = 20.0f;
      playerBrick_two.brickWidth = 60.0f;
      playerBrick_two.brickHeight = 20.0f;
  
      brick.push_back(playerBrick);
      brick.push_back(playerBrick_two);
  
      for (int i = 0; i &lt; 2; i++)
      {
          cout &lt;&lt; brick[i].x &lt;&lt; " " &lt;&lt; brick[i].y &lt;&lt; " " &lt;&lt; brick[i].brick_x &lt;&lt; " " &lt;&lt; brick[i].brick_y &lt;&lt; " " &lt;&lt; brick[i].brickWidth &lt;&lt; " " &lt;&lt; brick[i].brickHeight &lt;&lt; endl;
      }
      for (int i = 0; i &lt; 2; i++)
      {
          if (brick[i].x &gt; brick[i].brick_x &amp;&amp; brick[i].x &lt; brick[i].brick_x + brick[i].brickWidth &amp;&amp; brick[i].y &gt; brick[i].brick_y &amp;&amp; brick[i].y &lt; brick[i].brick_y + brick[i].brickHeight)
          {
              cout &lt;&lt; "Collision" &lt;&lt; endl;
          }
      }
  
  void bricks_eight()
  {
      glColor3f(0.8f, 0.0f, 0.0f);
      glRectf(50.0f, 60.0f, 70.0f, 50.0f);
      glRectf(70.0f, 60.0f, 90.0f, 50.0f);
      glRectf(90.0f, 60.0f, 110.0f, 50.0f);
      glRectf(110.0f, 60.0f, 130.0f, 50.0f);
      glRectf(130.0f, 60.0f, 150.0f, 50.0f);
      glRectf(150.0f, 60.0f, 170.0f, 50.0f);
  }
  </code></pre>
  
  <p>Well I am posting a row of bricks I want to eliminate when the ball strikes with them. Since I have several rows of bricks I want to use a collision detection function that checks for a ball and brick collision using a vector. The x and y variables are the ball coordinates and the brick_x and brick_y variables and the brick coordinates.</p>
  
  <p>I adjusted Makogan's code but it still does not check for a lot  collisions all at once.</p>
  
  <pre><code>``
  class Brick
  {
  public:
      float x;
      float y;
      float brick_x=0.0f;
      float brick_y=0.0f;
      float brickWidth=20.0f;
      float brickHeight=10.0f;
      bool TestCollision(float x, float y)
      {
          if (x &gt; brick_x &amp;&amp; x&lt;brick_x + brickWidth &amp;&amp; y &gt; brick_y &amp;&amp; y &lt; brick_y + brickHeight)
          {
              return true;
          }
          return false;
      }
  };
  
  class BrickLayer
  {
  public:
      vector&lt;Brick&gt; bricks{(5.0f,5.0f)};
      bool TestCollisions(float x, float y) {
          for (auto&amp; brick : bricks) if (brick.TestCollision(x, y)) return true;
          return false;
      }
  };
  
  int main()
  {
      BrickLayer brick;
      cout &lt;&lt; brick.TestCollisions(5.0f,5.0f)&lt;&lt; endl;
  
  
      system("pause");
      return 0;
  }
  </code></pre>