views:

219

answers:

2

I'm developing an indie video game, and have been operating under the assumption that because the thumbstick on my controller has a circular range of motion, it returns "circular" coordinates; that is, Cartesian coordinates constrained to a circular area (of radius 1). In fact, the coordinates are "square"; e.g., the top-right thumbstick position registers as x=1,y=1. When I convert the coordinates from Cartesian to polar, the magnitude can exceed 1 - which has the effect that the player can move faster diagonally than they can vertically or horizontally.

So, to clarify, I want to record the position of an analog thumbstick in terms of a direction and magnitude, where the magnitude is between 0 and 1. The thumbstick returns coordinates on a square plane, so simply converting the coordinates from Cartesian to polar is not sufficient. I think I need to convert the coordinate space, but that is pressing the limits of my monkey brain.

A: 

Divide each value by the magnitude to normalize all values to a unit vector, e.g.

magn = sqrt(x * x + y * y);
newx = magn > 1.0 ? x / magn : x;
newy = magn > 1.0 ? y / magn : y;

However, this may have the effect of clipping the magnitude instead of normalizing for the interior values.. That is, you'll get the same value for a controller pushed "fully" into the upper-left and a controller almost pushed fully into the same direction.

Jason
I definitely don't want to clip the magnitude. I already have a deadzone. The two together would significantly reduce the stick's effective range of motion. I think Eemeli has the right idea.
Metaphile
+5  A: 

See Mapping a Square to a Circle. There's also a nice visualization for the mapping. You get:

xCircle = xSquare * sqrt(1 - 0.5*ySquare^2)
yCircle = ySquare * sqrt(1 - 0.5*xSquare^2)
Eemeli Kantola
Based on the visualization, I think this is _exactly_ what I want. Thank you!
Metaphile