Warning, /education/gcompris/external/qml-box2d/examples/friction/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     color: "#EFEFFF"
0011 
0012     Component {
0013         id: frictionJoint
0014         FrictionJoint {
0015             bodyA: anchor.body
0016             maxForce: 0.5
0017             maxTorque: 0.5
0018             localAnchorA: Qt.point(10, 10)
0019             localAnchorB: Qt.point(10, 10)
0020         }
0021     }
0022 
0023     Component {
0024         id: boxComponent
0025         RectangleBoxBody {
0026             id: box
0027 
0028             width: 20
0029             height: 20
0030             border.color: "blue"
0031             color: "#EFEFEF"
0032             smooth: true
0033             antialiasing: true
0034 
0035             world: physicsWorld
0036             bodyType: Body.Dynamic
0037 
0038             density: 0.1
0039             friction: 1
0040             restitution: 0.5
0041         }
0042     }
0043 
0044     World {
0045         id: physicsWorld
0046         gravity: Qt.point(0.0, 0.0);
0047     }
0048 
0049     Wall {
0050         id: topWall
0051         height: 40
0052         anchors {
0053             left: parent.left
0054             right: parent.right
0055             top: parent.top
0056         }
0057     }
0058 
0059     Wall {
0060         id: leftWall
0061         width: 40
0062         anchors {
0063             left: parent.left
0064             top: parent.top
0065             bottom: parent.bottom
0066             bottomMargin: 40
0067         }
0068     }
0069 
0070     Wall {
0071         id: rightWall
0072         width: 40
0073         anchors {
0074             right: parent.right
0075             top: parent.top
0076             bottom: parent.bottom
0077             bottomMargin: 40
0078         }
0079     }
0080     Wall {
0081         id: bottomWall
0082         height: 40
0083         anchors {
0084             right: parent.right
0085             left: parent.left
0086             bottom: parent.bottom
0087         }
0088     }
0089 
0090     RectangleBoxBody {
0091         id: anchor
0092         anchors.centerIn: parent
0093         width: 20
0094         height: 20
0095         color: "green"
0096         world: physicsWorld
0097     }
0098 
0099     Rectangle {
0100         id: debugButton
0101         x: 50
0102         y: 50
0103         width: 120
0104         height: 30
0105         Text {
0106             text: debugDraw.visible ? "Debug view: on" : "Debug view: off";
0107             anchors.centerIn: parent
0108         }
0109         color: "#DEDEDE"
0110         border.color: "#999"
0111         radius: 5
0112         MouseArea {
0113             anchors.fill: parent
0114             onClicked: debugDraw.visible = !debugDraw.visible;
0115         }
0116     }
0117 
0118     DebugDraw {
0119         id: debugDraw
0120         anchors.fill: parent
0121         world: physicsWorld
0122         opacity: 1
0123         visible: false
0124     }
0125 
0126     Timer {
0127         id: ballsTimer
0128         interval: 500
0129         running: true
0130         repeat: true
0131         onTriggered: {
0132             var newBox = boxComponent.createObject(screen);
0133             newBox.x = screen.width / 2 - 10;
0134             newBox.y = screen.height / 2 - 10;
0135             var x = ((Math.random() * 800) - 400) / 200;
0136             var y = ((Math.random() * 600) - 300) / 200;
0137             if (Math.round(Math.random() * 5) == 1) {
0138                 newBox.border.color = "red";
0139             } else {
0140                 var joint = frictionJoint.createObject(screen);
0141                 joint.bodyB = newBox.body;
0142             }
0143             newBox.body.applyLinearImpulse(Qt.point(x,y), Qt.point(10,10));
0144         }
0145     }
0146 }