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 the usage of a Prismatic Joint. By clicking on the window, the motor speed is inverted and the movingSquare is pulled towards the other direction.
  */

 GameWindow {
   id: gameWindow
   activeScene: scene

   // Timer that keeps creating heavy balls that crash down on the building
   Timer {
     running: true
     repeat: true
     interval: 1000
     onTriggered: {
       var ballId = entityManager.createEntityFromUrlWithProperties(
             Qt.resolvedUrl("Ball.qml"),
             {
               // the radius of the ball is 20, so its width is 40 - generate numbers between 20 and world.width-20
               x: Math.random() * (world.width-40) + 20,
               y: -100
             })
     }
   }

   MouseArea {
     anchors.fill: parent
     onClicked: {

       if(prismaticJoint.motorSpeed === 0)
         prismaticJoint.motorSpeed = 320 //degrees / second

       // invert direction where movingSquare is pulled to
       prismaticJoint.motorSpeed *= -1
     }
   }

   Scene {
     id: scene

     PhysicsWorld {
       id: world
       gravity.y: 20
       updatesPerSecondForPhysics: 60
     }

     // this square is moved right and left with a motor speed
     Square {
       id: movingSquare
       x: 50
       y: 80
       width: 60
       height: 40
     }

     // this square stays in the center
     Square {
       id: staticMiddleSquare
       // the world size is 480x320
       x: 240
       y: 160
       width: 80
       height: 20
       color: "red"
       bodyType: Body.Static
     }

     PrismaticJoint {
       id: prismaticJoint
       lowerTranslation: -30
       upperTranslation: 120
       enableLimit: true
       maxMotorForce: 3000
       motorSpeed: 0 // do not set the motor speed initially, otherwise the initial joint connection is not seen in the debug draw
       enableMotor: true

       bodyA: staticMiddleSquare.body
       bodyB: movingSquare.body

       // the center of the joint will be at the top left corner of middle Square boxCollider by default
       // to change that, you could use the following:
       localAnchorA: Qt.point(staticMiddleSquare.width/2, staticMiddleSquare.height/2)
       // NOTE: localAnchorB is not supported, as it makes no sense for a PrismaticJoint, because only the center of the PrismaticJoint should be movable

       // only allow movement along the horizontal axis
       localAxisA: Qt.point(1, 0)
     //  world: world
     }

     Wall {
       id: ground
       height: 20
       anchors { left: parent.left; right: parent.right; top: parent.bottom }
     }

     Wall {
       id: leftWall
       width: 20
       anchors { right: parent.left; bottom: ground.top; top: parent.top }
     }
     Wall {
       id: rightWall
       width: 20
       anchors { left: parent.right; bottom: ground.top; top: parent.top }
     }

     Text {
       z: 1 // put on top of DebugDraw in QML renderer
       color: "white"
       text: "Click on the screen to toggle the direction of the PrismaticJoint motor speed"
     }
   } // end of Scene

   EntityManager {
     id: entityManager
     entityContainer: scene
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded