libgdx animation inside of a rectangle - collison detection - rectangles

前端 未结 1 1631
借酒劲吻你
借酒劲吻你 2021-01-26 05:50

I am writing a RPG game similar to the style of Pokemon (top down view). I am now working on the issue of collision detection. I am wanting to create the collision detection b

1条回答
  •  囚心锁ツ
    2021-01-26 06:20

    If you want to detect collision between two rectangles, there is an example I used with sprites.

    public class TestSpriteOne extends Sprite{  
        private Rectangle rectangle;
    

    Add in the constructor.

    rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
    

    In update Method

    rectangle.setPosition(getX(), getY());
    

    Is a new nethod

    public Rectangle getColliderActor(){
        return this.rectangle;
    }
    

    Other classes

    public class TestSpriteTwo extends Sprite{
        private Rectangle rectangle;
    

    In the constructor.

    rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
    

    In update method

    rectangle.setPosition(getX(), getY());
    

    In new method

    public Rectangle getColliderActor(){
       return this.rectangle;
    }
    

    // Call in the TestSpriteOne class

    boolean hasCollided = TestSpriteTwo.getColliderActor().overlaps(getColliderActor());
    

    //Call in the GameScreen class

    boolean hasCollided = TestSpriteOne.getColliderActor().overlaps(TestSpriteTwo.getColliderActor());
    

    It overlaps whether this rectangle overlaps the other rectangle, so hasCollided variable will become true.

    Edit: if the animation changes its width or height, you could resize the rectangle in the update method

    rectangle.setSize (width, height);
    

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