2021-10-07 16:56:11 +02:00
|
|
|
import QtQuick
|
|
|
|
|
import QtSensors
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
Window {
|
2021-10-07 16:56:11 +02:00
|
|
|
id: mainWindow
|
|
|
|
|
width: 320
|
|
|
|
|
height: 480
|
2015-11-09 17:09:42 +01:00
|
|
|
visible: true
|
2021-10-07 16:56:11 +02:00
|
|
|
title: qsTr("Accelerate Bubble")
|
|
|
|
|
readonly property double radians_to_degrees: 180 / Math.PI
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2016-12-07 14:58:32 +01:00
|
|
|
Accelerometer {
|
2021-10-07 16:56:11 +02:00
|
|
|
id: accel
|
|
|
|
|
dataRate: 100
|
|
|
|
|
active:true
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2021-10-07 16:56:11 +02:00
|
|
|
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)
|
2014-04-08 12:28:47 +02:00
|
|
|
|
2021-10-07 16:56:11 +02:00
|
|
|
if (isNaN(newX) || isNaN(newY))
|
|
|
|
|
return;
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2021-10-07 16:56:11 +02:00
|
|
|
if (newX < 0)
|
|
|
|
|
newX = 0
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2021-10-07 16:56:11 +02:00
|
|
|
if (newX > mainWindow.width - bubble.width)
|
|
|
|
|
newX = mainWindow.width - bubble.width
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2021-10-07 16:56:11 +02:00
|
|
|
if (newY < 18)
|
|
|
|
|
newY = 18
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2021-10-07 16:56:11 +02:00
|
|
|
if (newY > mainWindow.height - bubble.height)
|
|
|
|
|
newY = mainWindow.height - bubble.height
|
2015-11-09 17:09:42 +01:00
|
|
|
|
2021-10-07 16:56:11 +02:00
|
|
|
bubble.x = newX
|
|
|
|
|
bubble.y = newY
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-28 11:10:55 +01:00
|
|
|
|
2020-08-13 18:15:37 +02:00
|
|
|
function calcPitch(x,y,z) {
|
2021-10-07 16:56:11 +02:00
|
|
|
return -Math.atan2(y, Math.hypot(x, z)) * mainWindow.radians_to_degrees;
|
2017-11-03 15:38:54 +01:00
|
|
|
}
|
2020-08-13 18:15:37 +02:00
|
|
|
function calcRoll(x,y,z) {
|
2021-10-07 16:56:11 +02:00
|
|
|
return -Math.atan2(x, Math.hypot(y, z)) * mainWindow.radians_to_degrees;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Image {
|
|
|
|
|
id: bubble
|
|
|
|
|
source: "Bluebubble.svg"
|
|
|
|
|
smooth: true
|
|
|
|
|
property real centerX: mainWindow.width / 2
|
|
|
|
|
property real centerY: mainWindow.height / 2
|
|
|
|
|
property real bubbleCenter: bubble.width / 2
|
|
|
|
|
x: centerX - bubbleCenter
|
|
|
|
|
y: centerY - bubbleCenter
|
|
|
|
|
|
|
|
|
|
Behavior on y {
|
|
|
|
|
SmoothedAnimation {
|
|
|
|
|
easing.type: Easing.Linear
|
|
|
|
|
duration: 100
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Behavior on x {
|
|
|
|
|
SmoothedAnimation {
|
|
|
|
|
easing.type: Easing.Linear
|
|
|
|
|
duration: 100
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-03 15:38:54 +01:00
|
|
|
}
|
2013-10-28 11:10:55 +01:00
|
|
|
}
|