Forums

OverviewV-Play 1 SupportEntityBaseDraggable with embedded ParallaxItem › Reply To: EntityBaseDraggable with embedded ParallaxItem

OverviewV-Play 1 SupportEntityBaseDraggable with embedded ParallaxItem › Reply To: EntityBaseDraggable with embedded ParallaxItem
#5250

Alex
V-Play Team

You’re welcome anytime Martin.

That’s because the PhysicsWorld is a child of the non moving Scene. That was obviously my fault, I didn’t watch out where i put it since we just needed it that the colliders of the EntityBaseDraggable work.

Try the following setup for your main.qml:

import VPlay 1.0
import QtQuick 1.1

GameWindow {
    activeScene: scene
    width: 960; height: 640
    focus: true
    Keys.forwardTo: controller

    EntityManager{
        id: entityManager
        entityContainer: levelgrid
        dynamicCreationEntityList: [Qt.resolvedUrl("Block.qml")]
    }

    TwoAxisController {
        id: controller
        inputActionsToKeyCode: {
            "up": Qt.Key_W,
                    "down": Qt.Key_S,
                    "left": Qt.Key_D,
                    "right": Qt.Key_A,
                    "fire": Qt.Key_Space
        }
    }

    MovementAnimation{
        target: levelContainer
        property: "pos"
        running: true
        property real scale: 200
        velocity: Qt.point(controller.xAxis*scale,controller.yAxis*scale)
    }

    Scene {
        id: scene
        width: 480
        height: 320
        state: "levelEditing"

        LevelEditor {
            id: levelEditor
            toRemoveEntityTypes: ["obstacle"]
            toStoreEntityTypes: ["obstacle"]
        }

        // === LEVEL ===
        Item{
            id: levelContainer
            // === BACKGROUND ===
            ParallaxItem{
                id: background
                ratio: Qt.point(0.6,0.6)
                z:1

                Grid{
                    rows: 10; columns: 10; spacing: 50
                    Repeater{
                        model: 100
                        Rectangle{
                            width: 100; height: 100
                            color: "grey"
                        }
                    }
                }
            }

            // === GAME LAYER ===
            Item{
                id: levelgrid
                z:2
                width: 2000
                height: 2000
                PhysicsWorld {
                  anchors.fill: parent
                  id: world
                  z:10
                }

                Grid{
                    rows: 10; columns: 10; spacing: 75
                    Repeater{
                        model: 100
                        Rectangle{
                            x: 75; y: 75
                            width: 75; height: 75
                            color: "red"
                        }
                    }
                }

                EntityBase {
                  id: entity
                  entityType: "ball"
                  x: 100
                  y: 100

                  Rectangle {
                    id: rectangle
                    width: 30
                    height: 30
                    color: "yellow"
                  }

                  BoxCollider {
                    id: collider
                    anchors.fill: rectangle
                  }
                }

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

                MouseArea {
                  anchors.fill: parent

                  property Body selectedBody: null
                  property MouseJoint mouseJointWhileDragging: null

                  onPressed: {

                    selectedBody = world.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(world)

                      // 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 pressed a body initially, remove the created MouseJoint
                    if(selectedBody) {
                      selectedBody = null
                      if (mouseJointWhileDragging)
                        mouseJointWhileDragging.destroy()
                    }
                  }
                }
            }

            // === FOREGROUND ===
            ParallaxItem{
                id: foreground
                ratio: Qt.point(1.3,1.3)
                z:3

                Grid{
                    rows: 10; columns: 10; spacing: 100
                    Repeater{
                        model: 100
                        Rectangle{
                            x: 100; y: 100
                            width: 50; height: 50
                            color: "Blue"
                        }
                    }
                }
            }
        }

        BEButton{ }
    }
}

I also added a “ball” for you to check that the physics definitely work and all colliders are at the right spot.

Cheers

 

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