[LibGDX] Intersector.overlaps(r1, r2) not properly detecting overlap.

So basically in my game, the character has a Rectangle around him, and the collidable tiles have a rectangle around them.

Whenever I check for a collision, with the Intersector.overlaps(r1, r2) method, it only detects on the original point of r1(The bottom left corner), and nowhere else. I’ve tried this with Intersector.intersectRectangles() and Rectangle.overlaps(Rectangle) as well, and they do the same thing so I’m convinced it’s a problem with my code.

I’ve rendered the rectangles using ShapeRenderer so I’m sure they’re in the proper positions.

This is the code for the blocked(Rectangle rect) method in my Map class. This looks through an array of rectangles(called data[][]) where the non collidable tiles are null, and the collidable ones have a rectangle object in their place.

Here is the code for my Map class:


public class Map {
	
	private TiledMap tileMap;
	private int tileMapWidth;
	private int tileMapHeight;
	private int tileSize;
	private OrthogonalTiledMapRenderer tmRenderer;
	
	private Rectangle[][] data;
	
	ShapeRenderer shapeRenderer = new ShapeRenderer();
	
	public Map(String path) {
		initTMX(path);
		data = new Rectangle[tileMapWidth][tileMapHeight];
		System.out.println(data.length + ", " + data[0].length);
		createCollision();
	}
	
	private void initTMX(String path) {
		this.tileMap = new TmxMapLoader().load(path);
		tileMapWidth = tileMap.getProperties().get("width", Integer.class);
		tileMapHeight = tileMap.getProperties().get("height", Integer.class);
		tileSize = tileMap.getProperties().get("tilewidth", Integer.class);
		tmRenderer = new OrthogonalTiledMapRenderer(tileMap);		
	}
	
	private void createCollision() {
		TiledMapTileLayer layer = (TiledMapTileLayer) tileMap.getLayers().get("collision");
		
		for (int row = 0; row < layer.getHeight(); row++) {
			for (int col = 0; col < layer.getWidth(); col++) {
				Cell cell = layer.getCell(col, row);
				
				System.out.println(col + ", " + row);
				
				if (cell == null) {
					data[col][row] = null;
				} else if (cell.getTile() == null) {
					data[col][row] = null;
				} else {
					data[col][row] = new Rectangle(col * 16, row * 16, 16, 16);
				}
			}
		}
	}
	
	public void update(float dt) {
		
	}
	
	public void render(OrthographicCamera cam) {
		tmRenderer.setView(cam);
		tmRenderer.render();
		
		shapeRenderer.begin(ShapeType.Line);
		shapeRenderer.setColor(Color.GREEN);
		for (int col = 0; col < data.length; col++) {
			for (int row = 0; row < data[0].length; row++) {
				if (data[col][row] == null) continue;
				shapeRenderer.rect(data[col][row].x, data[col][row].y, data[col][row].width, data[col][row].height);
			}
		}
		shapeRenderer.end();
	}
	
	public void dispose() {
		
	}
	
	public int realPos(int tileX, int tileY) {
		int xpos = tileX * tileSize;
		int ypos = tileY * tileSize;
		return (xpos + ypos * (tileMapWidth * tileSize));
	}
	
	public boolean blocked(Rectangle rect) {
		int xx = (int) rect.x / 16;
		int yy = (int) rect.y / 16;
		System.out.println(xx + ", " + yy);
		if (data[xx][yy] == null) return false;
		else if (Intersector.overlaps(rect, data[xx][yy])) return true;
		return false;
	}
	
	public int getWidth() {
		return tileMapWidth;
	}
	
	public int getHeight() {
		return tileMapHeight;
	}
}

Here is my Entity class


public class Entity {
	
	private Rectangle rectangle;
	
	private Map map;
	
	private AtlasRegion ar;
	
	ShapeRenderer shapeRenderer = new ShapeRenderer();
	
	public Entity(AtlasRegion ar, Map map, float x, float y) {
		this.ar = ar;
		this.map = map;
		this.rectangle = new Rectangle(x * 16, y * 16, ar.getRegionWidth(), ar.getRegionHeight());
	}
	
	public boolean move(float dx, float dy) {
		float nx = rectangle.x + dx;
		float ny = rectangle.y + dy;

		Rectangle nr = new Rectangle(nx, ny, rectangle.width, rectangle.height);
		
		if (validLocation(nr)) {
			rectangle.x = nx;
			rectangle.y = ny;
			return true;
		}
		return false;
	}
	
	public boolean validLocation(Rectangle rectangle) {
		if (map.blocked(rectangle)) return false;
		return true;
	}
	
	public void render(SpriteBatch sb) {
		sb.begin();
		sb.draw(ar, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
		sb.end();
		shapeRenderer.begin(ShapeType.Line);
		shapeRenderer.setColor(Color.RED);
		shapeRenderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
		shapeRenderer.end();
	}
}

The move method is called from within the game state like this:


	@Override
	public void handleInput(float dt) {
		float dx = 0;
		float dy = 0;
		
		float speed = 60f;
		
		if (Input.isDown(Input.LEFT)) {
			dx -= speed;
		}
		if (Input.isDown(Input.RIGHT)) {
			dx += speed;
		}
		if (Input.isDown(Input.UP)) {
			dy += speed;
		}
		if (Input.isDown(Input.DOWN)) {
			dy -= speed;
		}
		
		if ((dx != 0) || (dy != 0)) {
			player.move(dx * dt, dy * dt);
		}
	}