Platinum Partner
java,mobile,unity3d

Detect Swipes on Your Own

I started a new job[not new now] at Progmaatic Developer Network which is a great place to work so far. I am also consulting at ICDDR,B as an Android Developer on weekends to finalize one of the project i did for them more then a year ago.

I got interested in Game Development and iOS Development. I am currently doing 1 iOS project and 1 unity3d based game.

So what will be today’s post is going to be about?

Today’s post is going to be about something that I solved pretty recently for my unity3d based game[still on going]. So, let’s get cracking :) .

The game I am currently working is a 2d game.

For the gameplay I needed to know which way user was swiping, based on that direction my player will move in that direction. Since i am no longer in Android or iOS where i can just use the gesture detector classes and get it done so i needed a different way. So how am i going to solve that?

Thanks to
Fletcher Dunn and Ian Parberry for their amazing book: “3D Math Primer For Graphics And Game Development”. Out of my own interest I just wanted to read this book to have some knowledge about 3d game development in general so i can have some knowledge about what’s going on underneath. There I got introduced to some vector mathematics techniques that i long forgot.

So to solve the problem I had to apply some vector mathematics :) .

To know the direction of swiping i need to know at least two position and i know can get them via touch/mouse api from unity3d.

  Vector2 m_v2DownPos = Input.touches [0].position;
  // api call to give first position 
  // user touched/mouse down and make it a Vector2
  Vector2 m_v2UpPos = Input.touches [Input.touches.Length - 1].position;
  // api call to give the last position 
  // user touched/mouse down and make it a Vector2;
  Vector2 v2delta = m_v2UpPos - m_v2DownPos; // difference between them
      if (v2delta.normalized.x >= 0.8f) {
        // user swiping in right direction
      } else if (v2delta.normalized.x <= -0.8f) {
        // user swiping in left direction
      } else if (v2delta.normalized.y <= -0.8) {
        // user swiping in down direction
      } else if (v2delta.normalized.y >= 0.8) {
        // user swiping in up direction
      }

I think you’ve got the idea of how it works. After getting v2delta if we normalize it, we will get the unit vector of v2delta and when you get that just check x and y component of the normalized vector to check swiping status of the user.I have defined the threshold of 0.8 . But it’s really up to you how you want to define the threshold. 1 means perfect swipe. But user hands might not be steady all the time so he/she might slip-away a little bit from perfect swipe.


Well this portion of code is applicable anywhere where we might need to calculate swipe event of our own.

 

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}