# Moving a 2D character on a straight line

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.

-
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? – Qqwy Jan 15 '13 at 12:20
Thank you Qqwy, I edit as a complement ! – user1876708 Jan 15 '13 at 12:24
You said you were going to describe your problem, but you only described what you want to do. What's the actual problem? – Byte56 Jan 15 '13 at 14:44
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. – Sidar Jan 15 '13 at 15:28
@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. – user1876708 Jan 15 '13 at 15:37

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

-
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 ? – user1876708 Jan 15 '13 at 19:01
@user1876708 I think you're trying to cover too many aspects in one go. Perhaps you should simplify your question. One at a time. – Sidar Jan 15 '13 at 19:35
I'll open a new question, thank you very much. – user1876708 Jan 15 '13 at 20:05