Slick2D Rectangle Collision Detection

后端 未结 1 682
梦谈多话
梦谈多话 2021-01-23 21:28

I\'m having an issue showing that one rectangle has collided with another. So my question is, how can I get the intersect method to check for collision? Or are there any other w

1条回答
  •  失恋的感觉
    2021-01-23 21:54

    You are not updating the hitbox positions themselves.

    You are drawing this:

    g.drawRect(playX, playY, playW, playH); //draws player bounds
    g.drawRect(enemyX, enemyY, enemyW, enemyH); //draws enemy bounds
    

    But this isn't the actual hitbox, it's just the position of the player/enemy and the rectangles drawn here will be on the correct position while the hitboxes themselves aren't.

    I suggest you do the following:

    public void update(GameContainer container, int delta)
    {
        playerUpdate(delta);
    
        player.rect.setLocation(playX, playY);
        enemy.rect.setLocation(enemyX, enemyY); // update the hitboxes to the new positions
    
        if (player.rect.intersects(enemy.rect))
        {
            System.out.println("INTERSECT!");
        }
    }
    
    public void playerUpdate(int delta)
    {
        if (playerForward == true)
        {
           playX -= delta * 0.4f;
    
           if (playX <= 140) 
           {
                 playX = 140;
                 playerForward = false;
                 playerBackward = true;
           }
        }
    
        if (playerBackward == true) 
        {
             playX += delta * 0.4f;
    
             if (playX >= 700) 
             {
                 playX = 700;
                 playerBackward = false;
                 delay = 1250;
             }
        }
    }
    
    public void keyReleased(int key, char c) 
    {
       if (key == Input.KEY_ENTER)
       {
          playerForward = true;
       }
    }
    

    Furthermore, as you seem to be new to game development in Java, some tips for you:

    • Format your code properly

    Always place full {...} after if, else, switch, while, for, etc.; proper line indentation, .

    • Think OO (Object-Oriented)

    This one is pretty important. Your enemy and player class should both extend some kind of entity class because they both will pretty much want to obtain similar behavior (avoid code duplication!). Sum up similar behavior to a super class, simplify the behavior to be controlled with a few adjustable parameters and so on.

    For example, you store the positions of your enemy and player as a static integer in your main class. This is not OO. Move the positions to the entity class where you can implement it in whatever manner you wish.

    • Don't just throw an exception for no reason

    Your update(...) method throws a SlickException even though it's never needed.

    • Be careful about encapsulation

    This is something a lot of beginners do: Just grab some parameters, put them in a class as private (or maybe even public) and generate getters- and setters for them. This is not encapsulation. This is almost as bad as making them public in the first place.

    But why don't we just make everything public?

    We don't want anyone (or even ourselves) to rely on some parameters that just happen to be there because of some very specific implementation of something we might want to change later. Don't just put all possible values out there to be changed, the sense of encapsulation is to be independent from what kind of implementation we end up using and to protect the usability of our code by guarding what can be set/changed.

    • Performance does matter

    This is one of the aspects you should watch out for, for any kind of software, but you can often most drastically see the consequences in games. Performance is important! And by that, I don't mean that you have to watch out for every single detail, but just keep an overview in mind on how to improve and fasten up your code, especially with frequently called methods such as update(..) and render(..) in Slick2D.

    Update

    As a solution to another problem discussed in the comments:

    public class Enemy {
    
        private int startX, startY, width, height;
        public Shape rect = new Rectangle(startX, startY, width, height);
        // plus getters and setters
    }
    

    width and height can only be 0 as they are never assigned an integers have the value 0 per default, so the enemy rectangle hitbox does have 0 width and will never trigger.

    Try something like:

     public class Enemy {
    
        private int startX, startY, width = 50, height = 70;
        public Shape rect = new Rectangle(startX, startY, width, height);
        // plus getters and setters
     }
    

    This should work, but you should probably move all these attributes to the enemy class and put them in the constructor.

    0 讨论(0)
提交回复
热议问题