title: Raylib X Collision Response Isn't Working Properly
body: <p>I've been making a game with C and Raylib, and I've managed to get the collisions on the Y axis working, but not on the X axis. This is what happens when the player lands on the top of
  a rectangle in the game:</p>
  <p><a href="https://i.stack.imgur.com/BvOIK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BvOIK.png" alt="enter image description here" /></a></p>
  <p>I've tried adjusting the collisions in every way possible, but I still have not gotten it to work. I want the player to be able to move freely on top of the block, and when the player falls off the block, be able to slide down the block but not go through it, just like a normal solid platform block. Currently, the player gets stuck on the edge of the block (as seen from the image), and slides down. Anytime the player tries to land on top of the block, the player gets teleported to the corner (as seen on the image).</p>
  <p>Here is my collision code:</p>
  <pre class="lang-c prettyprint-override"><code>        for (int i = 0; i &lt; MAX_BUILDINGS; i++) {
              if (CheckCollisionRecs(playerArea, buildings[i])) {
                  DrawText(&quot;Collided&quot;, 10, 10, 25, BLACK);
              
                  //Y COLLISIONS
                  if (player.vel.y &gt; 0) {
                      player.pos.y = buildings[i].y - player.height;
                      player.vel.y = 0;
                      player.acc.y = 0;
                      player.canJump = true;
                  }
  
                  //X COLLISIONS
                  if (player.vel.x &lt; 0) { // If the player moved left and collided with the right side of block
                          player.pos.x = buildings[i].x + buildings[i].width;
                      } else { // If the player moved right and collided with the left side of block
                          player.pos.x = buildings[i].x - player.width;
                      }
                      player.vel.x = 0;
              }
          }
  </code></pre>
  <p>I'm just looping through an array of rectangles, called buildings, and then checking collisions for all of them with <code>buildings[i]</code>.</p>
  <p>Here is my full code if needed:</p>
  <pre class="lang-c prettyprint-override"><code>#include &quot;raylib.h&quot;
  
  #include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define MAX_BUILDINGS 100
  
  
  
  int main(void) {
  
      const int screenWidth = 1600;
      const int screenHeight = 1000;
  
      InitWindow(screenWidth, screenHeight, &quot;Window Title&quot;);
  
      /*---STRUCTS---*/
      typedef struct {
          int FPS;
          int frames;
      } Window;
  
      Window window = {
          .FPS = 60,
          .frames = 0,
      };
  
      Rectangle buildings[MAX_BUILDINGS] = { 0 };
      Color buildColors[MAX_BUILDINGS] = { 0 };
      int buildRotation[MAX_BUILDINGS] = { 0 };
  
      /*---BUILDINGS---*/
      int spacing = screenWidth/2;
  
      for (int i = 0; i &lt; MAX_BUILDINGS; i++) {
          buildings[i].width = (float)GetRandomValue(750, 1000);
          buildings[i].height = screenHeight*2;
          buildings[i].y = (float)GetRandomValue(200, 500);
          buildings[i].x = spacing;
  
          spacing += (int)buildings[i].width + GetRandomValue(250, 500);
  
          buildColors[i] = BLUE;
          buildRotation[i] = 0;
  
      }
  
      typedef struct {
          float width;
          float height;
          Vector2 pos;
          Vector2 vel;
          Vector2 acc;
  
          double accSpeed;
          int maxVel;
          double friction;
          double rotation;
          double scale;
  
          float jumpForce;
          float gravity;
          bool canJump;
      } Player;
  
      Player player = {
          .width = 50,
          .height = 100,
          .pos = {buildings[0].x + buildings[0].width/2 - player.width/2, buildings[0].y - player.height},
          .vel = {0, 0},
          .acc = {0, 0},
  
          .accSpeed = 0.15,
          .maxVel = 7,
          .friction = 0.2,
          .rotation = 0,
          .scale = 0.5,
  
          .jumpForce = 20,
          .gravity = 0.5,
          .canJump = true,
      };
  
     typedef struct {
          float x;
          float y;
          int speed;
      } Camera;
  
      Camera cameraPos = {
          .x = screenWidth/2,
          .y = screenHeight/2,
          .speed = 5,
      };
  
      Camera2D camera = { 0 };
      camera.target = (Vector2) {cameraPos.x, cameraPos.y};
      camera.offset = (Vector2) {screenWidth/2, screenHeight/2};
      camera.rotation = 0.0f;
      camera.zoom = 1.0f;
  
  
      SetTargetFPS(window.FPS);
  
      Rectangle playerArea;
  
      /*---MAIN GAME LOOP---*/
      while (!WindowShouldClose()) {
          window.frames++;
          // Update
          if (window.frames &gt;= 3*window.FPS) {
              camera.target.x += cameraPos.speed;
              camera.target.y = player.pos.y;
          }
          playerArea = (Rectangle) {    
              player.pos.x,                
              player.pos.y,                
              player.width,  
              player.height,
          };   
          //----------------------------------------------------------------------------------
          // TODO: Update your variables here
  
          for (int i = 0; i &lt; MAX_BUILDINGS; i++) {
              if (CheckCollisionRecs(playerArea, buildings[i])) {
                  DrawText(&quot;Collided&quot;, 10, 10, 25, BLACK);
              
                  //Y COLLISIONS
                  if (player.vel.y &gt; 0) {
                      player.pos.y = buildings[i].y - player.height;
                      player.vel.y = 0;
                      player.acc.y = 0;
                      player.canJump = true;
                  }
  
                  //X COLLISIONS
                  if (player.vel.x &lt; 0) { // If the player moved left and collided with the right side of block
                          player.pos.x = buildings[i].x + buildings[i].width;
                      } else { // If the player moved right and collided with the left side of block
                          player.pos.x = buildings[i].x - player.width;
                      }
                      player.vel.x = 0;
              }
          }
  
          if (IsKeyDown(KEY_LEFT) &amp;&amp; player.vel.x &gt; -player.maxVel) {
              player.acc.x = -player.accSpeed;
          } else if (IsKeyDown(KEY_RIGHT) &amp;&amp; player.vel.x &lt; player.maxVel) {
              player.acc.x = player.accSpeed;
          } else if (abs(player.vel.x) &gt; 0.2) {
              if (player.vel.x &lt; 0) {
                  player.acc.x = player.friction;
              } else {
                  player.acc.x = -player.friction;
              }
          } else {
              player.vel.x = 0;
              player.acc.x = 0;
          }
          player.vel.x += player.acc.x;
          player.pos.x += player.vel.x;
  
              if (IsKeyPressed(KEY_UP) &amp;&amp; player.vel.y == 0 &amp;&amp; player.acc.y == 0 &amp;&amp; player.canJump) {
                  player.canJump = false;
                  player.vel.y = -player.jumpForce;
              }
          player.acc.y += player.gravity;
          player.vel.y += player.acc.y;
          player.pos.y += player.vel.y;
          player.acc.y = 0;
          //----------------------------------------------------------------------------------
  
          // Draw
          //----------------------------------------------------------------------------------
          BeginDrawing();
              ClearBackground(RAYWHITE); 
  
              BeginMode2D(camera);  
              for (int i = 0; i &lt; MAX_BUILDINGS; i++) {
                  DrawRectanglePro(buildings[i], (Vector2) {0, 0}, buildRotation[i], buildColors[i]);
              }
              DrawRectangle(player.pos.x, player.pos.y, player.width, player.height, RED); 
              EndMode2D();
  
          EndDrawing();
          //----------------------------------------------------------------------------------
      }
  
      //--------------------------------------------------------------------------------------
      CloseWindow();        // Close window and OpenGL context
      //--------------------------------------------------------------------------------------
  
      return 0;
  }
  </code></pre>
  <p>Therefore, <strong>could someone please tell me why the collisions are broken for the X axis?</strong> Is it because of the order of my code in which the collisions are detected? Is my collision logic broken?</p>
  <p>Thanks for any help.</p>