android: fix inverted device-orientation mapping (real accelerometer reads world-up; emulator uses gravity convention)
This commit is contained in:
@@ -13,8 +13,15 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
* Physical orientation of the phone relative to its portrait grip, from the
|
||||
* accelerometer. [deg] is the CLOCKWISE rotation of the device as seen by
|
||||
* the user: 0 = upright portrait, 90 = turned clockwise (portrait top edge
|
||||
* points to the user's right, gravity along +X device), 180 = upside down,
|
||||
* 270 = turned counter-clockwise.
|
||||
* points to the user's right, sensor reads -g on X), 180 = upside down
|
||||
* (sensor reads -g on Y), 270 = turned counter-clockwise (sensor reads +g
|
||||
* on X).
|
||||
*
|
||||
* Android accelerometer convention (REAL devices): at rest the reading is
|
||||
* "acceleration minus gravity", i.e. it points to world UP in device coords
|
||||
* — flat on a table screen-up -> z=+9.81, upright portrait -> y=+9.81.
|
||||
* (Note the emulator's virtual sensor uses the opposite, gravity-vector
|
||||
* convention; do not "fix" this mapping to match emulator defaults.)
|
||||
*
|
||||
* The activity is portrait-locked (composition glued to the phone frame, so
|
||||
* the thermal image region always matches the lens direction). UI layers use
|
||||
@@ -45,11 +52,12 @@ object DeviceOrientation : SensorEventListener {
|
||||
override fun onSensorChanged(event: SensorEvent) {
|
||||
val gx = event.values[0]
|
||||
val gy = event.values[1]
|
||||
// world-up in device coords: (0,+g)=0 (-g,0)=90 (0,-g)=180 (+g,0)=270.
|
||||
// hysteresis: only switch pose when the dominant axis clearly wins,
|
||||
// so ~45 deg in-between holds keep the previous reading
|
||||
val next = when {
|
||||
abs(gx) > abs(gy) + 2.5f -> if (gx > 0) 90 else 270
|
||||
abs(gy) > abs(gx) + 2.5f -> if (gy < 0) 0 else 180
|
||||
abs(gx) > abs(gy) + 2.5f -> if (gx < 0) 90 else 270
|
||||
abs(gy) > abs(gx) + 2.5f -> if (gy > 0) 0 else 180
|
||||
else -> _deg.value
|
||||
}
|
||||
if (next != _deg.value) _deg.value = next
|
||||
|
||||
Reference in New Issue
Block a user