Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert coordinates on a square to coordinates on a circle?

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.

like image 309
Adam Siler Avatar asked Oct 25 '09 19:10

Adam Siler


1 Answers

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)
like image 132
Eemeli Kantola Avatar answered Sep 25 '22 18:09

Eemeli Kantola