Warning, /education/gcompris/external/qml-box2d/examples/cannon/main.qml is written in an unsupported language. File is not indexed.
0001 import QtQuick 2.2 0002 import QtQuick.Controls 1.1 0003 import Box2D 2.0 0004 import QtMultimedia 5.0 0005 import "../shared" 0006 0007 Rectangle { 0008 id: root 0009 0010 width: 800 0011 height: 600 0012 0013 Component { 0014 id: link 0015 Rectangle { 0016 id: chainBall 0017 0018 width: 20 0019 height: 20 0020 x: 400 0021 color: "orange" 0022 radius: 10 0023 0024 property Body body: circleBody 0025 0026 CircleBody { 0027 id: circleBody 0028 target: chainBall 0029 world: physicsWorld 0030 0031 bodyType: Body.Dynamic 0032 radius: 10 0033 friction: 0.9 0034 density: 0.8 0035 } 0036 } 0037 } 0038 0039 Component { 0040 id: linkJoint 0041 RevoluteJoint { 0042 localAnchorA: Qt.point(10, 30) 0043 localAnchorB: Qt.point(10, 5) 0044 collideConnected: true 0045 } 0046 } 0047 0048 Component { 0049 id: ball 0050 Rectangle { 0051 id: bulletBall 0052 0053 width: 10 0054 height: 10 0055 radius: 5 0056 color: "black" 0057 smooth: true 0058 0059 property Body body: circleBody 0060 0061 CircleBody { 0062 id: circleBody 0063 0064 target: bulletBall 0065 world: physicsWorld 0066 0067 bullet: true 0068 bodyType: Body.Dynamic 0069 0070 radius: 5 0071 density: 0.9 0072 friction: 0.9 0073 restitution: 0.2 0074 } 0075 } 0076 } 0077 0078 Component { 0079 id: dominoComponent 0080 RectangleBoxBody { 0081 width: 10 0082 height: 50 0083 x: 0 0084 y: 510 0085 color: "black" 0086 0087 world: physicsWorld 0088 bodyType: Body.Dynamic 0089 0090 density: 1 0091 friction: 0.3 0092 restitution: 0.5 0093 } 0094 } 0095 0096 World { id: physicsWorld } 0097 0098 function createDominos() { 0099 var i; 0100 0101 for (i = 0; i < 5; i++) { 0102 var newDomino = dominoComponent.createObject(root); 0103 newDomino.x = 500 + 50 * i; 0104 newDomino.y = 510; 0105 } 0106 for (i = 0; i < 4; i ++) { 0107 newDomino = dominoComponent.createObject(root); 0108 newDomino.x = 525 + 50 * i; 0109 newDomino.y = 480; 0110 newDomino.rotation = 90; 0111 } 0112 for (i = 0; i < 4; i ++) { 0113 newDomino = dominoComponent.createObject(root); 0114 newDomino.x = 525 + 50 * i; 0115 newDomino.y = 450; 0116 } 0117 for (i = 0; i < 3; i ++) { 0118 newDomino = dominoComponent.createObject(root); 0119 newDomino.x = 550 + 50 * i; 0120 newDomino.y = 420; 0121 newDomino.rotation = 90; 0122 } 0123 } 0124 0125 function createChain() { 0126 var i, prev = chainAnchor; 0127 for (i = 0; i < 12; i++) { 0128 var y = 300 + i * 20 - 5; 0129 var newLink = link.createObject(root); 0130 newLink.y = y; 0131 var newJoint = linkJoint.createObject(root); 0132 newJoint.bodyA = prev.body; 0133 newJoint.bodyB = newLink.body; 0134 prev = newLink; 0135 } 0136 } 0137 0138 Component.onCompleted: { 0139 createDominos(); 0140 createChain(); 0141 } 0142 0143 RectangleBoxBody { 0144 id: ground 0145 world: physicsWorld 0146 height: 40 0147 anchors { 0148 left: parent.left 0149 right: parent.right 0150 bottom: parent.bottom 0151 } 0152 friction: 1 0153 density: 1 0154 color: "#DEDEDE" 0155 } 0156 0157 Wall { 0158 id: topWall 0159 height: 40 0160 anchors { 0161 left: parent.left 0162 right: parent.right 0163 top: parent.top 0164 } 0165 } 0166 0167 Wall { 0168 id: leftWall 0169 width: 40 0170 anchors { 0171 left: parent.left 0172 top: parent.top 0173 bottom: parent.bottom 0174 bottomMargin: 40 0175 } 0176 } 0177 0178 Wall { 0179 id: rightWall 0180 width: 40 0181 anchors { 0182 right: parent.right 0183 top: parent.top 0184 bottom: parent.bottom 0185 bottomMargin: 40 0186 } 0187 } 0188 0189 ImageBoxBody { 0190 id: canon 0191 bodyType: Body.Dynamic 0192 world: physicsWorld 0193 x: 150 0194 y: 443 0195 source: "images/cannon.png" 0196 density: 0.5 0197 } 0198 0199 ImageBoxBody { 0200 id: canonBase 0201 x: 50 0202 y: 493 0203 source: "images/cannon_base.png" 0204 world: physicsWorld 0205 density: 0.5 0206 } 0207 0208 RevoluteJoint { 0209 id: joint 0210 bodyA: canonBase.body 0211 bodyB: canon.body 0212 localAnchorA: Qt.point(75, 18) 0213 localAnchorB: Qt.point(36, 19) 0214 collideConnected: false 0215 motorSpeed: 0 0216 enableMotor: false 0217 maxMotorTorque: 100 0218 enableLimit: true 0219 lowerAngle: 0 0220 upperAngle: -60 0221 } 0222 0223 RectangleBoxBody { 0224 id: chainAnchor 0225 width: 20 0226 height: 20 0227 x: 400 0228 y: 230 0229 color: "black" 0230 world: physicsWorld 0231 } 0232 0233 Rectangle { 0234 id: debugButton 0235 x: 50 0236 y: 50 0237 width: 120 0238 height: 30 0239 Text { 0240 id: debugButtonText 0241 text: "Debug view: off" 0242 anchors.centerIn: parent 0243 } 0244 color: "#DEDEDE" 0245 border.color: "#999" 0246 radius: 5 0247 MouseArea { 0248 anchors.fill: parent 0249 onClicked: { 0250 debugDraw.visible = !debugDraw.visible; 0251 debugButtonText.text = debugDraw.visible ? "Debug view: on" : "Debug view: off"; 0252 } 0253 } 0254 } 0255 0256 Rectangle { 0257 id: upButton 0258 x: 50 0259 y: 90 0260 width: 50 0261 height: 50 0262 Text { 0263 id: upButtonText 0264 text: "up" 0265 anchors.centerIn: parent 0266 } 0267 color: "#DEDEDE" 0268 border.color: "#999" 0269 radius: 5 0270 MouseArea { 0271 acceptedButtons: Qt.LeftButton 0272 anchors.fill: parent 0273 onPressed: { 0274 canon.fixture.density = 0.5; 0275 joint.motorSpeed = -15; 0276 joint.enableMotor = true; 0277 upButton.color = "#AAA"; 0278 gearSound.play(); 0279 } 0280 onReleased: { 0281 joint.motorSpeed = 0; 0282 upButton.color = "#DEDEDE"; 0283 gearSound.stop() 0284 } 0285 } 0286 } 0287 0288 Rectangle { 0289 id: downButton 0290 x: 110 0291 y: 90 0292 width: 50 0293 height: 50 0294 Text { 0295 id: downButtonText 0296 text: "down" 0297 anchors.centerIn: parent 0298 } 0299 color: "#DEDEDE" 0300 border.color: "#999" 0301 radius: 5 0302 MouseArea { 0303 acceptedButtons: Qt.LeftButton 0304 anchors.fill: parent 0305 onPressed: { 0306 joint.motorSpeed = 15; 0307 joint.enableMotor = true; 0308 downButton.color = "#AAA"; 0309 gearSound.play(); 0310 } 0311 onReleased: { 0312 joint.motorSpeed = 0; 0313 downButton.color = "#DEDEDE"; 0314 gearSound.stop(); 0315 } 0316 } 0317 } 0318 0319 Rectangle { 0320 id: shotButton 0321 x: 170 0322 y: 90 0323 width: 50 0324 height: 50 0325 0326 Text { 0327 id: shotButtonText 0328 text: "shot!" 0329 anchors.centerIn: parent 0330 } 0331 color: "#DEDEDE" 0332 border.color: "#999" 0333 radius: 5 0334 MouseArea { 0335 acceptedButtons: Qt.LeftButton 0336 anchors.fill: parent 0337 onClicked: { 0338 var angle = Math.abs(joint.getJointAngle()); 0339 var offsetX = 65 * Math.cos(angle * Math.PI / 180); 0340 var offsetY = 65 * Math.sin(angle * Math.PI / 180); 0341 var newBall = ball.createObject(root); 0342 newBall.x = 125 + offsetX; 0343 newBall.y = 500 - offsetY; 0344 var impulse = power.value; 0345 var impulseX = impulse * Math.cos(angle * Math.PI / 180); 0346 var impulseY = impulse * Math.sin(angle * Math.PI / 180); 0347 newBall.body.applyLinearImpulse(Qt.point(impulseX, -impulseY), newBall.body.getWorldCenter()); 0348 shotSound.play(); 0349 } 0350 } 0351 } 0352 0353 Slider { 0354 id: power 0355 minimumValue: 0.01 0356 maximumValue: 5 0357 value: 3 0358 width: 200 0359 height: 30 0360 x: 230 0361 y: 100 0362 } 0363 0364 DebugDraw { 0365 id: debugDraw 0366 world: physicsWorld 0367 visible: false 0368 z: 1 0369 } 0370 0371 SoundEffect { id: shotSound; source: "sounds/cannon.wav" } 0372 SoundEffect { id: gearSound; source: "sounds/gear.wav" } 0373 }