# Math behind moving a gamepad hat button

This topic is 4160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am wondering if taking the crossProduct(UP, RIGHT) * .5 will get me the up and right keypad movement I need for a gamepad? I need up/right up/left down/right down/left so is my math correct or do I need to do it another way? thanks

##### Share on other sites
prolly not
you prolly just want

up * sin(45) + right * cos(45)
-up * sin(45) + right * cos(45)
up * sin(45) + -right * cos(45)
-up * sin(45) + -right * cos(45)

The crosproduct will give you a vector "-at" in a standard (at, up, left) world. wich you could then rotate around using
the standard axis-angle rotation functions of your graphics api. (in 90deg incriments starting at 45deg)

##### Share on other sites
The cross product will give you a vector that points straight up out of the dpad (as in, towards your face). You'll want to do what KulSeran told you, and make the up-right, up-left, etc movements just linear combinations of the up and right directions.

##### Share on other sites
RIGHT_UP = up * sin(45) + right * cos(45)
RIGHT_DOWN = -up * sin(45) + right * cos(45)
LEFT_UP = up * sin(45) + -right * cos(45)
LEFT_DOWN = -up * sin(45) + -right * cos(45)

Are those correct? guess I need a bit more clarification on it.

Thanks for the help.

##### Share on other sites
That looks perfectly correct. It's helpful to just think of the directions as vectors on a unit circle. You can think of any direction as:

UP * sin(theta) + RIGHT * cos(theta);

For example, just straight up would be the vector pointing directly up at an angle of 90 degrees, therefor the direction vector would be:

UP * sin(90) + RIGHT * cos(90) = UP * 1 + RIGHT * 0 = UP

As you can see, it just simplifies to UP, which you would expect.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
18

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009802
• ### Who's Online (See full list)

There are no registered users currently online

×