title: Libgdx intersection of polygons
body: <p>I'm learning libgdx by adding some more features to the open source jumper game from Mario Zechner. I'm trying to make some platforms with an angle and run into the problem of collision detection of rotated rectangles.<br>
  I followed <a href="http://www.java-gaming.org/index.php?topic=28930.0" rel="nofollow noreferrer">this</a> solution and used Polygons along with my rectangle bounds.<br>
  For testing purposes I don't set an angle yet. I just want to verify that bob jumps correctly off the platforms. But for some reason this doesn't work. the bounds are either too far to the left, above the platform, or not there at all. Am I not setting the polygon correctly?
  Would it be easier to use Box2d? I don't have any experience with that and I'm wondering if that's overkill for simple platforms.</p>
  
  <pre><code>public PlatformClient(int platformType, float x, float y) {
      this.platformType = platformType;
      float x1 = x - Platform.PLATFORM_WIDTH/2;
      float y1 = y + Platform.PLATFORM_HEIGHT/2;
  
      this.polyBounds = new Polygon(new float[]{x1, y1, x1+Platform.PLATFORM_WIDTH, y1, x1+Platform.PLATFORM_WIDTH, y1-Platform.PLATFORM_HEIGHT, x1, y1-Platform.PLATFORM_HEIGHT}); 
      polyBounds.setPosition(x-Platform.PLATFORM_WIDTH/2, y-Platform.PLATFORM_HEIGHT/2);
  }
  
  class Platform {
     public static final float PLATFORM_WIDTH = 2f;
     public static final float PLATFORM_HEIGHT = 0.35f;
  
  }
  </code></pre>
  
  <p>In Bob class update the polygon bounds when he moves:</p>
  
  <pre><code>public void update(float deltaTime) {   
      ...
      position.add(velocity.x * deltaTime, velocity.y * deltaTime);
      bounds.x = position.x - BOB_WIDTH / 2;
      bounds.y = position.y - BOB_HEIGHT / 2;
      float newX = position.x - BOB_WIDTH / 2;
      float newY = position.y - BOB_HEIGHT / 2;
      polyBounds.setVertices(new float[]{ 
              newX, newY, 
              newX+BOB_WIDTH, newY, 
              newX+BOB_WIDTH, newY-BOB_HEIGHT,
              newX, newY-BOB_HEIGHT});
  }
  </code></pre>
  
  <p>In World class:</p>
  
  <pre><code> private void checkPlatformCollisions () {
     int len = platforms.size();
     for (int i = 0; i &lt; len; i++) 
     {
        PlatformClient platform = platforms.get(i);
        if (bob.position.y &gt;= platform.position.y) 
        {
           if(Intersector.overlapConvexPolygons(bob.polyBounds, platform.polyBounds))
           {
                 System.out.println("it overlaps");
                 // jump off platform
           }
        }
     }
   }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks to the shape renderer, I was able to set the polygons correctly. I fixed some +,- issues in the code above. But the following code: <em>Intersector.overlapConvexPolygons()</em> still doesn't work (see image). He jumps before the polygons are making contact or he doesn't jump at all.
  Any further ideas?</p>
  
  <p><img src="https://i.stack.imgur.com/mu6Ct.png" alt="enter image description here"></p>
  
  <p>That's how I draw the polygon of Bob and the platforms that clearly overlap.</p>
  
  <pre><code> public void render() {
      shapeRenderer.setProjectionMatrix(cam.combined);
      shapeRenderer.begin(ShapeType.Line);
      for(int i=0; i&lt;world.platforms.size(); i++) {
  
          shapeRenderer.setColor(1, 0, 0, 1);
  
          shapeRenderer.polygon(world.platforms.get(i).polyBounds.getVertices());
          shapeRenderer.polygon(world.bob.polyBounds.getVertices());
      }
      shapeRenderer.end();
     }
  </code></pre>