Advertisement
Guest User

Untitled

a guest
May 31st, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtSensors module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** BSD License Usage
  18. ** Alternatively, you may use this file under the terms of the BSD license
  19. ** as follows:
  20. **
  21. ** "Redistribution and use in source and binary forms, with or without
  22. ** modification, are permitted provided that the following conditions are
  23. ** met:
  24. **   * Redistributions of source code must retain the above copyright
  25. **     notice, this list of conditions and the following disclaimer.
  26. **   * Redistributions in binary form must reproduce the above copyright
  27. **     notice, this list of conditions and the following disclaimer in
  28. **     the documentation and/or other materials provided with the
  29. **     distribution.
  30. **   * Neither the name of The Qt Company Ltd nor the names of its
  31. **     contributors may be used to endorse or promote products derived
  32. **     from this software without specific prior written permission.
  33. **
  34. **
  35. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  46. **
  47. ** $QT_END_LICENSE$
  48. **
  49. ****************************************************************************/
  50.  
  51.  
  52. import QtQuick 2.1
  53. import QtQuick.Controls 1.0
  54.  
  55. //! [0]
  56. import QtSensors 5.10
  57. //! [0]
  58.  
  59.  
  60. ApplicationWindow {
  61.     title: "Accelerate Bubble"
  62.     id: mainWindow
  63.     width: 320
  64.     height: 480
  65.     visible: true
  66.     readonly property double radians_to_degrees: 180 / Math.PI
  67.  
  68.     property var modes: [ "combined", "user", "gravity" ]
  69.     property var modesMap: { "combined": Accelerometer.Combined, "user": Accelerometer.User, "gravity": Accelerometer.Gravity }
  70.     property int modesIter: 0;
  71.  
  72.     Button{
  73.         anchors.horizontalCenter: parent.horizontalCenter
  74.         anchors.top: parent.top
  75.         anchors.topMargin: 20
  76.         width: 280
  77.         text: modes[modesIter];
  78.  
  79.         onClicked: {
  80.             modesIter = ((++modesIter)%modes.length);
  81.         }
  82.     }
  83.  
  84. //! [1]
  85.     Accelerometer {
  86.         id: accel
  87.         dataRate: 100
  88.         active:true
  89.         accelerationMode: modesMap[modes[modesIter]];
  90.  
  91.         onReadingChanged: {
  92.  
  93.             console.log("reading x:  " + String( accel.reading.x) )
  94.             console.log("reading y:  " + String( accel.reading.y) )
  95.             console.log("reading z:  " + String( accel.reading.z) )
  96.  
  97.             var newX = (bubble.x + calcRoll(accel.reading.x, accel.reading.y, accel.reading.z) * .1)
  98.             var newY = (bubble.y - calcPitch(accel.reading.x, accel.reading.y, accel.reading.z) * .1)
  99.  
  100.             if (isNaN(newX) || isNaN(newY))
  101.                 return;
  102.  
  103.             if (newX < 0)
  104.                 newX = 0
  105.  
  106.             if (newX > mainWindow.width - bubble.width)
  107.                 newX = mainWindow.width - bubble.width
  108.  
  109.             if (newY < 18)
  110.                 newY = 18
  111.  
  112.             if (newY > mainWindow.height - bubble.height)
  113.                 newY = mainWindow.height - bubble.height
  114.  
  115.                 bubble.x = newX
  116.                 bubble.y = newY
  117.         }
  118. //! [3]
  119.     }
  120.  
  121.     function calcPitch(x,y,z) {
  122.         return -Math.atan2(y, Math.hypot(x, z)) * mainWindow.radians_to_degrees;
  123.     }
  124.     function calcRoll(x,y,z) {
  125.         return -Math.atan2(x, Math.hypot(y, z)) * mainWindow.radians_to_degrees;
  126.     }
  127.  
  128.     Image {
  129.         id: bubble
  130.         source: "content/Bluebubble.svg"
  131.         smooth: true
  132.         property real centerX: mainWindow.width / 2
  133.         property real centerY: mainWindow.height / 2
  134.         property real bubbleCenter: bubble.width / 2
  135.         x: centerX - bubbleCenter
  136.         y: centerY - bubbleCenter
  137.  
  138.         Behavior on y {
  139.             SmoothedAnimation {
  140.                 easing.type: Easing.Linear
  141.                 duration: 100
  142.             }
  143.         }
  144.         Behavior on x {
  145.             SmoothedAnimation {
  146.                 easing.type: Easing.Linear
  147.                 duration: 100
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement