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
 import QtQuick.Controls
 import QtQuick.Dialogs

 /*!
   This example uses the modified Box2d plugin and examples which is part of import Felgo 4.0.
   Compared to the normal Box2d examples, it adds entity structure and content scaling with GameWindow & Scene components.
   You can use the original qml-box2d sources by "import Box2D 1.1" - this makes it easy to combine existing QML games with \vp.

   NOTE: On Android, showing multiple GameWindows does NOT work! Also, the FileDialog points to the wrong directory on Android - so for Android only choose a single example to load.
  */
 GameWindowApplicationWindow {
   color: "white"

   Loader {
     id: loader

     source: "boxes/Boxes.qml"

     asynchronous: true

     // always prevent shutdown from the loaded item - a shutdown shall only occur if the outer window has focus!
     onLoaded: item.shutdownAppWithEscapeKeyEnabled = false

     Connections {
       target: loader.item
       function onClosing() {
         switchSource("")
       }
     }
   }

   Flow {
     spacing: 10
     width: parent.width

     Button {
       text: "Select Box2D Example"

       onClicked: {
         fileDialog.open()
       }
     }
     Button {
       text: "Unload"

       onClicked: {
         switchSource("")
       }
     }
     Button {
       text: "Boxes"

       onClicked: {
         switchSource("boxes/Boxes.qml")
       }
     }
     Button {
       text: "Demolition"

       onClicked: {
         switchSource("demolition/Demolition.qml")
       }
     }
     Button {
       text: "Distance"
       onClicked: {
         switchSource("distance/Distance.qml")
       }
     }
     Button {
       text: "Filtering"
       onClicked: {
         switchSource("filtering/Filtering.qml")
       }
     }
     Button {
       text: "Impulse"
       onClicked: {
         switchSource("impulse/Impulse.qml")
       }
     }
     Button {
       text: "Monera"
       onClicked: {
         switchSource("monera/Monera.qml")
       }
     }
     Button {
       text: "MovingBox"
       onClicked: {
         switchSource("movingBox/MovingBox.qml")
       }
     }
     Button {
       text: "Polygons"
       onClicked: {
         switchSource("polygons/Polygons.qml")
       }
     }
     Button {
       text: "Prismatic"
       onClicked: {
         switchSource("prismatic/Prismatic.qml")
       }
     }
     Button {
       text: "Raycast"
       onClicked: {
         switchSource("raycast/Raycast.qml")
       }
     }
     Button {
       text: "Revolute"
       onClicked: {
         switchSource("revolute/Revolute.qml")
       }
     }
     Button {
       text: "Rope"
       onClicked: {
         switchSource("rope/Rope.qml")
       }
     }
     Button {
       text: "TwoWorlds"
       onClicked: {
         switchSource("twoWorlds/TwoWorlds.qml")
       }
     }
   }

   FileDialog {
     id: fileDialog
     title: "Please choose a qml file from a subfolder of the Box2D Examples"
     //selectedNameFilter: "Qml files (*.qml)" // doesnt work like that?
     onAccepted: {
       console.log("You chose: " + fileDialog.fileUrl)

       switchSource(fileDialog.fileUrl)
     }

     // open in the current folder, then there will be the subfolders like "boxes", "accelerometer", etc.
     currentFolder: "."
   }

   function switchSource(newSource) {
     // clear the default world, so new bodies will find the new world
     // this is necessary because the loader first loads the new item and only then destroys the old one
     Box2D.defaultWorld = null

     // new the new PhysicsWorld will automatically be set as defaultWorld
     loader.source = newSource
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded