Forums

OverviewV-Play 2 Support (Qt 5) › Distance joint length

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #10137

    Phil

    Hi. I have dynamically created objects in my game that are connected by a distance joint and I want the length of the distance joint to change over time. As an example I’ve edited the Distance.qml file from box2d-examples to show what I want to achieve. However, when the distance joint is created the length property doesn’t set accurately. Do you know why this is happening? Thanks, Phil

     

    import QtQuick 2.0
    import VPlay 2.0
    
    GameWindow {
    
      property var myJoint
    
      //...
    
      // Timer that keeps creating heavy balls that crash down on the building
      Timer {
        //...
        onTriggered: {
          //...
          myJoint = extraJoint.createObject(world,
                                              {
                                                "bodyA": ball1.body,
                                                "bodyB": ball2.body,
                                                "world": world,
                                                "collideConnected": true,
                                                "length": 400
                                              })
          togetherTimer.start()
        }
      }
    
      Timer {
          id: togetherTimer
          repeat: true
          running: false
          interval: 1000
          onTriggered: {
              console.debug("")
              console.debug(myJoint.length)
              console.debug("")
    //          if(myJoint.length > heavyBox.width/2 + lightBox.width/2){
                myJoint.length = myJoint.length - 10
    //          }
          }
      }
    
      Scene {
      //...
      }
    
      EntityManager {
      //...
      }
    
    }
    

     

    #10142

    Günther
    V-Play Team

    Hi totalzero!

    I looked into the matter. Within the DistanceJoint implementation, a certain scaleRatio is applied to the length value whenever a new length is set. If you set the length to a new value, everything works fine. But when you read the length at a later point, you get the modified length that is used internally.

    For the moment, you can fix this problem by manually calculating the “real” length when you need it:

    // ...
      property var myJoint
      property  real scaleRatio
    
    // ...
    
      // Timer that keeps creating heavy balls that crash down on the building
      Timer {
       // ...
        onTriggered: {
          // ...
    
          myJoint = extraJoint.createObject(world,
                                              {
                                                "bodyA": ball1.body,
                                                "bodyB": ball2.body,
                                                "world": world,
                                                "collideConnected": true,
                                                "length": 200
                                              })
    
    
          // myJoint.length holds the modified value, as we know that the "real" value is 200,
          // we can calculate the scaleRatio that is applied within the distance joint
          scaleRatio = 200 / myJoint.length 
    
          togetherTimer.start()
        }
      }
    
      Timer {
            id: togetherTimer
            // ...
            onTriggered: {
                console.debug("")
                var realJointLength = myJoint.length * scaleRatio
                console.debug("LENGTH: "+myJoint.length)
                console.debug("SCALE-RATIO: "+scaleRatio)
                console.debug("REAL LENGTH: "+realJointLength)
                console.debug("")
      //          if(myJoint.length > heavyBox.width/2 + lightBox.width/2){
                  myJoint.length = realJointLength - 10
      //          }
            }
        }

     

    This is just a workaround for now. I think we can fix this problem and include the correct version in the next V-Play update.
    Thanks for reporting this issue!

    Best,
    GT

     

     

     

     

    #10145

    Phil

    Thanks! That makes sense – it’s quite a simple workaround really

Viewing 3 posts - 1 through 3 (of 3 total)

RSS feed for this thread

You must be logged in to reply to this topic.

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