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

0001 import QtQuick 2.2
0002 import Box2D 2.0
0003 import QtQuick.Controls 1.1
0004 import "../shared"
0005 
0006 Rectangle {
0007     id: screen
0008     width: 800
0009     height: 600
0010     color: "#EFEFFF"
0011 
0012     Component {
0013         id: ballsComponent
0014         PhysicsItem {
0015             id: ball
0016             width: 20
0017             height: 20
0018             bodyType: Body.Dynamic
0019             property color boxColor: "blue"
0020             fixtures: Circle {
0021                 id: fx
0022                 radius: ball.width / 2
0023                 density: 0.1
0024                 friction: 1
0025                 restitution: 0.5
0026             }
0027             Rectangle {
0028                 radius: parent.width / 2
0029                 border.color: "blue"
0030                 color: "#EFEFEF"
0031                 width: parent.width
0032                 height: parent.height
0033                 smooth: true
0034             }
0035         }
0036     }
0037     World { id: physicsWorld }
0038 
0039     Wall {
0040         id: topWall
0041         height: 40
0042         anchors {
0043             left: parent.left
0044             right: parent.right
0045             top: parent.top
0046         }
0047     }
0048 
0049     Wall {
0050         id: leftWall
0051         width: 40
0052         anchors {
0053             left: parent.left
0054             top: parent.top
0055             bottom: parent.bottom
0056             bottomMargin: 40
0057         }
0058     }
0059 
0060     Wall {
0061         id: rightWall
0062         width: 40
0063         anchors {
0064             right: parent.right
0065             top: parent.top
0066             bottom: parent.bottom
0067             bottomMargin: 40
0068         }
0069     }
0070 
0071     PhysicsItem {
0072         id: ground
0073         height: 40
0074         anchors {
0075             left: parent.left
0076             right: parent.right
0077             bottom: parent.bottom
0078         }
0079         fixtures: Box {
0080             width: ground.width
0081             height: ground.height
0082             friction: 1
0083             density: 1
0084         }
0085         Rectangle {
0086             anchors.fill: parent
0087             color: "#DEDEDE"
0088         }
0089     }
0090 
0091     PhysicsItem {
0092         anchors.right: parent.right
0093         anchors.bottom: parent.bottom
0094         anchors.bottomMargin: 40
0095         anchors.rightMargin: 40
0096         width: 300
0097         height: 300
0098         fixtures: Chain {
0099             vertices: [
0100                 Qt.point(0,300),
0101                 Qt.point(210,180),
0102                 Qt.point(240,130),
0103                 Qt.point(240,50),
0104                 Qt.point(220,0),
0105                 Qt.point(300,0),
0106                 Qt.point(300,300)
0107             ]
0108         }
0109         Canvas {
0110             id: canvas1
0111             anchors.fill: parent
0112             onPaint: {
0113                 var context = canvas1.getContext("2d");
0114                 context.beginPath();
0115                 context.moveTo(0,300);
0116                 context.lineTo(210,180);
0117                 context.lineTo(240,130);
0118                 context.lineTo(240,50);
0119                 context.lineTo(220,0);
0120                 context.lineTo(300,0);
0121                 context.lineTo(300,300);
0122                 context.fillStyle = "#AAA";
0123                 context.fill();
0124             }
0125         }
0126     }
0127 
0128     Wall {
0129         anchors.right: parent.right
0130         anchors.rightMargin: 40
0131         width:500
0132         height: 40
0133         y: 220
0134     }
0135 
0136     PhysicsItem {
0137         id: body
0138         property int speed: speedSlider.value
0139         property int k: -1
0140         x: 600
0141         y: 100
0142         width: 100
0143         height: 20
0144         bodyType: Body.Dynamic
0145         fixtures: Box {
0146             width: body.width
0147             height: body.height
0148             density: 0.8
0149             friction: 0.5
0150             restitution: 0.8
0151         }
0152         Rectangle {
0153             anchors.fill: parent
0154             color: "orange"
0155         }
0156     }
0157     PhysicsItem {
0158         id: wheelA
0159         x: 700
0160         y: 100
0161         width: 48
0162         height: 48
0163         bodyType: Body.Dynamic
0164         fixtures: Circle {
0165             radius: wheelA.width / 2
0166             density: 0.8
0167             friction: 10
0168             restitution: 0.8
0169         }
0170         Image {
0171             source: "images/wheel.png"
0172             anchors.fill: parent
0173         }
0174     }
0175 
0176     PhysicsItem {
0177         id: wheelB
0178         x: 600
0179         y: 100
0180         width: 48
0181         height: 48
0182         bodyType: Body.Dynamic
0183         fixtures: Circle {
0184             radius: wheelB.width / 2
0185             density: 0.8
0186             friction: 10
0187             restitution: 0.8
0188         }
0189         Image {
0190             source: "images/wheel.png"
0191             anchors.fill: parent
0192         }
0193     }
0194 
0195     WheelJoint {
0196         id: wheelJointA
0197         bodyA: body.body
0198         bodyB: wheelA.body
0199         localAnchorA: Qt.point(100,10)
0200         localAnchorB: Qt.point(24,24)
0201         enableMotor: true
0202         motorSpeed: body.k * body.speed
0203         maxMotorTorque: torqueSlider.value
0204         frequencyHz: 10
0205     }
0206 
0207     WheelJoint {
0208         id: wheelJointB
0209         bodyA: body.body
0210         bodyB: wheelB.body
0211         localAnchorA: Qt.point(0,10)
0212         localAnchorB: Qt.point(24,24)
0213         enableMotor: true
0214         motorSpeed: body.k * body.speed
0215         maxMotorTorque: torqueSlider.value
0216         frequencyHz: 10
0217     }
0218 
0219     Rectangle {
0220         id: debugButton
0221         x: 50
0222         y: 50
0223         width: 120
0224         height: 30
0225         Text {
0226             text: "Debug view: " + (debugDraw.visible ? "on" : "off")
0227             anchors.centerIn: parent
0228         }
0229         color: "#DEDEDE"
0230         border.color: "#999"
0231         radius: 5
0232         MouseArea {
0233             anchors.fill: parent
0234             onClicked: debugDraw.visible = !debugDraw.visible;
0235         }
0236     }
0237     Text {
0238         id: leftMotorState
0239         x: 180
0240         y: 50
0241         width: 200
0242         text : "Speed: " + speedSlider.value +
0243                "\nTorque: " + torqueSlider.value
0244     }
0245 
0246     Slider {
0247         id: speedSlider
0248         x: 50
0249         y: 90
0250         width: 120
0251         height: 20
0252         minimumValue: 0
0253         maximumValue: 720
0254         value: 0
0255         stepSize: 1
0256     }
0257     Slider {
0258         id: torqueSlider
0259         x: 50
0260         y: 120
0261         width: 120
0262         height: 20
0263         minimumValue: 1
0264         maximumValue: 100
0265         value: 50
0266         stepSize: 1
0267     }
0268     Rectangle {
0269         id: leftButton
0270         x: 50
0271         y: 150
0272         width: 30
0273         height: 30
0274         color: body.k > 0 ? "#FFF" : "#DEDEDE"
0275         border.color: "#999"
0276         radius: 5
0277         Image {
0278             width: 24
0279             height: 24
0280             anchors.centerIn: parent
0281             source: "images/arrow.png"
0282             MouseArea {
0283                 anchors.fill: parent
0284                 onClicked: {
0285                     body.k = -1
0286                 }
0287             }
0288         }
0289     }
0290     Rectangle {
0291         id: rightButton
0292         x: 90
0293         y: 150
0294         width: 30
0295         height: 30
0296         color: body.k < 0 ? "#FFF" : "#DEDEDE"
0297         border.color: "#999"
0298         radius: 5
0299         Image {
0300             width: 24
0301             height: 24
0302             anchors.centerIn: parent
0303             source: "images/arrow.png"
0304             rotation: 180
0305             MouseArea {
0306                 anchors.fill: parent
0307                 onClicked: body.k = 1
0308             }
0309         }
0310     }
0311 
0312     DebugDraw {
0313         id: debugDraw
0314         world: physicsWorld
0315         opacity: 0.5
0316         z: 1
0317         visible: false
0318     }
0319 
0320     Timer {
0321         id: ballsTimer
0322         interval: 1000
0323         running: true
0324         repeat: true
0325         onTriggered: {
0326             var newBox = ballsComponent.createObject(screen);
0327             newBox.x = 40 + Math.round(Math.random() * 720);
0328             newBox.y = 50;
0329         }
0330     }
0331 }