I am trying to create an Android Game and I am facing my first big problem.

After reading blogs, tutos ... I am able to easily move a character with a drag and drop, make it move move to a touched area, but I am unable to move it on a straight line with a finger gesture !

Here's a quick explanation of my problem

I want the character to move on a virtual grid, only up down left right, on a straight line and until it uncounter a wall. If there is nothing on his path, he is unstopped and "fall" from the screen !

He can only move from center of a square to the center on the square he is stopped on. This mean that when he hits a wall, he bounce and go back to the center of the previous square.

For complement :

  • It will be a sliding movement
  • The wall is just a term, in fact it will be objects ( let say fruits, animals ... ), with different size in a square with the same size that all the other squares.
  • I want to movement to be fluid over the tiles, not moving from tiles to tiles with a stop every time.

I am on that movement since weeks and can afford to find a solution, so your help is really welcome !

Thank you very much.

  • \$\begingroup\$ To clarify: You want to create movement of a player that keeps sliding until they hit something? Also, do the walls occupy a full square as well, or are they 'between' squares of the playing field? Do you want the player movement to be 'fluid' in one direction or tile-based? \$\endgroup\$
    – Qqwy
    Commented Jan 15, 2013 at 12:20
  • \$\begingroup\$ You said you were going to describe your problem, but you only described what you want to do. What's the actual problem? \$\endgroup\$
    – House
    Commented Jan 15, 2013 at 14:44
  • \$\begingroup\$ Why not just apply manhattan-distance and move along each axis? As for smooth transitions, you decide whether he stops or not. If you tell your character to go from A to B there is no stopping unless you specify it. \$\endgroup\$
    – Sidar
    Commented Jan 15, 2013 at 15:28
  • \$\begingroup\$ @Byte56 In fact, my question is also the problem, considering the fact that I cant do anything I want, moving on a straight line, stopping on the center of a square, move from A to B ...@Sidar I already think about that, fixing either X or Y and just change the other value, but I dont know how to do it ! If moving on X axis, then Y will be fix and X will change. \$\endgroup\$ Commented Jan 15, 2013 at 15:37
  • \$\begingroup\$ @user1876708 I have no idea what you mean by If moving on X axis, then Y will be fix and X will change. \$\endgroup\$
    – Sidar
    Commented Jan 15, 2013 at 19:34

3 Answers 3


Check to see if the touch was within the player's bounds for X (touch>playerX && touch < playerX+width)...If so, check whether the touch was on the right or left side (just check to see if the touch was less than or more than the player's position) and move the player toward the touch on the X axis. Do the same for the Y axis.

Do a simple collision check against wall objects to determine whether to stop the player or keep him moving.

if(!collision) { movePlayer(); }


It sounds to me like you have to take your input, in gesture form, and convert it into simple 4-directional input. You're basically converting very complex input into something much simpler.

You're going to want to take the starting position and the ending position of your gesture command and determine the angle of the vector between them. Recall your high school math - it's a simple formula. You're going to have to get comfortable using COS and SIN. With that angle, just determine which direction it most faces. You can break them up into 90 degree segments for each direction.


if you can move it by a given touch position on the screen, where's the problem? you already know how to move it

using gestures you just have to check the first and the last touchpoints, and find out if it was a vertical or a horizontal gesture according of what that was you just increment the x or y axis

  • \$\begingroup\$ But I dont know how to increment the axis and make the character stop when he hit a "wall". I think I have to increment with like an "infinite" value, because it is a slide movement and the character leave the screen ( die ) if there is nothing on his way. But how to detect collision and make it stop the character and make it going back to the previous square ? \$\endgroup\$ Commented Jan 15, 2013 at 19:01
  • \$\begingroup\$ @user1876708 I think you're trying to cover too many aspects in one go. Perhaps you should simplify your question. One at a time. \$\endgroup\$
    – Sidar
    Commented Jan 15, 2013 at 19:35

You must log in to answer this question.

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