Hi everyone,
I am trying to control my Unity camera with the data received from an android mobile device sensor sent through websockets.
I am using SensorServer to receive data from my android device. I can receive separate magnetometer, accelerometer and gyroscope raw data, but I was trying to adapt the code from Device Orientation Controls from a deprecated example of Three.js (here) which uses the orientation sensor.
On my script I receive three values: x, y and z which are actually alpha, beta and gamma (in degrees) as the following:
· The DeviceOrientationEvent.alpha value
represents the motion of the device
around the z axis, represented in
degrees with values ranging from 0
(inclusive) to 360 (exclusive).
· The DeviceOrientationEvent.beta value
represents the motion of the device
around the x axis, represented in
degrees with values ranging from -180
(inclusive) to 180 (exclusive). This
represents a front to back motion of
the device.
· The DeviceOrientationEvent.gamma value
represents the motion of the device
around the y axis, represented in
degrees with values ranging from -90
(inclusive) to 90 (exclusive). This
represents a left to right motion of
the device.
I have been trying to adapt the code from three.js to c# but I get lost in the quaternion math, specially because of the abstraction of the euler class which does not exist in Unity and the axis change of the euler multiplication:
// SET UP
const _euler = new Euler();
const _q0 = new Quaternion();
const _q1 = new Quaternion(-Math.sqrt(0.5), 0, 0, Math.sqrt(0.5));
const _zee = new Vector3(0, 0, 1);
const orient = window.orientation // its only possible values are -90, 0, 90, and 180, then they are converted to radians.
// UPDATE FUNCTION
_euler.set(beta, alpha, -gamma, "YXZ"); // 'ZXY' for the device, but 'YXZ' for us. All values are converted to radians
// Quaternion is the camera transform.rotation
quaternion.setFromEuler(_euler); // orient the device
quaternion.multiply(_q1); // camera looks out the back of the device, not the top
quaternion.multiply(_q0.setFromAxisAngle(_zee, -orient));
Basically what I would like to do is take alpha, beta and gamma and use these values to change my camera rotation as if I was looking through the phone with it, like a vr viewer.
Hope someone can help me with this since I have been trying without success.
Thank you so much in advance