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 how to use the RevoluteJoint.
   By pressing the left and right keys, the RevoluteJoint motorSpeed gets modified.
   By clicking on the window, the motor is enabled and disabled.
  */

 GameWindow {
   id: screen

   activeScene: scene

   onKeyPressedGlobally: event => onKeysPressed(event)
   //Keys.onPressed: onKeysPressed(event)

   function onKeysPressed(event) {
     if (event.key === Qt.Key_Left) {
       revolute.motorSpeed -= 5
       console.debug("decreasing motor speed to", revolute.motorSpeed)
     }
     else if (event.key === Qt.Key_Right) {
       revolute.motorSpeed += 5
       console.debug("increasing motor speed to", revolute.motorSpeed)
     }
   }

   Scene {
     id: scene
     PhysicsWorld {
       id: world
       debugDrawVisible: true
     }

     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 }
     }

     Square {
       id: rod

       x: world.width/2+middleBall.radius+15
       y: world.height/2

       width: 80
       height: 20
     }

     Ball {
       id: middleBall

       x: world.width/2
       y: world.height/2
       radius: 20
     }

     RevoluteJoint {
       id: revolute
       maxMotorTorque: 300 * 32 * 32
       // initially, the speed is 0 - it gets modified by pressing left and right keys
       motorSpeed: 0
       bodyA: middleBall.body
       bodyB: rod.body
      // world: world
       enableMotor: true // start with enabled motor, to toggle click on the window

       // the center of the joint will be at the center of the middleBall
       // to change that, you could use the following:
       localAnchorA: Qt.point(middleBall.radius, middleBall.radius)
       localAnchorB: Qt.point(rod.width / 2 - 60, rod.height / 2)
       // NOTE: localAnchorB is not supported, as it makes no sense for a RevolutJoint, because only the center of the RevoluteJoint should be movable

     }

     Text {
       z: 1 // put on top of DebugDraw in QML renderer
       color: "white"
       text: "Press left and right keys to change the RevoluteJoint motorSpeed"
     }

   } // end of Scene

   EntityManager {
     id: entityManager
   }

   MouseArea {
     anchors.fill: parent
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded