summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRainer Keller <rainer.keller@digia.com>2014-07-29 08:54:39 +0200
committerRainer Keller <rainer.keller@digia.com>2014-07-29 14:53:26 +0300
commite7564e29be0e69ae4a52351fefd290939810dd2f (patch)
treebfd6cdbb2c15ee450f73a4c68aa1630719f87618
parent82e327e5bc8ff3073f8ca8558dce4066a10f6e4a (diff)
Handle accelerometer readings in rotated items
If an item is rotated the accelerometer readings have to be transformed to match the item rotation. Change-Id: I069dd2d97d1800385ffb08f4fc68c5522ef4b455 Reviewed-by: Laszlo Agocs <laszlo.agocs@digia.com> Reviewed-by: Oliver Wolff <oliver.wolff@digia.com>
-rw-r--r--basicsuite/sensors/Accelbubble.qml33
1 files changed, 31 insertions, 2 deletions
diff --git a/basicsuite/sensors/Accelbubble.qml b/basicsuite/sensors/Accelbubble.qml
index f70aa32..fa42770 100644
--- a/basicsuite/sensors/Accelbubble.qml
+++ b/basicsuite/sensors/Accelbubble.qml
@@ -49,9 +49,38 @@ Rectangle {
property real velocityX: 0
property real velocityY: 0
+ // Calculate effective rotation
+ function getRotation() {
+ var rot = rotation
+ var newParent = parent
+
+ while (newParent) {
+ rot += newParent.rotation
+ newParent = newParent.parent
+ }
+ return rot%360
+ }
+
function updatePosition() {
- velocityX += accel.reading.y
- velocityY += accel.reading.x
+ var actualRotation = getRotation();
+
+ // Transform accelerometer readings according
+ // to actual rotation of item
+ if (actualRotation == 0) {
+ velocityX -= accel.reading.x
+ velocityY += accel.reading.y
+ } else if (actualRotation == 90 || actualRotation == -270) {
+ velocityX += accel.reading.y
+ velocityY += accel.reading.x
+ } else if (actualRotation == 180 || actualRotation == -180) {
+ velocityX += accel.reading.x
+ velocityY -= accel.reading.y
+ } else if (actualRotation == 270 || actualRotation == -90) {
+ velocityX -= accel.reading.y
+ velocityY -= accel.reading.x
+ } else {
+ console.debug("The screen rotation of the device has to be a multiple of 90 degrees.")
+ }
velocityX *= 0.95
velocityY *= 0.95