# Designing a four-directional movement control

I'm developing an RPG for iOS. Currently, I plan my controls to look like this:

So the user can interact with objects, people, etc. by tapping on the right side of the screen, and moves by swiping in the direction they want to go in on the left side of the screen. Right now, I'm having a problem with the movement controls. What I want is for there not to be a fixed d-pad; I want the user to be able to tap anywhere on the left side of the screen and swipe in the direction they want (within a range, demonstrated in the below image), and it seems easy enough to implement this.

However, I'm having a bit of a math problem. Since I'm developing for iOS, how I planned to implement this was to subclass UIView and have two properties, startTouch and endTouch that represented where the user began his/her swipe and where the user lifted his/her finger. Then, I could do a bit of math and discern which direction the swipe was in. But I'm having a bit of trouble doing the math, and that's what my question's about: given two points, how can I figure out if the swipe was going up, down, left, or right? Keep in mind that the user could tap anywhere on the screen and that tap is the center of the imaginary d-pad, like so:

• My eyes! Please don't use those colors in game. You can get the position of the startTouch and endTouch right? Why don't you just subtract the startTouch from the endTouch and you'd have your relative direction? – MichaelHouse Aug 9 '13 at 15:00
• @Byte56 - These colors were just for demonstrating the controls, I agree they're not fit for a game :p. As for your suggestion, I actually tried subtracting the start and end points, but when I tested it it was unreliable when I swiped at a diagonal (sometimes the diagonal would register as a vertical swipe, sometimes as a horizontal), so I was looking for a more precise way to do it. – pasawaya Aug 9 '13 at 15:07
• Are you wanting dead zones like those in your images? You could get the angle between your swipe vector and a right vector. Then make decisions based on that angle (like if it's between pi-(pi/8) and pi+(pi/8) it would be a left swipe). – MichaelHouse Aug 9 '13 at 15:14

Take the resulting vector Dir from subtracting the start point from the end point. Now use atan2(Dir.y, Dir.x) to get the angle of the vector (angle between positive X axis). Use that angle to decide the direction of the swipe. Include your dead zones as desired.

You can easily utilize something like this answer. Where your direction could be:

enum direction {
Left,