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: gameWindow
   screenWidth: 960
   screenHeight: 640

   activeScene: scene

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

   Scene {
     id: scene
     width: 480
     height: 320

     PhysicsWorld {
       id: world
       debugDrawVisible: true
       gravity.y: 9.81
     } // end of PhysicsWorld

     Repeater {
       model: 4

       WoodenBox {
         x: Math.random() * (scene.width - 100) + 20
         y: Math.random() * (scene.height / 3) + 20
         rotation: Math.random() * 90
       }
     }

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

     //show the ray that is being cast as a rectangle
     Rectangle {
       property real mx: mouseArea.mouseX - scene.width / 2
       property real my: mouseArea.mouseY - scene.height / 2

       property real len: 2 * Math.sqrt(mx * mx + my * my)

       rotation: 180 / Math.PI * Math.atan2(my, mx)

       transformOrigin: Item.Center

       x: scene.width / 2 - len / 2
       y: scene.height / 2
       width: len
       height: 2
       color: "red"
     }

     //highlight the object which is hit by the ray
     Rectangle {
       id: rect
       property Item target: null
       width: target ? target.width : 0
       height: target ? target.height : 0
       x: target ? target.x : 0
       y: target ? target.y : 0
       color: "green"
       opacity: 0.5
       rotation: target ? target.rotation : 0
       visible: !!target
       transformOrigin: Item.TopLeft
     }

     RayCast {
       id: raycast
       maxFraction: 2 //cast ray twice the distance from start to end point
       onFixtureReported: {
         //fixture.getBody() returns the body object, body.target returns the entity which contains the body
         var body = fixture.getBody()
         var entity = body.target

         rect.target = entity
         maxFraction = 0 // cancel current raycast, report no more objects
       }
     }

     MouseArea {
       id: mouseArea
       anchors.fill: parent
       hoverEnabled: true

       onClicked: mouse => {
         rect.target = null

         raycast.maxFraction = 2

         //cast a ray from the mouse position through the center of the scene
         var from = Qt.point(mouse.x, mouse.y)
         var to = Qt.point(scene.width / 2, scene.height / 2)
         console.log("raycast", from, to)
         world.rayCast(raycast, from, to)
       }
     }
   } // end of Scene

   EntityManager {
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded