Help with collision detection

Hi, i could really use some help with the collison detection for my tile game. There are alot of problem with this algorithm but my mainproblem is when the hero is standing on top off a block i cant get him to fall off. He keeps floating away in the air when he should start falling. I also got trouble figuring out how to move when he jumps and hits the bottom of a block and should start falling back. Any help to optimize this and general tips is very appriciated!



	public void move(long delta){
		
		if(jumping){
			dy = dy + gravity;
		}
		
		float newx = (dx * delta) / 1000;
		float newy = (dy * delta) / 1000;
		
		Rectangle me = new Rectangle(
				(int)(x + newx), 
				(int)(y + newy), 
				(int)(getWidth()), 
				(int)(getHeight())
		);
		
		Rectangle block = new Rectangle(
				(int)(25*16), 
				(int)(29*16), 
				(int)16, 
				(int)16
		);
		
		if(y + getHeight() > Game.SCREEN_HEIGHT){
			dy = 0;
			newy = 0;
			jumping = false;
			y = Game.SCREEN_HEIGHT - getHeight();
		}
		
		
		if(me.intersects(block)){
			if(newx > 0){
				x = (float)block.getX() - getWidth();
				newx = 0;
			}
			else if(newx < 0){
				x = (float)block.getX() + 16;
				newx = 0;
			}
			if(newy > 0){
				y = (float)block.getY() - getHeight();
				jumping = false;
				newy = 0;
				dy = 0;
				
			}
			else if(newy < 0){
				y = (float)block.getMaxY() + 16;
				newy = -(newy);
			}
		}
		
		x += newx;
		y += newy;
	}


It’s very difficult to understand what the code is supposed to do by just looking at the code. You could explain how it is supposed to work, and how exactly the current implementation differs from that. For example it is difficult to assess the function of the “jumping” boolean: is it supposed to be true whenever the entity is in the air or just in the moment that the entity is jumping (i.e. not while falling down again)?

As for optimization, you may want to avoid creating new Rectangles every time that method is executed. Store two rectangles and load data into them as needed.