Sign in to follow this  
MARS_999

Math behind moving a gamepad hat button

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this