I am having trouble understanding fully the information I am getting back from a mobile device when a 'devicemotion' event is triggered in javascript. What I assumed I would be getting back does not match with what I am seeing, and even what I am seeing seems to be inconsistent.
So I understand that the devicemotion event has an X, Y and Z component, which registers movement along the phone's axises. My assumption had been that these numbers would be registers of the device's movement in one direction, with larger movements giving larger numbers back. So for example if the camera was moved left, I would get a negative X value, and right would give me a positive one.
It quickly became apparent that this was not true. I am now assuming (from the numbers I have console logged) that this event only measures changes in speed along an axis. So agnostic of moving left or right, I just know that the device was moved along the x axis, and since the last even trigger, the speed has changed by the returned amount. So the positives and negatives I was seeing were in fact related to a positive or negative change in speed.
Is this a correct understanding? And if so, does this mean that there is no way to gather direction information, past just knowing that the change happened on one axis or another?
Thanks.
The DeviceMotionEvent
returns (among other things) acceleration in m/s^2. It will return a positive value if you speed up your movement in one direction. If you are moving at a constant speed in that same direction, the acceleration will return 0 (note that due to the sensitivity of the accelerometer this is practically impossible, you will always see values around 0, never exactly 0). If you slow down in that same direction, but are still moving, you will get a negative value back from acceleration. To make things even more complicated, if you move in the opposite direction, you will get negative values back if you accelerate and positive values if you decelerate. This allows you to distinguish between moving the device up (= positive acceleration, along the Y-axis) or down (= negative acceleration, along the Y-axis) and right (= positive acceleration, along the X-axis) or left (= negative acceleration, along the X-axis).
If you want to know which direction the device is moving in, you will have to capture all acceleration measurements over a period of time and calculate the direction from that, e.g. if the Y acceleration starts out positive, the device must be moving upwards until you detect a negative acceleration along the Y that completely cancels out the initial acceleration.