My player is moved around the screen by touch user input. The user touches the character and drags him to another position. (implemented in touchevent
: actionmove
and actiondown
). I want the touch-movement to stop, as soon as the character reaches an object. So I don't want the character to pass through the object.
I used rect.intersect(player,object)
to detect a collision between the player and the non-moving object. As soon as a collision is detected, I have to move the character back from the object, so that both don't overlap.
I used this minkowski-algorithm (Which Side Was Hit?) to find out on which side the collision occured. According to which side it is, I set back the player's position so it doesn't overlap with the non-moving object. Example for one side:
player.setPositionx(barrier.getCollision().left - player.getImage().getWidth());
or
player.setPositiony(barrier.getCollision().bottom);
This is not working smoothly. When I move the player by touch really fast over the object, it still passes through and appears on the other side of the non-moving object (probably because the code for the other side is then being executed). When I move the player along the left side, it suddenly appears on top of the non-moving object. When I move it on the bottom of the object, it may suddenly appear on the right or left side. (corners not smoothly) How can I make sure that I can move the player (by touch input) around the object smoothly?