Warning, /education/gcompris/external/qml-box2d/examples/pulley/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     width: 800
0008     height: 600
0009 
0010     Component {
0011         id: ballComponent
0012         PhysicsItem {
0013             id: box
0014             width: 20
0015             height: 20
0016             bodyType: Body.Dynamic
0017             fixtures: Circle {
0018                 radius: box.width / 2
0019                 density: 10
0020                 friction: 0.3
0021                 restitution: 0.5
0022             }
0023             Rectangle {
0024                 radius: parent.width / 2
0025                 border.color: "blue"
0026                 color: "#EFEFEF"
0027                 anchors.fill: parent
0028             }
0029         }
0030     }
0031 
0032     World { id: physicsWorld; }
0033 
0034     Canvas {
0035         id: cords
0036         anchors.fill: parent
0037         onPaint: {
0038             var bodyACenter = bodyA.body.getWorldCenter();
0039             var bodyBCenter = bodyB.body.getWorldCenter();
0040             var context = cords.getContext("2d");
0041             context.clearRect(0, 0, width, height);
0042             context.beginPath();
0043             context.moveTo(225,100);
0044             context.lineTo(575,100);
0045             context.moveTo(bodyACenter.x, bodyACenter.y);
0046             context.lineTo(225,100);
0047             context.moveTo(bodyBCenter.x, bodyBCenter.y);
0048             context.lineTo(575,100);
0049             context.strokeStyle = "grey";
0050             context.stroke();
0051         }
0052     }
0053 
0054     PhysicsItem {
0055         id: ground
0056         height: 40
0057         anchors {
0058             left: parent.left
0059             right: parent.right
0060             bottom: parent.bottom
0061         }
0062         fixtures: Box {
0063             width: ground.width
0064             height: ground.height
0065             friction: 1
0066         }
0067         Rectangle {
0068             anchors.fill: parent
0069             color: "#DEDEDE"
0070         }
0071     }
0072     Wall {
0073         id: topWall
0074         height: 40
0075         anchors {
0076             left: parent.left
0077             right: parent.right
0078             top: parent.top
0079         }
0080     }
0081 
0082     Wall {
0083         id: leftWall
0084         width: 40
0085         anchors {
0086             left: parent.left
0087             top: parent.top
0088             bottom: parent.bottom
0089             bottomMargin: 40
0090         }
0091     }
0092 
0093     Wall {
0094         id: rightWall
0095         width: 40
0096         anchors {
0097             right: parent.right
0098             top: parent.top
0099             bottom: parent.bottom
0100             bottomMargin: 40
0101         }
0102     }
0103 
0104     PhysicsItem {
0105         id: limiterA
0106         x: 210
0107         y: 85
0108         width:30
0109         height: 30
0110         fixtures: Circle { radius: 15 }
0111         Rectangle {
0112             anchors.fill: parent
0113             radius: 15
0114             color: "green"
0115         }
0116     }
0117 
0118     PhysicsItem {
0119         id: limiterB
0120         x: 560
0121         y: 85
0122         width: 30
0123         height: 30
0124         fixtures: Circle { radius: 15 }
0125         Rectangle {
0126             anchors.fill: parent
0127             radius: 15
0128             color: "green"
0129         }
0130     }
0131 
0132     PhysicsItem {
0133         id: bodyA
0134         x: 125
0135         y: 300
0136         width: 200
0137         height: 100
0138         bodyType: Body.Dynamic
0139         fixtures: [
0140             Box {
0141                 y: 40
0142                 width: bodyA.width
0143                 height: bodyA.height - 40
0144                 density: 10
0145             },
0146             Box {
0147                 x: 0
0148                 y: 0
0149                 width: 10
0150                 height: 40
0151                 density: 10
0152             },
0153             Box {
0154                 x:190
0155                 y: 0
0156                 width: 10
0157                 height: 40
0158                 density: 10
0159             }
0160         ]
0161         Rectangle {
0162             anchors.fill: parent
0163             anchors.topMargin: 40
0164             color:"#555555"
0165         }
0166         Rectangle {
0167             x: 0
0168             y: 0
0169             width: 10
0170             height: 40
0171             color: "#555555"
0172         }
0173         Rectangle {
0174             x: 190
0175             y: 0
0176             width: 10
0177             height: 40
0178             color: "#555555"
0179         }
0180         onXChanged: cords.requestPaint();
0181         onYChanged: cords.requestPaint();
0182     }
0183     PhysicsItem {
0184         id: bodyB
0185         x: 475
0186         y: 300
0187         width: 200
0188         height: 100
0189         bodyType: Body.Dynamic
0190         fixtures: [
0191             Box {
0192                 y: 40
0193                 width: bodyB.width
0194                 height: bodyB.height - 40
0195                 density: 10
0196             },
0197             Box {
0198                 x: 0
0199                 y: 0
0200                 width: 10
0201                 height: 40
0202                 density: 10
0203             },
0204             Box {
0205                 x: 190
0206                 y: 0
0207                 width: 10
0208                 height: 40
0209                 density: 10
0210             }
0211         ]
0212         Rectangle {
0213             anchors.fill: parent
0214             anchors.topMargin: 40
0215             color: "#555555"
0216         }
0217         Rectangle {
0218             x: 0
0219             y: 0
0220             width: 10
0221             height: 40
0222             color: "#555555"
0223         }
0224         Rectangle {
0225             x: 190
0226             y: 0
0227             width: 10
0228             height: 40
0229             color:"#555555"
0230         }
0231     }
0232     PulleyJoint {
0233         bodyA: bodyA.body
0234         bodyB: bodyB.body
0235         groundAnchorA: Qt.point(225,100)
0236         groundAnchorB: Qt.point(575,100)
0237         localAnchorA: Qt.point(100,0)
0238         localAnchorB: Qt.point(100,0)
0239         lengthA: 150
0240         lengthB: 150
0241     }
0242 
0243     PhysicsItem {
0244         id: floor
0245         x: 40
0246         y: 260
0247         width: 720
0248         height: 120
0249         fixtures: [
0250             Box {
0251                 x: 0
0252                 y: 0
0253                 width: 84
0254                 height: floor.height
0255                 friction: 0.2
0256             },
0257             Box {
0258                 x: 286
0259                 y: 0
0260                 width: 148
0261                 height: floor.height
0262                 friction: 0.2
0263             },
0264             Box {
0265                 x: 636
0266                 y: 0
0267                 width: 84
0268                 height: floor.height
0269                 friction: 0.2
0270             }
0271         ]
0272         Rectangle {
0273             x: 0
0274             y: 0
0275             width: 84
0276             height: parent.height
0277             color: "#DEDEDE"
0278         }
0279         Rectangle {
0280             x: 286
0281             y: 0
0282             width: 148
0283             height: parent.height
0284             color: "#DEDEDE"
0285         }
0286         Rectangle {
0287             x: 636
0288             y: 0
0289             width: 84
0290             height: parent.height
0291             color: "#DEDEDE"
0292         }
0293 
0294     }
0295 
0296     PhysicsItem {
0297         id: triangle
0298         x: 370
0299         y: 500
0300         width: 60
0301         height: 60
0302         fixtures: Polygon {
0303             vertices: [
0304                 Qt.point(30,0),
0305                 Qt.point(0,60),
0306                 Qt.point(60,60)
0307             ]
0308         }
0309         Canvas {
0310             id: canvas
0311             anchors.fill: parent
0312             onPaint: {
0313                 var context = canvas.getContext("2d");
0314                 context.beginPath();
0315                 context.moveTo(parent.width / 2,0);
0316                 context.lineTo(0,parent.height);
0317                 context.lineTo(parent.width,parent.height);
0318                 context.lineTo(parent.width / 2,0);
0319                 context.fillStyle = "green";
0320                 context.fill();
0321             }
0322         }
0323     }
0324     PhysicsItem {
0325         id: balancer
0326         x: 70
0327         y: 490
0328         width: 660
0329         height: 10
0330         bodyType: Body.Dynamic
0331         fixtures: Box {
0332             width: balancer.width
0333             height: balancer.height
0334             density: 10
0335             friction: 0.5
0336         }
0337         Rectangle {
0338             anchors.fill: parent
0339             color: "orange"
0340         }
0341     }
0342 
0343     PhysicsItem {
0344         id: circle
0345         x: 370
0346         y: 430
0347         width: 60
0348         height: 60
0349         bodyType: Body.Dynamic
0350         fixtures: Circle {
0351             radius: 30
0352             density: 20
0353             friction: 0.9
0354         }
0355         Rectangle {
0356             anchors.fill: parent
0357             radius: 30
0358             color:  "red"
0359         }
0360     }
0361 
0362     Rectangle {
0363         id: debugButton
0364         x: 50
0365         y: 50
0366         width: 120
0367         height: 30
0368         Text {
0369             text: debugDraw.visible ? "Debug view: on" : "Debug view: off";
0370             anchors.centerIn: parent
0371         }
0372         color: "#DEDEDE"
0373         border.color: "#999"
0374         radius: 5
0375         MouseArea {
0376             anchors.fill: parent
0377             onClicked: debugDraw.visible = !debugDraw.visible;
0378         }
0379     }
0380 
0381     DebugDraw {
0382         id: debugDraw
0383         world: physicsWorld
0384         opacity: 1
0385         visible: false
0386     }
0387 
0388     Timer {
0389         id: ballsTimer
0390         interval: 1000
0391         running: true
0392         repeat: true
0393         onTriggered: {
0394             var newBox = ballComponent.createObject(screen);
0395             newBox.x = 40 + Math.random() * screen.width - 80;
0396             newBox.y = 50;
0397             console.log(newBox.x, newBox.y)
0398         }
0399     }
0400 }