Warning, /education/gcompris/external/qml-box2d/examples/contacts/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 width: 800 0007 height: 600 0008 0009 Component { 0010 id: rectComponent 0011 RectangleBoxBody { 0012 id: rect 0013 width: 20 0014 height: 20 0015 0016 world: physicsWorld 0017 bodyType: Body.Dynamic 0018 0019 property variant colors : [ 0020 "#FF0000","#FF8000","#FFFF00","#00FF00", 0021 "#0080FF","#0000FF","#FF00FF","#FFFFFF" 0022 ] 0023 property int colorIndex : 0 0024 property bool animateDeletion: false 0025 0026 property bool isBall: true 0027 density: 0.5 0028 friction: 1 0029 restitution: 0.2 0030 0031 border.color: "#999" 0032 color: colors[colorIndex] 0033 radius: 3 0034 0035 PropertyAnimation { 0036 target: rect 0037 property: "opacity" 0038 duration: 1000 0039 to: 0 0040 easing.type: Easing.InCubic 0041 running: animateDeletion 0042 onRunningChanged: { 0043 if (!running) 0044 rect.destroy(); 0045 } 0046 } 0047 } 0048 } 0049 0050 World { 0051 id: physicsWorld 0052 0053 onPreSolve: { 0054 var targetA = contact.fixtureA.getBody().target; 0055 var targetB = contact.fixtureB.getBody().target; 0056 if (targetA.isBall && contact.fixtureB === topBeltFixture) 0057 contact.tangentSpeed = -3.0; 0058 else if (targetB.isBall && contact.fixtureA === topBeltFixture) 0059 contact.tangentSpeed = 3.0; 0060 } 0061 } 0062 0063 Item { 0064 id: physicsRoot 0065 anchors.fill: parent 0066 0067 Item { 0068 id: topWall 0069 height: 10 0070 y: -10 0071 anchors.left: parent.left 0072 anchors.right: parent.right 0073 BoxBody { 0074 target: topWall 0075 world: physicsWorld 0076 width: topWall.width 0077 height: topWall.height 0078 } 0079 } 0080 0081 Wall { 0082 id: leftWall 0083 width: 40 0084 anchors { 0085 left: parent.left 0086 top: parent.top 0087 bottom: parent.bottom 0088 bottomMargin: 40 0089 } 0090 } 0091 0092 Wall { 0093 id: rightWall 0094 width: 40 0095 anchors { 0096 right: parent.right 0097 top: parent.top 0098 bottom: parent.bottom 0099 bottomMargin: 40 0100 } 0101 } 0102 0103 PhysicsItem { 0104 id: ground 0105 height: 40 0106 world: physicsWorld 0107 anchors { 0108 left: parent.left 0109 right: parent.right 0110 bottom: parent.bottom 0111 } 0112 fixtures: Box { 0113 id: groundFixture 0114 width: ground.width 0115 height: ground.height 0116 friction: 1 0117 density: 1 0118 } 0119 Rectangle { 0120 anchors.fill: parent 0121 color: "#DEDEDE" 0122 } 0123 } 0124 0125 PhysicsItem { 0126 id: drivingWheel 0127 width: 48 0128 height: 48 0129 world: physicsWorld 0130 bodyType: Body.Dynamic 0131 fixtures: Circle { 0132 radius: 24 0133 density: 0.5 0134 } 0135 Image { 0136 anchors.fill: parent 0137 source: "images/wheel.png" 0138 } 0139 } 0140 0141 PhysicsItem { 0142 id: drivenWheel 0143 width: 48 0144 height: 48 0145 world: physicsWorld 0146 bodyType: Body.Dynamic 0147 fixtures: Circle { 0148 radius: 24 0149 density: 0.5 0150 } 0151 Image { 0152 anchors.fill: parent 0153 source: "images/wheel.png" 0154 } 0155 } 0156 0157 PhysicsItem { 0158 id: topBelt 0159 x: 65 0160 y: 500 0161 width: 600 0162 height: 5 0163 world: physicsWorld 0164 fixtures: Box { 0165 id: topBeltFixture 0166 width: topBelt.width 0167 height: topBelt.height 0168 density: 0.5 0169 } 0170 Rectangle { 0171 anchors.fill: parent 0172 color: "#000" 0173 radius: 5 0174 } 0175 } 0176 Rectangle { 0177 id: bottomBelt 0178 x: 65 0179 y: 543 0180 width: 600 0181 height: 5 0182 color: "#000" 0183 radius: 5 0184 } 0185 0186 RevoluteJoint { 0187 bodyA: topBelt.body 0188 bodyB: drivingWheel.body 0189 localAnchorA: Qt.point(600,24) 0190 localAnchorB: Qt.point(24,24) 0191 collideConnected: false 0192 enableMotor: true 0193 motorSpeed: 180 0194 maxMotorTorque: 100 0195 } 0196 0197 RevoluteJoint { 0198 bodyA: topBelt.body 0199 bodyB: drivenWheel.body 0200 localAnchorA: Qt.point(0,24) 0201 localAnchorB: Qt.point(24,24) 0202 collideConnected: false 0203 enableMotor: true 0204 motorSpeed: 180 0205 maxMotorTorque: 100 0206 } 0207 PhysicsItem { 0208 id: tube 0209 x: 500 0210 y: 10 0211 width: 250 0212 height: 450 0213 world: physicsWorld 0214 fixtures: [ 0215 Chain { 0216 vertices: [ 0217 Qt.point(0,60), 0218 Qt.point(170,60), 0219 Qt.point(180,70), 0220 Qt.point(180,350), 0221 Qt.point(170,400), 0222 Qt.point(170,430) 0223 ] 0224 }, 0225 Chain { 0226 vertices: [ 0227 Qt.point(0,5), 0228 Qt.point(190,5), 0229 Qt.point(225,25), 0230 Qt.point(240,60), 0231 Qt.point(240,350), 0232 Qt.point(250,400), 0233 Qt.point(250,430) 0234 ] 0235 } 0236 ] 0237 Canvas { 0238 id: canvas 0239 anchors.fill: parent 0240 onPaint: { 0241 var context = canvas.getContext("2d"); 0242 context.beginPath(); 0243 context.moveTo(0,60); 0244 context.lineTo(170,60); 0245 context.lineTo(180,70); 0246 context.lineTo(180,350); 0247 context.lineTo(170,400); 0248 context.lineTo(170,430); 0249 context.lineTo(250,430); 0250 context.lineTo(250,400); 0251 context.lineTo(240,350); 0252 context.lineTo(240,60); 0253 context.lineTo(225,25); 0254 context.lineTo(190,5); 0255 context.lineTo(0,5); 0256 context.lineTo(0,60); 0257 context.fillStyle = "#DEDEDE"; 0258 context.lineWidth = 1; 0259 context.lineJoin = "miter"; 0260 context.fill(); 0261 context.strokeStyle = "#999"; 0262 context.stroke(); 0263 } 0264 } 0265 } 0266 0267 BoxBody { 0268 id: flowVertical 0269 x: 680 0270 y: 60 0271 width: 60 0272 height: 500 0273 world: physicsWorld 0274 sensor: true 0275 onBeginContact: { 0276 other.getBody().gravityScale = -2; 0277 } 0278 } 0279 BoxBody { 0280 id: flowHorizontal 0281 x: 500 0282 y: 10 0283 width: 240 0284 height: 60 0285 world: physicsWorld 0286 sensor: true 0287 onBeginContact: { 0288 var body = other.getBody(); 0289 body.gravityScale = 0.5; 0290 body.applyLinearImpulse(Qt.point(-5,0), Qt.point(24,24)); 0291 } 0292 onEndContact: { 0293 var body = other.getBody(); 0294 body.gravityScale = 1; 0295 body.applyForce(Qt.point(5,0), Qt.point(24,24)); 0296 var rect = body.target 0297 var index = rect.colorIndex; 0298 index ++; 0299 rect.colorIndex = index; 0300 if ((index + 1) === rect.colors.length) 0301 rect.animateDeletion = true; 0302 } 0303 } 0304 0305 Rectangle { 0306 id: debugButton 0307 x: 50 0308 y: 10 0309 width: 120 0310 height: 30 0311 Text { 0312 text: debugDraw.visible ? "Debug view: on" : "Debug view: off" 0313 anchors.centerIn: parent 0314 } 0315 color: "#DEDEDE" 0316 border.color: "#999" 0317 radius: 5 0318 MouseArea { 0319 anchors.fill: parent 0320 onClicked: debugDraw.visible = !debugDraw.visible; 0321 } 0322 } 0323 0324 Timer { 0325 id: rectTimer 0326 interval: 2000 0327 running: true 0328 repeat: true 0329 onTriggered: { 0330 var newBox = rectComponent.createObject(physicsRoot); 0331 newBox.x = 60 + (Math.random() * 300); 0332 newBox.y = 200; 0333 } 0334 } 0335 0336 DebugDraw { 0337 id: debugDraw 0338 anchors.fill: parent 0339 world: physicsWorld 0340 opacity: 0.7 0341 visible: false 0342 } 0343 } 0344 }