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

 GameWindow {
   id: screen

   Image {
     anchors.fill: parent
     source: "images/background.png"
   }

   activeScene: scene

   // A wheel that will be created dynamically
   Component {
     id: wheelComponent

     EntityBase {
       id: wheel

       width: circleCollider.radius * 2
       height: width

       CircleCollider {
         id: circleCollider
         sleepingAllowed: true
         radius: 30
         density: 0.006
         friction: 1.0
         restitution: 0.6

         // torque gets set to positive or negative, depending on if clicked on the left or right side of the world when touching the wheel
       }

       Image {
         id: circleRect

         anchors.fill: parent
         smooth: true

         source: "images/wheel.png"
       }

       MouseArea {
         anchors.fill: circleRect
         onPressed: {
           console.debug("wheel.x when pressed:", wheel.x)

           // if wheel is on the left side of the world, twist it in the other direction
           var impulseStrength = 3000*32 //kg*pixels^2/second
           var impulse = impulseStrength * (wheel.x + wheel.width / 2 < world.width / 2 ? 1 : -1)
           circleCollider.body.applyAngularImpulse(impulse)
         }
       }
     }
   }

   Scene {
     id: scene

     Image {

       id: skyline
       anchors {
         bottom: parent.bottom
         left: parent.left
         right: parent.right
       }

       source: "images/skyline.png"
     }

     EntityManager {
       id: entityManager
       entityContainer: scene
     }

     PhysicsWorld {
       id: world
       width: scene.width
       height: scene.height

       gravity.x: 0
       gravity.y: 9.81

       updatesPerSecondForPhysics: 60

       debugDrawVisible: true
     }

     MouseArea {
       anchors.fill: parent
       onPressAndHold: mouse => {
         entityManager.createEntityFromComponentWithProperties(wheelComponent, {x: mouse.x, y: mouse.y})
       }
     }

     Building {
       id: victim

       anchors {
         bottom: ground.top
       }
       x: 100
       floors: 6
       stairways: 3
     }

     Building {
       anchors {
         bottom: ground.top
       }
       x: 400
       floors: 6
       stairways: 3
     }

     Wall {
       id: ceiling

       height: 20
       anchors {
         top: parent.top
         left: parent.left
         right: parent.right
       }
     }

     Wall {
       id: leftWall
       width: 20
       anchors {
         top: parent.top
         bottom: parent.bottom
         left: parent.left
       }
     }

     Wall {
       id: rightWall
       width: 20
       anchors {
         top: parent.top
         bottom: parent.bottom
         right: parent.right
       }
     }

     Wall {
       id: ground

       height: 20
       anchors {
         left: parent.left
         right: parent.right
         bottom: parent.bottom
       }

       //rotation: -1
     }

     Text {
       anchors {
         top: scene.gameWindowAnchorItem.top; topMargin: 20
         left: scene.gameWindowAnchorItem.left; leftMargin: 25
       }

       text: "Press and hold to create a wheel.\nPress and hold on top of the wheel to apply torque into the other half of the scene.\nClick on a box to apply a torque on it."
       color: "white"
       font.pixelSize: 12
     }

   } // end of Scene

   Image {
     anchors {
       top: parent.top; topMargin: 10
       right: parent.right; rightMargin: 20
     }

     fillMode: Image.PreserveAspectFit

     source: "images/plate.png"
     smooth: true

     MouseArea {
       anchors.fill: parent

       scale: 1.4

       Behavior on scale {
         PropertyAnimation { duration: 100 }
       }

       onClicked: Qt.quit()

       onPressed: parent.scale = 0.9
       onReleased: parent.scale = 1.0
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded