Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direction and thrust of the joystick #128

Open
Grovkillen opened this issue Mar 24, 2020 · 0 comments
Open

Direction and thrust of the joystick #128

Grovkillen opened this issue Mar 24, 2020 · 0 comments

Comments

@Grovkillen
Copy link

Grovkillen commented Mar 24, 2020

I'm currently implementing gamepad support for the GUI of ESP Easy (OS for IoT). I know that there's a discussion regarding adding events for button presses etc. so I will not discuss that here. This is what I've done to get the direction of the joystick + its thrust (0...1).

"joystick": {
	"left": {
		"x": Math.floor(gp.axes[0] * 100)/100,
		"y": Math.floor(gp.axes[1] * 100)/100,
		"direction": direction(gp.axes[0], gp.axes[1], "up"),
		"thrust": thrust(gp.axes[0], gp.axes[1])
	},
	"right": {
		"x": Math.floor(gp.axes[2] * 100)/100,
		"y": Math.floor(gp.axes[3] * 100)/100,
		"direction": direction(gp.axes[2], gp.axes[3], "up"),
		"thrust": thrust(gp.axes[2], gp.axes[3])
	}
}
thrust = function (x, y) {
    x = Math.floor(x * 100)/100;
    y = Math.floor(y * 100)/100;
    if (x === 0 && y === 0) {
        return NaN;
    }
    if (x === 0) {
        return Math.abs(y);
    }
    if (y === 0) {
        return Math.abs(x);
    }
    let xy = Math.sqrt(Math.pow(x,2) + Math.pow(y, 2));
    xy = Math.floor(xy * 100)/100;
    return (xy > 1 ? 1 : xy);
};

direction = function (x, y, zeroPoint) {
    x = Math.floor(x * 100)/100;
    y = Math.floor(y * 100)/100;
    if (x === 0 && y === 0) {
        return NaN;
    }
    let degree = (Math.atan2(y, x) > 0 ? Math.atan2(y, x) : (2*Math.PI + Math.atan2(y, x))) * 360 / (2*Math.PI);
    if (zeroPoint === "up") {
        degree = degree - 270;
        return (degree < 0 ? degree + 360 : degree);
    }
    if (zeroPoint === "down") {
        degree = degree - 90;
        return (degree < 0 ? degree + 360 : degree);
    }
    if (zeroPoint === "left") {
        degree = degree - 180;
        return (degree < 0 ? degree + 360 : degree);
    }
    // right just return the degrees...
    return degree;
};

It would be nice to get this info from the API. Perhaps also the 3D vector of the joystick position. Advanced data like acceleration and spin might also be of interest?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant