Forums

OverviewV-Play 1 SupportElastic tape stretch and release › Reply To: Elastic tape stretch and release

OverviewV-Play 1 SupportElastic tape stretch and release › Reply To: Elastic tape stretch and release
#5245

Alex
V-Play Team

And i guess you don’t only need a rubber whip animation, like you can make with a SpriteSequenceFromFile?

If you need the whole whip to have correct physical detection along the whole length, you can try connecting many small entities with RopeJoints in combination with other Joints. Have a look at http://v-play.net/doc/vplay-group.html#physics-components

In the following example i created a very raw version of an elastic rope (just the physics part, with just a few entities, and not very elegant 😉 )

Part.qml

import QtQuick 1.1
import VPlay 1.0
import Box2D 1.0

EntityBase {
  id: entity
  entityType: "part"

  property alias color: rectangle.color
  property alias bodyType: collider.bodyType
  property alias body: collider.body
  property alias partWidth: rectangle.width
  property alias partHeight: rectangle.height

  Rectangle {
    id: rectangle
    width: 5
    height: 5
    color: "blue"
  }

  BoxCollider {
    id: collider
    anchors.fill: rectangle
    friction: 0.2
    restitution: 0.2
    density: 5
    sleepingAllowed: false
    fixedRotation: true
  }
}

main.qml

import QtQuick 1.1
import VPlay 1.0
import Box2D 1.0

GameWindow {
  id: screen

  EntityManager {
    id: entityManager
  }

  Scene {
    id: scene
    PhysicsWorld {
      id: world
      gravity.y: -9.81
    }

    Part {id: part1; x: 200; y: 100
      color: "red"
      bodyType: Body.Static
    }
    Part {id: part2; x: 215; y: 100}
    Part {id: part3; x: 230; y: 100}
    Part {id: part4; x: 245; y: 100}
    Part {id: part5; x: 260; y: 100}
    Part {id: part6; x: 275; y: 100}
    Part {id: part7; x: 290; y: 100}
    Part {id: part8; x: 305; y: 100
      color: "red"
      partWidth: 20
      partHeight: 20
    }

    // connect each part to the following
    RopeJoint {
      id: ropeJoint1
      maxLength: 15
      bodyA: part1.body
      bodyB: part2.body
      world: world
      localAnchorA: Qt.point(part1.width/2, part1.height/2)
      localAnchorB: Qt.point(part2.width/2, part2.height/2)
    }
    RopeJoint {
      id: ropeJoint2
      maxLength: 15
      bodyA: part2.body
      bodyB: part3.body
      world: world
      localAnchorA: Qt.point(part2.width/2, part2.height/2)
      localAnchorB: Qt.point(part3.width/2, part3.height/2)
    }
    RopeJoint {
      id: ropeJoint3
      maxLength: 15
      bodyA: part3.body
      bodyB: part4.body
      world: world
      localAnchorA: Qt.point(part3.width/2, part3.height/2)
      localAnchorB: Qt.point(part4.width/2, part4.height/2)
    }
    RopeJoint {
      id: ropeJoint4
      maxLength: 15
      bodyA: part4.body
      bodyB: part5.body
      world: world
      localAnchorA: Qt.point(part4.width/2, part4.height/2)
      localAnchorB: Qt.point(part5.width/2, part5.height/2)
    }
    RopeJoint {
      id: ropeJoint5
      maxLength: 15
      bodyA: part5.body
      bodyB: part6.body
      world: world
      localAnchorA: Qt.point(part5.width/2, part5.height/2)
      localAnchorB: Qt.point(part6.width/2, part6.height/2)
    }
    RopeJoint {
      id: ropeJoint6
      maxLength: 15
      bodyA: part6.body
      bodyB: part7.body
      world: world
      localAnchorA: Qt.point(part6.width/2, part6.height/2)
      localAnchorB: Qt.point(part7.width/2, part7.height/2)
    }
    RopeJoint {
      id: ropeJoint7
      maxLength: 15
      bodyA: part7.body
      bodyB: part8.body
      world: world
      localAnchorA: Qt.point(part7.width/2, part7.height/2)
      localAnchorB: Qt.point(part8.width/2, part8.height/2)
    }

    // maximum length of whole rope, by making a RopeJoint between the first and the last Part
    RopeJoint {
      id: ropeJoint8
      maxLength: 200 //maximum lenght of streched rope will be 200 now
      bodyA: part1.body
      bodyB: part8.body
      world: world
      localAnchorA: Qt.point(part1.width/2, part1.height/2)
      localAnchorB: Qt.point(part2.width/2, part2.height/2)
    }

    Component {
      id: mouseJoint
      MouseJoint {
        maxForce: 5000
        dampingRatio: 1
        frequencyHz: 2
      }
    }

    MouseArea {
      anchors.fill: parent

      property Body selectedBody: null
      property MouseJoint mouseJointWhileDragging: null

      onPressed: {

        selectedBody = physicsWorld.bodyAt(Qt.point(mouseX, mouseY));
        console.debug("selected body at position", mouseX, mouseY, ":", selectedBody);
        // if the user selected a body, this if-check is true
        if(selectedBody) {
          // create a new mouseJoint
          mouseJointWhileDragging = mouseJoint.createObject(physicsWorld)

          // set the target position to the current touch position (initial position)
          mouseJointWhileDragging.targetPoint = Qt.point(mouseX, mouseY)

          // connect the joint with the body
          mouseJointWhileDragging.movingBody = selectedBody

          // set the physicsWorld where the joint should be created
          mouseJointWhileDragging.world = world
        }
      }

      onPositionChanged: {
        // this check is necessary, because the user might also drag when no initial body was selected
        if (mouseJointWhileDragging)
          mouseJointWhileDragging.targetPoint = Qt.point(mouseX, mouseY)
      }
      onReleased: {
        // if the user releases the body, remove the created MouseJoint
        if(selectedBody) {
          selectedBody = null
          if (mouseJointWhileDragging)
            mouseJointWhileDragging.destroy()
        }
      }
    }
  } // end of Scene
}

I hope this helps you. Of course achieving a realistic looking rubber whip with physically correct behavior and collision detection will be tricky.

Let me know if you have further questions!

Cheers, Alex

 

 

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