Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
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? – Byte56 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). – Byte56 Aug 9 '13 at 15:14
up vote 2 down vote accepted

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 {
share|improve this answer
Thanks so much! This is exactly what I needed. – pasawaya Aug 9 '13 at 15:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.