Forums

OverviewV-Play 1 SupportComplex Box2D Components › Re: Complex Box2D Components

OverviewV-Play 1 SupportComplex Box2D Components › Re: Complex Box2D Components
#4147

Elisabeth

Hi Christian,

find enclosed my test case. There are two fixed bodies with each having a rope approximation (two BoxColliders connected via RevoluteJoints) attached. On the left side, everything is OK, but the code is contained in the top level qml file. On the right side you’ll see my naive attempt to separate the code – looks like stiff paranomal multiplexing (but: only one element is OK)…

main.qml:


import QtQuick 1.1
import VPlay 1.0
import Box2D 1.0

GameWindow {
  id: window
  width: 960
  height: 640

  EntityManager {
    id: entityManager
    entityContainer: scene
  }
  
  Scene {
    id: scene
    width: 480
    height: 320
    sceneAlignmentY: "bottom"

    PhysicsWorld {
      id: physicsWorld
      z: 1
      height: parent.height
      updatesPerSecondForPhysics: 60
      gravity.y: -0.81
      velocityIterations: 5
      positionIterations: 5
    }

    /////////////////////////////////////////////////////////////
    // Without Rope Component
    /////////////////////////////////////////////////////////////
    EntityBase {
      id: fixedBody0
      x: physicsWorld.width / 3
      y: physicsWorld.height / 3
      
      property alias body: fixedBodyCollider0.body

      BoxCollider {
        id: fixedBodyCollider0
        anchors.centerIn: parent
        width: fixedBodyRect0.width
        height: fixedBodyRect0.height      
        bodyType: Body.Static
      }
     
      Rectangle {
        id: fixedBodyRect0
        width: 5
        height: 5
        color: "grey"
        anchors.centerIn: parent
      }
    }

    EntityBase {
      id: rod0
      x: physicsWorld.width / 3 + fixedBodyRect0.width / 2
      y: physicsWorld.height / 3 - fixedBodyRect0.height / 2
      width: 25
      height: 5
      transformOrigin: Item.TopLeft

      property alias body: rodCollider0.body

      BoxCollider {
        id: rodCollider0
        sleepingAllowed: false
        density: 1
        angularDamping: 1
      }
     
      Rectangle {
        id: rodRect0
        anchors.fill: parent
        color: "green"
      }
    }

    EntityBase {
      id: rod1
      x: physicsWorld.width / 3 + fixedBodyRect0.width / 2 + rod0.width 
      y: physicsWorld.height / 3 - fixedBodyRect0.height / 2
      width: rod0.width
      height: rod0.height
      transformOrigin: Item.TopLeft

      property alias body: rodCollider1.body

      BoxCollider {
        id: rodCollider1
        sleepingAllowed: false
        density: 1
        angularDamping: 1
      }
     
      Rectangle {
        id: rodRect1
        anchors.fill: parent
        color: "red"
      }
    }

    RevoluteJoint {
      id: revolute0
      bodyA: fixedBodyCollider0.body
      bodyB: rodCollider0.body
      world: physicsWorld
    }

    RevoluteJoint {
      id: revolute1
      bodyA: rodCollider0.body
      bodyB: rodCollider1.body
      world: physicsWorld
      localAnchorA: Qt.point(rodCollider0.width / 2, 0)
    }


    /////////////////////////////////////////////////////////////
    // With Rope Component
    /////////////////////////////////////////////////////////////
    Component.onCompleted: {
      entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("Rope.qml"), {"anchorBody": fixedBody1.body, "x": physicsWorld.width / 3 * 2, "y": physicsWorld.height / 3});
    }
    
    EntityBase {
      id: fixedBody1
      x: physicsWorld.width / 3 * 2
      y: physicsWorld.height / 3
      
      property alias body: fixedBodyCollider1.body

      BoxCollider {
        id: fixedBodyCollider1
        anchors.centerIn: parent
        width: fixedBodyRect1.width
        height: fixedBodyRect1.height      
        bodyType: Body.Static
      }
     
      Rectangle {
        id: fixedBodyRect1
        width: 5
        height: 5
        color: "grey"
        anchors.centerIn: parent
      }
    }

  } // scene

}

Rope.qml:


import QtQuick 1.1
import VPlay 1.0
import Box2D 1.0

EntityBase {
  id: rope

  property variant anchorBody: null
  property double elementWidth: 25
  property double elementHeight: 5

  BoxCollider {
    id: rodCollider0
    width: elementWidth
    height: elementHeight
    x: 0
    y: -elementHeight/2
    sleepingAllowed: false
    density: 1
    angularDamping: 1

    Rectangle {
      anchors.fill: parent
      color: "green"
    }
  }
 
  BoxCollider {
    id: rodCollider1
    width: elementWidth
    height: elementHeight
    x: elementWidth
    y: -elementHeight/2
    sleepingAllowed: false
    density: 1
    angularDamping: 1

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

  RevoluteJoint {
    id: joint0
    bodyA: anchorBody
    bodyB: rodCollider0.body
    world: physicsWorld
  }

  RevoluteJoint {
    id: joint1
    bodyA: rodCollider0.body
    bodyB: rodCollider1.body
    world: physicsWorld
    localAnchorA: Qt.point(rodCollider0.width / 2, 0)
  }

}

As I don’t feel good with the experiments so far, I would be grateful for any hint pointing in the right direction.

Cheers,
Thomas

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