DistanceJoint

The DistanceJoint keeps the distance between 2 bodies constant. More...

Import Statement: import
Inherits:

Joint

Properties

Methods

Detailed Description

The DistanceJoint connects two physics bodies with a joint that keeps the distance between these bodies constant. The anchor points are defined relative to the bodies' centers. If no length is specified, it is auto calculated by the distance between the two anchor points.

For a comprehensive documentation of all joints see the Box2D documentation at http://www.box2d.org/manual.html#_Toc258082974.

Example Usage

The following example shows how two physics bodies connected with a DistanceJoint are created every 4 seconds. The source code of the full example is provided in the distance folder of the Box2D Examples.

 import QtQuick 2.0
 import VPlay 2.0


 GameWindow {

     // 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
             anchors.fill: parent
             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
         }

         MouseArea {
             anchors.fill: parent
             onClicked: {

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

     EntityManager {
       id: entityManager
       entityContainer: scene
     }
 }

Property Documentation

dampingRatio : real

The damping ratio. 0 = no damping, 1 = critical damping. The default value is 0.


frequencyHz : real

The mass-spring-damper frequency in Hertz. The default value is 0.

Typically the frequency should be less than a half the frequency of the PhysicsWorld::timeStep. So if you are using a 60Hz time step, the frequency of the distance joint should be less than 30Hz. The reason is related to the Nyquist frequency.


length : real

The length between the 2 bodies, in pixels.

By default, it will be set to the distance between localAnchorA of Joint::bodyA and localAnchorB of Joint::bodyB.


localAnchorA : point

The local anchor point relative to the Joint::bodyA center in pixels.

The default is (0, 0), which means the center of Joint::bodyA.


localAnchorB : point

The local anchor point relative to the Joint::bodyB center in pixels.

The default is (0, 0), which means the center of Joint::bodyB.


Method Documentation

point getReactionForce(real inv_dt)

Returns the reaction force, in kg*pixels/second^2 for a timestep of length inv_dt, in seconds.

For the Parameter inv_dt, World::timeStep can be used.


real getReactionTorque(real inv_dt)

This is always 0, since a DistanceJoint only applies force and no torque.


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