Warning, /education/gcompris/external/qml-box2d/examples/weld/main.qml is written in an unsupported language. File is not indexed.

0001 import QtQuick 2.2
0002 import Box2D 2.0
0003 import "../shared"
0004 
0005 Rectangle {
0006     id: screen
0007 
0008     width: 800
0009     height: 600
0010 
0011     property int hz: 0
0012 
0013     Component {
0014         id: ballComponent
0015 
0016         PhysicsItem {
0017             id: box
0018 
0019             width: 20
0020             height: 20
0021             bodyType: Body.Dynamic
0022 
0023             fixtures: Circle {
0024                 radius: box.width / 2
0025                 density: 0.1
0026                 friction: 0.3
0027                 restitution: 0.5
0028             }
0029 
0030             Rectangle {
0031                 radius: parent.width / 2
0032                 border.color: "blue"
0033                 color: "#EFEFEF"
0034                 width: parent.width
0035                 height: parent.height
0036             }
0037         }
0038     }
0039 
0040     Component {
0041         id: linkComponent
0042 
0043         PhysicsItem {
0044             id: link
0045 
0046             width: 20
0047             height: 20
0048             z: 1
0049             bodyType: Body.Dynamic
0050 
0051             fixtures: Box {
0052                 width: link.width
0053                 height: link.height
0054                 density: 0.2
0055                 friction: 0.3
0056             }
0057 
0058             Rectangle {
0059                 anchors.fill: parent
0060                 border.color: "#DEDEDE"
0061                 color: "#44A51C"
0062             }
0063         }
0064     }
0065 
0066     Component {
0067         id: linkJoint
0068         WeldJoint {
0069             dampingRatio: 0.5
0070             frequencyHz: hz
0071         }
0072     }
0073 
0074     function createBranch(base,count) {
0075         var prevLink = base;
0076         var angle = Math.random() * 10 - 5;
0077         for (var i = 0; i < count; i++) {
0078             var newLink = linkComponent.createObject(screen);
0079             newLink.width = 20;
0080             newLink.height = 3 + count - i;
0081             if (count % 2) {
0082                 newLink.x = prevLink.x + prevLink.width;
0083                 newLink.y = prevLink.y - (prevLink.height / 2);
0084             } else {
0085                 newLink.x = prevLink.x - newLink.width;
0086                 newLink.y = prevLink.y - (prevLink.height / 2);
0087             }
0088             var newJoint = linkJoint.createObject(screen);
0089             if (count % 2) {
0090                 newJoint.localAnchorA = Qt.point(prevLink.width, prevLink.height / 2);
0091                 newJoint.localAnchorB = Qt.point(0, newLink.height / 2);
0092             } else {
0093                 newJoint.localAnchorA = Qt.point(0, prevLink.height / 2);
0094                 newJoint.localAnchorB = Qt.point(newLink.width, newLink.height / 2);
0095             }
0096             newJoint.referenceAngle = angle;
0097             newJoint.bodyA = prevLink.body;
0098             newJoint.bodyB = newLink.body;
0099             prevLink = newLink;
0100         }
0101     }
0102 
0103     World { id: physicsWorld }
0104 
0105     Component.onCompleted: {
0106         var prevLink = bodyA;
0107         for (var i = 0; i < 10; i++) {
0108             var newLink = linkComponent.createObject(screen);
0109             newLink.x = 350 + i * 3;
0110             newLink.y = 500 - (i * 40);
0111             newLink.width = 20 - i * 1.5;
0112             newLink.height = 40;
0113             var newJoint = linkJoint.createObject(screen);
0114             if (i == 0)
0115                 newJoint.localAnchorA = Qt.point(bodyA.width / 2, 0);
0116             else
0117                 newJoint.localAnchorA = Qt.point(newLink.width / 2, 0);
0118             newJoint.localAnchorB = Qt.point(newLink.width / 2, newLink.height);
0119             newJoint.bodyA = prevLink.body;
0120             newJoint.bodyB = newLink.body;
0121             prevLink = newLink;
0122             createBranch(newLink, 10 - i);
0123         }
0124     }
0125 
0126     PhysicsItem {
0127         id: ground
0128         height: 40
0129         anchors {
0130             left: parent.left
0131             right: parent.right
0132             bottom: parent.bottom
0133         }
0134         fixtures: Box {
0135             width: ground.width
0136             height: ground.height
0137             friction: 1
0138             density: 1
0139         }
0140         Rectangle {
0141             anchors.fill: parent
0142             color: "#DEDEDE"
0143         }
0144     }
0145     Wall {
0146         id: topWall
0147         height: 40
0148         anchors {
0149             left: parent.left
0150             right: parent.right
0151             top: parent.top
0152         }
0153     }
0154 
0155     Wall {
0156         id: leftWall
0157         width: 40
0158         anchors {
0159             left: parent.left
0160             top: parent.top
0161             bottom: parent.bottom
0162             bottomMargin: 40
0163         }
0164     }
0165 
0166     Wall {
0167         id: rightWall
0168         width: 40
0169         anchors {
0170             right: parent.right
0171             top: parent.top
0172             bottom: parent.bottom
0173             bottomMargin: 40
0174         }
0175     }
0176 
0177     PhysicsItem {
0178         id: bodyA
0179         width: 100
0180         height: 20
0181         x: 300
0182         y: 540
0183         fixtures: Box {
0184             width: bodyA.width
0185             height: bodyA.height
0186         }
0187         Rectangle {
0188             anchors.fill: parent
0189             color: "black"
0190         }
0191     }
0192 
0193     Rectangle {
0194         id: debugButton
0195         x: 50
0196         y: 50
0197         width: 120
0198         height: 30
0199         Text {
0200             text: debugDraw.visible ? "Debug view: on" : "Debug view: off";
0201             anchors.centerIn: parent
0202         }
0203         color: "#DEDEDE"
0204         border.color: "#999"
0205         radius: 5
0206         MouseArea {
0207             anchors.fill: parent
0208             onClicked: debugDraw.visible = !debugDraw.visible;
0209         }
0210     }
0211     Rectangle {
0212         id: crazyButton
0213         x: 180
0214         y: 50
0215         width: 70
0216         height: 30
0217         Text {
0218             text: "Get crazy"
0219             anchors.centerIn: parent
0220         }
0221         color: "#DEDEDE"
0222         border.color: "#999"
0223         radius: 5
0224         MouseArea {
0225             anchors.fill: parent
0226             onClicked: {
0227                 if (hz == 0) {
0228                     hz = 60;
0229                     crazyButton.color = "#999";
0230                 } else {
0231                     hz = 0;
0232                     crazyButton.color = "#DEDEDE";
0233                 }
0234             }
0235         }
0236     }
0237 
0238     DebugDraw {
0239         id: debugDraw
0240         world: physicsWorld
0241         opacity: 1
0242         visible: false
0243         z: 10
0244     }
0245 
0246     Timer {
0247         id: ballsTimer
0248         interval: 1000
0249         running: true
0250         repeat: true
0251         onTriggered: {
0252             var newBox = ballComponent.createObject(screen);
0253             newBox.x = 40 + (Math.random() * screen.width - 80);
0254             newBox.y = 50;
0255         }
0256     }
0257 }