GameMaker Finding the Real Distance a Joystick is Pushed

Mookal

Member
I'm making a game where the character's speed is analog, based on how much the player pushes the left stick on a connected controller. I'd think finding the distance between the joystick's position and its origin would be simple:
GML:
var xpos = gamepad_axis_value(0, gp_axislh);
var ypos = gamepad_axis_value(0, gp_axislv);
var dist = point_distance(0, 0, xpos, ypos);
return dist;
Unfortunately, this ends up causing a pretty major problem. While the area the joystick can move around in is a circle, that area is interpreted as a square.
So pushing up on the joystick would put its position at the top center of the square, but pushing both up and right on the joystick would put it in the top right of the square.
This means that, when the joystick is pushed towards a corner, the distance it's being pushed is overestimated.

I understand this concept is pretty abstract, so here's a diagram:

1606072472260.png

Essentially, I want the interpreted distance from the origin to be the same no matter what direction the stick is held in, while also keeping analog movement. How can I achieve this?
 

Slyddar

Member
Try something like this :
Code:
if xpos != 0 or ypos != 0 {
    var _dir = point_direction(0, 0, xpos, ypos);
    var _f = 2; //speed factor
    hsp = lengthdir_x(abs(xpos), _dir)*_f;
    vsp = lengthdir_y(abs(ypos), _dir)*_f;
}
Then you can get dist with point_distance(0, 0, hsp, vsp)/_f if needed.
 
Last edited:
Top