Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Box2D Examples

 import QtQuick 2.0
 import Felgo 4.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

   activeScene: scene

   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
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded