- From: Grovkillen <notifications@github.com>
- Date: Tue, 24 Mar 2020 05:37:43 -0700
- To: w3c/gamepad <gamepad@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <w3c/gamepad/issues/128@github.com>
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. -- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/w3c/gamepad/issues/128
Received on Tuesday, 24 March 2020 12:37:56 UTC