Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Detect Swipes on Your Own

· Java Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue.

Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.