3
votes

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();
   }
1
Have you tryed printing the shapes with ShapeRenderer for debug purposes? - Springrbua
I'm using SpriteBatch not ShapeRenderer. According to this I shouldn't mix these two. - user2246120
I know you shouldn't mix them. But for debug purposes it is better to use ShapeRenderer to draw your exact shapes. The images you draw with spritebatch could be bigger smaller or even on another Position then your shapes. So just call spritebatch.end() then shaperenderer.begin(Shapetype.LINE) then draw your polygon call shaperenderer.end() and spritebatch.start() again. I tryed to do polygon collision detection once and for some reason my polygon was non convex and it didn't work. So ist better you try it! - Springrbua
thanks, I was able to set the polygons correctly. But it still isn't working (see my edit in the original post) - user2246120
Because if you render with spritebatch you set the position Rotation etc. manually. And the shape always depends on the image. It happened to me, that i set the Points of the polygon in the wrong order and the shape was like this: __ \/ /\ And so the collision detection could not work. Pls answer your question and mark it as solved. Thanks - Springrbua

1 Answers

2
votes

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.