Rope.qml Example File

examples/physics/box2d-examples/qml/rope/Rope.qml
 import QtQuick 2.0
 import VPlay 2.0

 /*
   This example shows how to use the RopeJoint.
   The 3 bodies are connected with 2 RopeJoints, which limits the maximum distance between 2 bodies.
   By clicking on the window, an impulse to the right can be applied to move the 2 lower bodies.
  */

 GameWindow {
   id: screen

   Scene {
     id: scene

     PhysicsWorld {
       id: world
       debugDrawVisible: true
       gravity.y: 10
     }

     Ball {
       id: ball1
       x: 200
       y: 30
       radius: 15
       // make this ball static, otherwise all would fall down
       // the second ball is connected to the first, and the third to the second
       bodyType: Body.Static
     }

     Square {
       id: square2
       x: 250
       y: 30
       width: 20
       height: 20
     }

     Ball {
       id: ball3
       x: 330
       y: 30
       radius: 10
     }

     RopeJoint {
       id: ropeJoint1
       // the initial distance between ball1 & ball2 is 50, so the rope is longer than the initial distance
       maxLength: 70
       bodyA: ball1.body
       bodyB: square2.body
       //world: world

       //anchors should be in the middle of the items
       localAnchorA: Qt.point(ball1.width/2, ball1.height/2)
       localAnchorB: Qt.point(square2.width/2, square2.height/2)
     }

     RopeJoint {
       id: ropeJoint2
       // the initial distance between ball2 & ball3 is 80, so the rope is longer than the initial distance
       maxLength: 100
       bodyA: square2.body
       bodyB: ball3.body
       //world: world

       //anchors should be in the middle of the items
       localAnchorA: Qt.point(square2.width/2, square2.height/2)
       localAnchorB: Qt.point(ball3.width/2, ball3.height/2)
     }

     Text {
       z: 1 // put on top of DebugDraw in QML renderer
       color: "blue"
       text: "Click to apply an impulse to the right on the green square in the middle"
     }

   } // end of Scene

   MouseArea {
     anchors.fill: parent
     onClicked: {
       square2.body.applyLinearImpulse(Qt.point(100, 0), square2.body.getWorldCenter())
     }
   }

   EntityManager {
     id: entityManager
   }

 }

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support