Warning, /education/gcompris/external/qml-box2d/examples/raycast/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     width: 800
0008     height: 600
0009 
0010     Component {
0011         id: ballComponent
0012         PhysicsItem {
0013             id: ball
0014             width: 20
0015             height: 20
0016             bodyType: Body.Dynamic
0017 
0018             fixtures: Circle {
0019                 property bool isBall: true
0020                 radius: 10
0021                 density: 0.5
0022                 friction: 1
0023                 restitution: 0.2
0024             }
0025 
0026             Rectangle {
0027                 id: ballShape
0028                 border.color: "#999"
0029                 color: "#DEDEDE"
0030                 width: parent.width
0031                 height: parent.height
0032                 radius: 10
0033             }
0034         }
0035     }
0036 
0037     World {
0038         id: physicsWorld
0039 
0040         onStepped:
0041             physicsWorld.rayCast(sensorRay,
0042                                  sensorRay.point1,
0043                                  sensorRay.point2)
0044     }
0045 
0046     Item {
0047         id: physicsRoot
0048         anchors.fill: parent
0049 
0050         Wall {
0051             id: leftWall
0052             width: 40
0053             anchors {
0054                 left: parent.left
0055                 top: parent.top
0056                 bottom: parent.bottom
0057                 bottomMargin: 40
0058             }
0059         }
0060 
0061         Wall {
0062             id: rightWall
0063             width: 40
0064             anchors {
0065                 right: parent.right
0066                 top: parent.top
0067                 bottom: parent.bottom
0068                 bottomMargin: 40
0069             }
0070         }
0071 
0072         PhysicsItem {
0073             id: ground
0074             height: 40
0075             anchors {
0076                 left: parent.left
0077                 right: parent.right
0078                 bottom: parent.bottom
0079             }
0080             fixtures: Polygon {
0081                 vertices: [
0082                     Qt.point(0,0),
0083                     Qt.point(ground.width,ground.height),
0084                     Qt.point(0,ground.height)
0085 
0086                 ]
0087                 friction: 0.2
0088                 density: 0.5
0089             }
0090             Canvas {
0091                 id: groundCanvas
0092                 anchors.fill: parent
0093                 onPaint: {
0094                     var context = groundCanvas.getContext("2d");
0095                     context.beginPath();
0096                     context.moveTo(0,0);
0097                     context.lineTo(parent.width,parent.height);
0098                     context.lineTo(0,parent.height);
0099                     context.lineTo(0,0);
0100                     context.fillStyle = "#DEDEDE";
0101                     context.fill();
0102                 }
0103             }
0104         }
0105 
0106         RayCast {
0107             id: sensorRay
0108             property point point1: Qt.point(40, 200)
0109             property point point2: Qt.point(fractionSlider.value * 10, 200)
0110             onFixtureReported: {
0111                 if (fixture.isBall) {
0112                     intersectionPoint.x = point.x - 5;
0113                     intersectionPoint.y = point.y - 5;
0114                     intersectionPoint.opacity = 1;
0115                     pointHideAnimation.running = true;
0116                     bucketAnimation.to = point.x - 15;
0117                     bucketAnimation.running = true;
0118                 }
0119             }
0120         }
0121 
0122         Rectangle {
0123             x: 40
0124             y: 200
0125             width: 10 * fractionSlider.value
0126             height: 1
0127             color: "aqua"
0128             opacity: 1
0129         }
0130 
0131         PhysicsItem {
0132             id: bucket
0133             x: 60
0134             y: 480
0135             height: 50
0136             width: 40
0137             bodyType: Body.Kinematic
0138             fixtures: [
0139                 Polygon {
0140                     vertices: [
0141                         Qt.point(0,0),
0142                         Qt.point(40,0),
0143                         Qt.point(35,50),
0144                         Qt.point(5,50)
0145                     ]
0146                 },
0147                 Edge {
0148                     vertices: [
0149                         Qt.point(0,-1),
0150                         Qt.point(40,-1)
0151                     ]
0152                     sensor: true
0153                     onBeginContact: {
0154                         if (other.isBall)
0155                             other.getBody().target.destroy();
0156                     }
0157                 }
0158             ]
0159             Canvas {
0160                 id:bucketCanvas
0161                 anchors.fill: parent
0162                 onPaint: {
0163                     var context = bucketCanvas.getContext("2d");
0164                     context.beginPath();
0165                     context.moveTo(0,0);
0166                     context.lineTo(40,0);
0167                     context.lineTo(35,50);
0168                     context.lineTo(5,50);
0169                     context.lineTo(0,0);
0170                     context.fillStyle = "lightblue";
0171                     context.fill();
0172                 }
0173             }
0174             PropertyAnimation {
0175                 id:bucketAnimation
0176                 target: bucket
0177                 property: "x"
0178                 duration: 300
0179                 easing.type: Easing.OutBack
0180             }
0181         }
0182 
0183         Rectangle {
0184             id: debugButton
0185             x: 50
0186             y: 10
0187             width: 120
0188             height: 30
0189             Text {
0190                 text: debugDraw.visible ? "Debug view: on" : "Debug view: off";
0191                 anchors.centerIn: parent
0192             }
0193             color: "#DEDEDE"
0194             border.color: "#999"
0195             radius: 5
0196             MouseArea {
0197                 anchors.fill: parent
0198                 onClicked: debugDraw.visible = !debugDraw.visible;
0199             }
0200         }
0201 
0202         DebugDraw {
0203             id: debugDraw
0204             world: physicsWorld
0205             opacity: 0.7
0206             visible: false
0207         }
0208     }
0209 
0210     Slider {
0211         id: fractionSlider
0212         x: 180
0213         y: 10
0214         width: 200
0215         height: 30
0216         minimumValue: 1
0217         maximumValue: 70
0218         value: 70
0219     }
0220 
0221     Rectangle {
0222         id: intersectionPoint
0223         width: 10
0224         height: 10
0225         radius: 5
0226         color: "red"
0227         border.color: "yellow"
0228         opacity: 0
0229         PropertyAnimation {
0230             id: pointHideAnimation
0231             target: intersectionPoint
0232             property: "opacity"
0233             to: 0
0234             duration: 200
0235         }
0236     }
0237 
0238     Timer {
0239         id: rectTimer
0240         interval: 1000
0241         running: true
0242         repeat: true
0243         onTriggered: {
0244             var newBall = ballComponent.createObject(physicsRoot);
0245             newBall.x = 100 + (Math.random() * 600);
0246             newBall.y = 50;
0247         }
0248     }
0249 }