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



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?


1 Answer 1


Consider using a swept collision test instead of just an intersect/overlap test.

enter image description here

When doing a swept test, the area traveled is also considered for the collision test, not just the final position/destination for the object in that frame.

Essentially it's about (for the AABB case at least) extending the corners of the AABB in the direction of travel by the velocity, and calculating if and when the intersection with the obstacle occurs. The article linked covers this in detail for AABBs along a linear path, the solution for more complex shapes with non-linear movement is a bit trickier.

  • \$\begingroup\$ Hi bornander. Thank you so much for providing this information. This helped me a lot and is what I was looking for. I still have problems computing the velocity in my code: There is a gameloop running in my main thread: each loop I use the aabb-algorithm for the player-object and all the barriers to detect the collision. But the user input happens in onTouchEvent, is it where I get the velocity from? That's what my input method looks like. Action_down which checks if the user touched the player-image ( then sets to touched = true; ). \$\endgroup\$
    – eiskaktus
    Nov 4, 2016 at 14:59
  • \$\begingroup\$ And then Action_move (when the user moves the image): checks if touched==true and sets the new coordinates of the player-image. I tried to add a public VelocityTracker velocity = VelocityTracker.obtain(); to the input method. And I added velocity.addMovement(event); to my Action_move method. In my gameloop before the aabb-algorithm I added: v.velocity.computeCurrentVelocity(1000); float vx = v.velocity.getXVelocity(); float vy = v.velocity.getYVelocity(); It doesn't work but it is the right way to compute the velocity? Thanks so much \$\endgroup\$
    – eiskaktus
    Nov 4, 2016 at 15:00
  • \$\begingroup\$ @eiskaktus As you're doing drag and drop the velocity is whatever distance you dragged the player. Position before drag and position after, the delta between them is effectively your velocity. \$\endgroup\$
    – bornander
    Nov 5, 2016 at 9:09
  • \$\begingroup\$ Thank you so much for your answer and telling me about AABB-algorithm. I decided to create another new thread about the velocity-computation, since I still can't get it to work and I want to post some of my code. \$\endgroup\$
    – eiskaktus
    Nov 6, 2016 at 20:52
  • \$\begingroup\$ That's the link to my code snippets. Thx gamedev.stackexchange.com/questions/132689/… \$\endgroup\$
    – eiskaktus
    Nov 6, 2016 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .