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

public class Collision {

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;
        }
    }
}

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? Thanks for any suggestions!

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;
        }
    }
}

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? Thanks for any suggestions!

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;
        }
    }
}

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?

added 889 characters in body
Source Link
user20439
user20439

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? Thanks for any suggestions!

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? Thanks for any suggestions!

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? Thanks for any suggestions!

Source Link
user20439
user20439

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));
}

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? Thanks for any suggestions!