Friday, October 22, 2010

Input System: Part 5 Thumbsticks

This tutorial will go over creating two basic functions to use in our input class that will use the thumbsticks. These two functions will be identical in concept to the triggers but will involve a little more work to get the same result. Thumbsticks are very important to most games so having an easy function will probably save a lot of trouble. Before I start I would like to first say that the thumbsticks use a normal Cartesian coordinate system where the center is the origin, that is you haven't moved the thumbstick at all. To the right is the positive X, left is negative X, up is positive Y and down is negative Y. This is slightly different from the screen coordinate system where down is positive Y and up is negative Y so that is just a heads up. 

So we want to create two functions one that will return the value of a thumbstick ( left or right ) and another that will return a bool for if we passed the threshold in any direction ( left, right, up or down ). So were going to start by creating two enums one for the thumbstick and another for each direction. The thumbsticks enum is the same as the trigger enum so we could combine to one enum, but I doubt having two enums with the same values will make any performance issue and the clarity we get is really nice.


So we have our enums for the different cases we are going to find later. Next were going to create the basic prototype of our functions. Our first function is to get our thumbstick which is in the type of vector2 and we are going to need to know which player it is for and which thumbstick so those are the two parameters we need. The other one will return a bool and we also need to know which direction to check for. Click the images to enlarge if you can not read them.


Because the thumbstick one is easier were going to do that one first.  We simply check which thumbstick they want and return the appropriate thumbstick from our current game pad state and player index.


Now this part is a little more complicated because there are more possible cases. Now instead of doing something like nested switch statements or nested if statements we can make this much simpler. Now were going to have to compare our current and previous game pad states so lets first create those. We can call our previously created function in this one for our current state but have to create our own for the previous state but that isn't too bad. Now we have a problem of the threshold because the range of values of the thumbsticks are from -1 to 1 meaning that if we wanted to go left or up our threshold would be negative. So to get around this we simply reverse the vector2 values of our previous and current states if were checking a direction that is negative which are left and down. Next we have our checks to return the value. If were going left or right we check the X value otherwise we check the Y value in our vector2's.


Now that those are out of the way our input class is fully functional but there are some other functions I would like to add to make this as simple and reusable as possible. To use the thumbstick to move something around on screen you would do something like the following. This will move around based on the game time so that it will never changed based on your frames per second. Also see how we have to reverse the Y coordinate of the thumbstick because the screen coordinate system and Cartesian coordinate system have there Y values reversed.  Note the error is because playerPosition is undefined.


Next time I will be making it so we can get the controlling player so that you can use only the controlling player to get controls and not just guess by using Player Index One which is not the way to do it. 

4 comments:

  1. Will we get an update soon? :/

    ReplyDelete
  2. Sorry about that I have been working an a new project, maybe my other developers will let me post about it. As cliche as it sounds it is an MMO that I am working on with a few friends of mine. Maybe I can finish this input tutorial this weekend the way I wanted to do the rest of this tutorial is not really what I feel is the most beneficial for an input helper so I am probably going to change it slightly.

    ReplyDelete
  3. Still waiting.. :(

    ReplyDelete
  4. I'm the same guy who posted the 1st comment on this post, I was hoping maybe you managed to complete your tutorial by now. Not that I still need it, but it would have been nice to see some new content on this blog. :/

    ReplyDelete