title: How do I check collision between 2 square objects on x and y? (p5.js)
body: <p>How do I test collisions between 2 square objects?
  I have a player, and a block object, and i want to check if they are colliding whit each other.</p>
  
  <p>I've tried to use a lot of collision detections algos, but they seem to not work in my project, or i just didnt't code them right.</p>
  
  <p>This is my player collision function, at the start it has defined x,y positions and a grounded variable.</p>
  
  <pre><code>this.testCollisions = function(other){
      if (this.x+20 &lt; other.x || this.x &gt; other.x+other.w ||
          this.y+20 &lt; other.y || this.y &gt; other.y+other.h) {
        print("collision")
        this.grounded = true;
      } else {
        this.grounded = false;
      }
    }
  </code></pre>
  
  <p>It thinks that the object is somewhere down, and also it has infinite x axis?
  the important variables in the block object for collision are:</p>
  
  <pre><code>this.x = x; // float
  this.y = y;
  this.h = 40;
  this.w = 40;
  </code></pre>
  
  <p>It also starts going down at the start, even tho i have set it to be on the block at the start.
  Thank you for your time.</p>
  
  <p><a href="https://pastebin.com/b4Uqyv9z" rel="nofollow noreferrer">Here is my full code ( each function NAME(){} is a new file)</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function Player(){
      this.x = width/2+10;
      this.y = height/2-20;
      this.grounded = true;
      this.show = function(){
          fill(255);
          square(this.x,this.y,20);
      }
      this.testCollisions = function(other){
          if (this.x+20 &lt; other.x || this.x &gt; other.x+other.w ||
              this.y+20 &lt; other.y || this.y &gt; other.y+other.h) {
                print("collision")
                this.grounded = true;
          } else {  
                this.grounded = false;
          }
      }
      this.affectGravity = function(){
          if (!this.grounded)
              this.y+=1;
      }
  }
   
  function Block(x,y,grassed){
      this.grassed = grassed; // bool
      this.x = x; // float
      this.y = y;
      this.h = 40;
      this.w = 40;
      this.gh = 15;
      this.gw = 40;
      this.render = function(){
          if (this.grassed){
              fill("#AF7250");
              rect(this.x,this.y,this.w,this.h);
              fill("#869336");
              rect(this.x,this.y,this.gw,this.gh);
          }
      }
  }
   
  var block;
  var player;
  var grounded = true;
  function setup() {
      createCanvas(400, 400);
      block = new Block(height/2,width/2,true);
      player = new Player();
  }
   
  function draw() {
      background(120);
      block.render();
      player.show();
      if (keyIsDown(LEFT_ARROW)){
          player.x --;
      } else if (keyIsDown(RIGHT_ARROW)){
          player.x ++;
      }
      strokeWeight(1);
      player.testCollisions(block);
      player.affectGravity();
      console.log(player.grounded);
  }
  function keyPressed(){
      if (player.grounded &amp;&amp; keyCode === 32){
          for (let i = 0; i &lt; 10; i++)
              player.y-=1;
      }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>