Skip to main content
3 of 3
removed blacklisted tag, fixed code formatting
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

Draw Rectangle To All Dimensions of Image

I have some rudimentary collision code:

public class Collision {
    static boolean isColliding = false;
    static Rectangle player;
    static Rectangle female;

    public static void collision(){
        Rectangle player = Game.Playerbounds();
        Rectangle female = Game.Femalebounds();
    
        if(player.intersects(female)){
            isColliding = true;
        }else{
            isColliding = false;
        }
    }
}

And this is the rectangle code:

public static Rectangle Playerbounds() {
    return(new Rectangle(posX, posY, 25, 25));
}

public static Rectangle Femalebounds() {
    return(new Rectangle(femaleX, femaleY, 25, 25));
}

My InputHandling class:

public static void movePlayer(GameContainer gc, int delta){
    Input input = gc.getInput();
    
    if(input.isKeyDown(input.KEY_W)){
        Game.posY -= walkSpeed * delta;
        walkUp = true;
        
        if(Collision.isColliding == true){
            Game.posY += walkSpeed * delta;
        }
    }
    
    if(input.isKeyDown(input.KEY_S)){
        Game.posY += walkSpeed * delta;
        walkDown = true;
        
        if(Collision.isColliding == true){
            Game.posY -= walkSpeed * delta;
        }
    }
    
    if(input.isKeyDown(input.KEY_D)){
        Game.posX += walkSpeed * delta;
        walkRight = true;
        
        if(Collision.isColliding == true){
            Game.posX -= walkSpeed * delta;
        }
    }
    
    if(input.isKeyDown(input.KEY_A)){
        Game.posX -= walkSpeed * delta;
        walkLeft = true;
        
        if(Collision.isColliding == true){
            Game.posX += walkSpeed * delta;
        }
    }
}

The code works partially. Only the right and top side of the images collide. How do I correct the rectangle so it will draw on all sides?

user20439