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

Layout of Movement Controls

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.

enter image description here

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:

enter image description here

  • 2
    \$\begingroup\$ 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? \$\endgroup\$
    – House
    Aug 9, 2013 at 15:00
  • \$\begingroup\$ @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. \$\endgroup\$
    – pasawaya
    Aug 9, 2013 at 15:07
  • 1
    \$\begingroup\$ 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). \$\endgroup\$
    – House
    Aug 9, 2013 at 15:14

1 Answer 1


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 {

You must log in to answer this question.

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