summaryrefslogtreecommitdiffstats
path: root/basicsuite/Sensors/Accelbubble.qml
blob: c5b75ada9d88ce8e922f7457edb93d4fe004d181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import QtQuick 2.0
import QtSensors 5.0

Item {
    Rectangle {
        id: field
        color: "lightblue"
        border.width: 1
        border.color: "darkblue"
        width: parent.width
        height: parent.height
        Accelerometer {
            id: accel
            active:true
            onReadingChanged: {
                var newX = (bubble.x + calcRoll(accel.reading.x, accel.reading.y, accel.reading.z) * .1)
                var newY = (bubble.y - calcPitch(accel.reading.x, accel.reading.y, accel.reading.z) * .1)

                if (newX < 0)
                    newX = 0
                if (newY < 0)
                    newY = 0

                var right = field.width - bubble.width
                var bottom = field.height - bubble.height

                if (newX > right)
                    newX = right
                if (newY > bottom)
                    newY = bottom

                bubble.x = newX
                bubble.y = newY
            }
        }

        Image {
            id: bubble
            source: "bluebubble.png"
            property real centerX: parent.width / 2
            property real centerY: parent.height / 2;
            property real bubbleCenter: bubble.width / 2
            x: centerX - bubbleCenter
            y: centerY - bubbleCenter
            smooth: true

            Behavior on y {
                SmoothedAnimation {
                    easing.type: Easing.Linear
                    duration: 100
                }
            }
            Behavior on x {
                SmoothedAnimation {
                    easing.type: Easing.Linear
                    duration: 100
                }
            }
        }
    }

    function calcPitch(x,y,z) {
        return Math.atan(y / Math.sqrt(x*x + z*z)) * 57.2957795;
    }
    function calcRoll(x,y,z) {
         return Math.atan(x / Math.sqrt(y*y + z*z)) * 57.2957795;
    }
}