Posts Tagged ‘Game Development’

detect your swipe on our own

April 29, 2013 2 comments

Howdy everyone,

This has been a long long time since i wrote any post regarding anything.I have been super busy with my life and job.
But it was worth it in the end i think. Started a new job[not new now] at Progmaatic Developer Network which is a great place to work so far. Also consulting at ICDDR,B as an Android Developer in my weekend to finalize one of the project i did for them more then a year ago.

Got interested in Game Development and iOS Development.
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 solve pretty recently for my unit3d 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 again with some of the techniques about vector mathematics that i long forgot.

So to solve the problem i was having i applied the 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’s working. 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.

So that’s it for today , until next time


FYI: This post can also be found in Progmaatic’s Blog .