Using AABB in a platformer not working well

I’m trying to use AABB collision in a platformer, but It’s not working correctly no matter how I change it. Here is the core setup for it. The player can jump and collide with the floor (for some reason i call it enemy) but if it touches the sides, it comes back up. Any tips or solutions? I’ve been going at this for far too long.

public class AABBDemo {
 
        public AABBDemo() {
                try {
                        Display.setDisplayMode(new DisplayMode(640, 480));
                        Display.setTitle("test");
                        Display.create();
                } catch (LWJGLException e) {
                        e.printStackTrace();
                }
 
                glMatrixMode(GL_PROJECTION);
                glLoadIdentity();
                glOrtho(0, 640, 480, 0, -1, 1);
                glMatrixMode(GL_MODELVIEW);
 
                Rectangle A = new Rectangle(100, 100, 50, 50);//player
                Rectangle B = new Rectangle(100, 300, 90, 20);//enemy
                
                int leftA, leftB;
                int rightA, rightB;
                int topA, topB;
                int bottomA, bottomB;
                
                
                int xVel = 0;
                int yVel = 1;
                
                int size = 50;
                
                boolean jumping = false;                
                float jump = 0;
                boolean jumped = false;
                
                while (!Display.isCloseRequested()) {
                        glClear(GL_COLOR_BUFFER_BIT);
                        
                       
                        
                        A.setX(A.getX() + xVel);
                        A.setY(A.getY() + yVel);
                        
                        
                        
                        if(jumping)
                        {
                        	A.setY((int) (A.getY() - jump));
                        	
                        	jump -= 0.3f;
                        	
                        	if(jump < -5)
                        		jump = -5;
                        	
                        }
                        
                        if(!jumping)
                        	jump = 10;
                        
                        if(A.getX() < 0 || A.getX() + size > 640 || check_collision(A, B))
                        {
                        	jumping = false;
                        	A.setX(A.getX() - xVel);
                        	A.setY(A.getY() -  2);
                        	
                        	
                        	
                        }
                        
                        
                        
                        if(Keyboard.isKeyDown(Keyboard.KEY_D))
                        	xVel = 2;
                        else if(Keyboard.isKeyDown(Keyboard.KEY_A))
                        	xVel = -2;
                        else 
                        	xVel = 0;
                        
                        if(Keyboard.isKeyDown(Keyboard.KEY_W) && !jumping && !jumped)
                        {
                        	jumped = true;
                        	jumping = true;
                        }
                        
                        if(!Keyboard.isKeyDown(Keyboard.KEY_W))
                        	jumped = false;
                        
                        
                        
                        
                        
                        glBegin(GL_QUADS);
                        glColor3f(1, 0, 0);
                        glVertex2i(A.getX(), A.getY()); // Upper-left
                        glVertex2i(A.getX() + A.getWidth(), A.getY()); // Upper-right
                        glVertex2i(A.getX() + A.getWidth(), A.getY() +A.getHeight()); // Bottom-right
                        glVertex2i(A.getX(), A.getY() + A.getHeight()); // Bottom-left
                        glEnd();
                        
                        
                        glBegin(GL_QUADS);
                        glColor3f(0, 1, 0);
                        glVertex2i(B.getX(), B.getY());
                        glVertex2i(B.getX() + B.getWidth(), B.getY());
                        glVertex2i(B.getX() + B.getWidth(), B.getY() + B.getHeight());
                        glVertex2i(B.getX(), B.getY() + B.getHeight());
                        glEnd();
 
                        Display.update();
                        Display.sync(60);
                }
 
                Display.destroy();
                System.exit(0);
        }
        
        
        public boolean check_collision( Rectangle A, Rectangle B )
        {
            //The sides of the rectangles
            int leftA, leftB;
            int rightA, rightB;
            int topA, topB;
            int bottomA, bottomB;

            //Calculate the sides of rect A
            leftA = A.getX();
            rightA = A.getX() + A.getWidth();
            topA = A.getY();
            bottomA = A.getY() + A.getHeight();
                
            //Calculate the sides of rect B
            leftB = B.getX();
            rightB = B.getX() + B.getWidth();
            topB = B.getY();
            bottomB = B.getY() + B.getHeight();
            
            //If any of the sides from A are outside of B
            if( bottomA <= topB )
            {
                return false;
            }
            
            if( topA >= bottomB )
            {
                return false;
            }
            
            if( rightA <= leftB )
            {
                return false;
            }
            
            if( leftA >= rightB )
            {
                return false;
            }
            
            //If none of the sides from A are outside B
            return true;
        }
            
            
        public static void main(String[] args) {
                new AABBDemo();
        }
 
}

No guarantees of correctness (I’ve been away from this stuff for a while), but here’s what I noticed.

A.setY(A.getY() -  2);

Here you’re moving the player up a little after a collision has been detected, presumably to move the player ‘out of’ the platform. The problem is that the collision detection function doesn’t differentiate between colliding with the platform from the top and colliding with it from the side. Because of this, whenever the player collides from the side, the player is moved upwards, leading to the undesirable behavior you’re seeing.

Assuming you can guarantee the player won’t move far enough in a single update to tunnel through the platform, you can solve this problem using a slightly more sophisticated version of the separating axis test. As a bonus, this will also give you more robust handling of interactions between the player and the platform.

Specifically, what you’re looking for is the ‘minimum translation vector’. A Google search for ‘sat minimum translation vector’ turns up a lot of promising-looking hits, so that’d probably be a reasonable place to start.

Because of jumping and movement speed, going by 2 wouldn’t work well. I tried to create an AABB class but that doesn’t work correctly though.
http://www.java-gaming.org/?action=pastebin&id=1260

ANy examples I’d find online would use either the same collision method I did, or not cover using it with platformers properly.