Revolute.qml Example File

examples/physics/box2d-examples/qml/revolute/Revolute.qml
 import QtQuick 2.0
 import Felgo 3.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: 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
     onClicked: revolute.enableMotor = !revolute.enableMotor
   }
 }

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support