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 DistanceJoint. It can be used to fix the distance between 2 bodies when they are moving.
  */

 GameWindow {

   activeScene: scene

   // A heavy ball that will be created dynamically with the timer below
   Component {
     id: heavyBox
     EntityBase {

       property alias body: collider.body

       transformOrigin: Item.TopLeft

       width: 40
       height: 40

       BoxCollider {
         id: collider
         density: 0.005
         friction: 0.3
         restitution: 0.2
       }

       Rectangle {
         anchors.fill: parent
         smooth: true
         color: "brown"
       }
     }
   }

   Component {
     id: lightBox
     EntityBase {

       property alias body: collider.body

       transformOrigin: Item.TopLeft

       width: 40
       height: 40

       BoxCollider {
         id: collider
         density: 0.002
         friction: 0.3
         restitution: 0.2
       }

       Rectangle {
         anchors.fill: parent
         color: "red"
       }
     }
   }

   Component {
     id: extraJoint
     DistanceJoint {
       frequencyHz: 20
       dampingRatio: 0.5
       collideConnected: false
     }
   }

   // Timer that keeps creating heavy balls that crash down on the building
   Timer {
     running: true
     repeat: false
     interval: 2000
     onTriggered: {

       // TODO: the body pos is not set to the entity pos initially, that is why the connecting is wrong when done dynamically!

       var ball1Id = entityManager.createEntityFromComponentWithProperties(heavyBox,
                                                                           { x: 400, y: 50})
       var ball1 = entityManager.getEntityById(ball1Id)

       var ball2Id = entityManager.createEntityFromComponentWithProperties(lightBox,
                                                                           {x: 150, y: 50})
       var ball2 = entityManager.getEntityById(ball2Id)

       var joint = extraJoint.createObject(world,
                                           {
                                             "bodyA": ball1.body,
                                             "bodyB": ball2.body,
                                          //   "world": world,
                                             "collideConnected": true
                                           })
     }
   }

   Scene {
     id: scene

     PhysicsWorld {
       id: world
       gravity.y: 9.81
     }

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

     Ball {
       id: ball
       x: 100
       y: 100
       rotation: 0
       radius: 20
     }

     Square {
       id: square
       x: 220
       y: 180
       rotation: 0
       width: 40
       height: 40
     }

     DistanceJoint {
       id: joint
       frequencyHz: 15
       dampingRatio: 0.5
       collideConnected: true
       bodyA: ball.body
       bodyB: square.body
    //   world: world

       // for testing a modification of the localAnchor - it starts at the center of the body
       localAnchorA: Qt.point(20, 0) // would move the anchor to the right side of the ball in the center
       localAnchorB: Qt.point(20, 40) // would move the anchor outside of the Rectangle
     }

     MouseArea {
       anchors.fill: parent
       onClicked: {

         // "pull" the box towards the clicked position
         ball.body.applyLinearImpulse(
               Qt.point(10 * (mouseX - ball.x),
                        10 * (mouseY - ball.y)),
               ball.body.getWorldCenter())
       }
     }
   } // end of Scene

   EntityManager {
     id: entityManager
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded