# Thumb Stick Challenge

Here’s a challenge inspired by page 403 of Game Engine Architecture.

See if you can write some code in Unity or Unreal that detects reliably when the player has made a full circle of one of the joysticks.

Share your code and maybe a short animated gif here.

oo, a challenge…

Thats me home from a couple of weeks away at work, so ill give this a bash tonight to get the ball rolling

1 Like

Mondays Edit
Still have a way to go, got to do the pattern matching etc. only got as far as getting the quadrant its in, and adding a deadzone to stick.

TUESDAY EDIT: updated code below as final.

```using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RotationTestScript : MonoBehaviour {

private const float RadianToDegreesScalar = 57.2958f;	// used to convert radians to degrees
private int currentRotationAngle = 0;
private const float DeadZone = 0.2f;									// just a deadzone for checks

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");

// so if the stick is not at rest
{
// get the angle that the stick is pointed in
currentRotationAngle = GetAngleInDegrees(inputX, inputY);

// now find out if the current quadrant position is still in the same state while moving
{
//Debug.Log("Quadrant change : " + currentQuadrant + "  |     Inputs  X:" + inputX + ", Y:" + inputY) ;
}

}
else
{
{
// run the else if the stick has returned to centre and not used
//Debug.Log("returned to centre");
}
}

}

//------------------------------------------------------------------------------------------------
//				argument : input X axis, and input Y axis values
//				output : provides a float in degrees from 0 pointing right CCW to 359
//------------------------------------------------------------------------------------------------
int GetAngleInDegrees(float _inputX, float _inputY)
{
// since 0 degrees is right, and the lower quadrants are 0 to -180
// and upper quadrants are 0 to +180, need to do a little conversion to get 0 to 360.
// starting from right side going counter clockwise

if(angleInDeg < 0)
angleInDeg += 360;

return Mathf.RoundToInt( angleInDeg);
}

//------------------------------------------------------------------------------------------------
//				argument : integer angle of stick direction
//				output : provides a quadrant enum
// 				ie 1 to 90 deg is quadrantEnum.UL
//    			91 to 180  is quadrantEnum.UR  etc  for one of the 4 quadrants
//------------------------------------------------------------------------------------------------
{
int quadrantInt =  angle/90 + 1;  // add 1 as integer division always rounds down

case	1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
break;
}

}

{

// for a whole revolution, I just need to check that all 4 quadrants are in the Queue as that the max size
// so to be what were after it must have one of each quadrant! (i hope)
{
// ok so have a complete revolution, so at this point need to clear the Queue, as any movement to another quadrant
// gets a false positive of a complete revolution
Debug.Log("REVOLITION!!");
}
}
}
```
2 Likes

Congratulations raising to this challenge, well done!

1 Like

enjoy it, keeps the grey matter going, and forces to read up on solution mechanics that are outwith comfort zone.

im just working on logging quadrant changes to a Queue (max 4 elements).
then when quadrant changes add to end of Q and pop the first one off.
if returns to centre, clear the Queue.

then comes the pattern matching, I might just end up comparing Q to the 4 possible positions in both CW and CCW directions. thats tonights job, see if i can find a tighter way of doing it. hehe, kettle and coffee first me thinks.

Code in original post edited to show final.
the comparison wasn’t as bad as i thought in the end.

so for checking for a revolution, my Queue which has a max size of 4 elements, if each of the 4 quadrants is in the Queue then it must have done a revolution, as when stick goes to centre it adds an NA empty placeholder to Queue. ie ( LL, NA, LR, UR } would be a no go, but { LL, LR, UR, UL} would be ok.

and once revolution is attained, clear the Queue, if not, another quadrant move, would give a false positive of a revolution

phew…

so couple of days to get my Unity game done for my 1GameAMonth challenge, my weekly blender submission… guess i’d best get kettle on and get Cracking! not enough time in the week to get coursework done for Blender and Unity with all the challenges lol

Cheers for the challenge @ben enjoyed it, practicality over elegance in my case

1 Like

A little late and off topic… but…

I ended up buying the 2nd edition of the book, its a fair old size of a tome. I’m only about a quarter way through, but man is there some serious ammount of amazing knowlege in there from behind the scenes of naughty dog and the industry in general.

Only got to hear of it on the back of this post

1 Like