Search code examples
libgdx

Collision of objects


I am just new in libgdx and I've been looking for a better solution to my problem. The stick man must catch the ball when he's going through it but I really don't know how to detect each time the stick man passes through the ball.

P.S. the ball can be dragged and drop by the user in order to avoid the stick man.


Solution

  • Without seeing any code and with the very vague description you are giving, I will attempt and answer.

    On you ball and on you stickman you make a Rectangle with the size of the ball and stickman and their coordinates.

    Rectangle bound = new Rectangle(x,y,width,height);
    

    When moving the objects around you move the rectangles too.

    Then you check if they overlap:

    if(stickman.bound.overlaps(ball.bound)){
        //collision
    }