user2246120
user2246120

Reputation: 1505

Libgdx intersection of polygons

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.
I followed this solution and used Polygons along with my rectangle bounds.
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.

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;

}

In Bob class update the polygon bounds when he moves:

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});
}

In World class:

 private void checkPlatformCollisions () {
   int len = platforms.size();
   for (int i = 0; i < len; i++) 
   {
      PlatformClient platform = platforms.get(i);
      if (bob.position.y >= platform.position.y) 
      {
         if(Intersector.overlapConvexPolygons(bob.polyBounds, platform.polyBounds))
         {
               System.out.println("it overlaps");
               // jump off platform
         }
      }
   }
 }

EDIT

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: Intersector.overlapConvexPolygons() still doesn't work (see image). He jumps before the polygons are making contact or he doesn't jump at all. Any further ideas?

enter image description here

That's how I draw the polygon of Bob and the platforms that clearly overlap.

 public void render() {
    shapeRenderer.setProjectionMatrix(cam.combined);
    shapeRenderer.begin(ShapeType.Line);
    for(int i=0; i<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();
   }

Upvotes: 4

Views: 4229

Answers (1)

user2246120
user2246120

Reputation: 1505

ok, I solved it by removing

polyBounds.setPosition(x-Platform.PLATFORM_WIDTH/2, y-Platform.PLATFORM_HEIGHT/2);

from the constructor. Now the collision works correctly.

Upvotes: 3

Related Questions