Physics.qml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import QtQuick 2.5
  2. import Box2D 2.0
  3. import Qt.labs.settings 1.0
  4. import ".."
  5. View {
  6. id: root
  7. signal togglePause
  8. signal toggleChaos
  9. signal next
  10. property var pictureDelegate: Component {
  11. ArtDelegate {}
  12. }
  13. Settings {
  14. id: physicsSettings
  15. category: "Physics"
  16. property int feedRate: 100
  17. // 0 is abutting
  18. property int verticalOffset: 5
  19. property real pace: 3
  20. property real density: 1.0
  21. property real friction: 1.0
  22. property bool globalWorld: false
  23. property bool fixedRotation: true
  24. // Very computationally heavy: 40% vs 20% for 0.1 vs 0
  25. property real restitution: 0
  26. }
  27. QtObject {
  28. id: d
  29. property real pace: physicsSettings.pace/60.0
  30. property bool paused: false
  31. }
  32. World {
  33. id: world
  34. timeStep: d.pace
  35. running: physicsSettings.globalWorld && globalUtil.primed
  36. property var limbo: World {
  37. timeStep: world.timeStep
  38. running: world.running
  39. }
  40. }
  41. Component {
  42. id: columnComponent
  43. Item {
  44. id: column
  45. property int stackHeight: 0
  46. property bool full: false
  47. property int xOffset: width * index
  48. property var pictureArray: []
  49. function considerImage() {
  50. if (stackHeight < (1.3 + 1/globalSettings.columnCount)*root.height) {
  51. addImage()
  52. }
  53. }
  54. function addImage() {
  55. var image = pictureDelegate.createObject(column, { x: -1000, y: -1000 })
  56. if (globalSettings.effect !== "" && Effects.validate(globalSettings.effect)) {
  57. image.effect = effectDelegate.createObject(column, { target: image, effect: globalSettings.effect })
  58. }
  59. image.beyondThePale.connect(removeImage)
  60. image.world = physicsSettings.globalWorld ? world : isolatedWorld
  61. image.x = xOffset
  62. stackHeight += image.height
  63. image.y = floor.y - stackHeight - physicsSettings.verticalOffset
  64. pictureArray.push(image)
  65. globalUtil.itemCount++
  66. }
  67. function removeImage(image) {
  68. if (image.effect) {
  69. image.effect.destroy()
  70. }
  71. stackHeight -= image.height
  72. image.destroy()
  73. globalUtil.itemCount--
  74. }
  75. function shift() {
  76. if (pictureArray.length > 0) {
  77. var image = pictureArray.shift()
  78. image.world = image.world.limbo
  79. addImage()
  80. }
  81. }
  82. Component.onCompleted: {
  83. columnArray.push(this)
  84. }
  85. onStackHeightChanged: {
  86. if (!column.full && (stackHeight > root.height)) {
  87. globalUtil.registerColumnPrimed()
  88. column.full = true
  89. }
  90. }
  91. width: parent.width/globalSettings.columnCount
  92. anchors { top: parent.top; bottom: parent.bottom }
  93. World {
  94. id: isolatedWorld
  95. timeStep: d.pace
  96. running: !physicsSettings.globalWorld && globalUtil.primed
  97. property var limbo: World {
  98. timeStep: isolatedWorld.timeStep
  99. running: isolatedWorld.running
  100. }
  101. }
  102. RectangleBoxBody {
  103. id: floor
  104. world: isolatedWorld
  105. height: 0
  106. width: parent.width
  107. x: xOffset
  108. anchors {
  109. top: parent.bottom
  110. }
  111. friction: 1
  112. }
  113. Timer {
  114. id: pumpTimer
  115. interval: Math.abs(physicsSettings.feedRate)
  116. repeat: true
  117. running: true
  118. onTriggered: considerImage()
  119. }
  120. Timer {
  121. id: deathTimer
  122. running: !globalSettings.commonFeed && globalUtil.primed && d.paused
  123. repeat: true
  124. interval: globalUtil.adjustedInterval
  125. onTriggered: shift()
  126. }
  127. Connections {
  128. target: root
  129. onTogglePause: d.paused = !d.paused
  130. onNext: deathTimer.triggered()
  131. onToggleChaos: physicsSettings.fixedRotation = !physicsSettings.fixedRotation
  132. }
  133. }
  134. }
  135. // floor
  136. RectangleBoxBody {
  137. id: globalFloor
  138. world: world
  139. height: 0
  140. anchors {
  141. left: parent.left
  142. right: parent.right
  143. top: parent.bottom
  144. }
  145. friction: 1
  146. }
  147. DebugDraw {
  148. id: debugDraw
  149. enabled: false
  150. z: 1
  151. world: world
  152. anchors.fill: parent
  153. opacity: 0.75
  154. visible: enabled
  155. }
  156. Keys.onUpPressed: root.togglePause()
  157. Keys.onDownPressed: root.toggleChaos() //root.next()
  158. Component.onCompleted: {
  159. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  160. effectDelegate.status !== Component.Ready && console.log('Component failed with:' + effectDelegate.errorString())
  161. }
  162. }