Forums

OverviewV-Play 1 Support › Item Movement

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #6443

    kitKat

    Hey everybody!

     

    Is it possible (or is there a function) to check whether an entity is selected and – according to that  – set the bodyType to “static” so that it can’t be moved by the other items?

    We would also need a hint on how to move an entity just a fix pixel-length on the onPressed() event in the mouse area!

     

    Yours,

    KitKat

    #6446

    Christian
    V-Play Team

    Hi,

    you cannot change bodyTypes after the entity was created, but you can have a look e.g. at the ColliderBase::gravityScale property if you want to avoid gravity to be applied to the dynamic body.

    For moving an entity, you can just change its x or y position (if collisionTestingOnlyMode is set to true), or apply forces or impulses to it (if collisionTestingOnlyMode is set to false, i.e. for physics-based movement).

    Cheers, Chris

    #6448

    kitKat

    Hi!

     

    The problem is, that the Boxcollider itself is moved properly, the image not – although we have the Image-anchors.fill:boxCollider.

    #6452

    kitKat

    Here’s the whole project – Andreas Jakl wasn’t able to find the error.

    It seems that the boxcolliders of the serveral entities (from the same type) depend on each other – although they shouldn’t because they are all unique entities???

     

    Yours KitKat

    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    import VPlay 1.0
    
    EntityBase {
        entityType: "ball"
       // signal collidedWithBall
        Component.onCompleted: console.debug("Ball.onCompleted()")
        Component.onDestruction: console.debug("Ball.onDestruction()")
    
        width: 32
        height: 32
        Image {
            source: "../img/ball.png"
            //anchors.fill: boxCollider
            x: boxCollider.x
            y: boxCollider.y
            width: 32
            height: 32
        }
        BoxCollider {
            id:boxCollider
    
            x: -width/2
            y: -height/2
    
            Rectangle
            {
                anchors.fill:parent
                color: "blue"
            }
    
            fixture.onBeginContact: {
                var fixture = other;
                var body = fixture.parent;
                var component = body.parent;
                var collidedEntity = component.owningEntity;
                var collidedEntityType = collidedEntity.entityType;
                if (collidedEntityType === 'ball') {
                    removeEntity();
                }
                if (collidedEntityType==='block1' || collidedEntityType==='block2'
                        ||collidedEntityType==='block3' ||collidedEntityType==='block4'
                        ||collidedEntityType==='block5' || collidedEntityType==='ente'
                        ||collidedEntityType==='auto' ||collidedEntityType==='turm' || collidedEntityType==='teddy') {
                        //TODO stehenbleiben!
                }
            }
        }
    }
    
    
    
    
    
    
    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import VPlay 1.0
    import QtQuick 1.1
    import "./entities"
    import "./levels"
    
    SceneBase{
        id:gameScene
    
        //property alias level: level1
        //property alias entityContainer: level1
    
        width: 480
        height: 320
    
    
        property string activeLevelStrg
        property variant activeLevel
    
    
        PhysicsWorld {
            id:physicsWorld
            gravity.y: -9.81
            z: 10 // draw the debugDraw on top of the entities
    
            // these are performance settings to avoid boxes colliding too far together
            // set them as low as possible so it still looks good
            updatesPerSecondForPhysics: 30
            //        velocityIterations: 2
            //        positionIterations: 5
        }
    
        function enterScene() {
            //level1.startGame();
            //loadLevel();
        }
    
    
        SimpleButton{
            text:"Back to menu"
            anchors.right: parent.right
            onClicked:backPressed()
        }
    
        onBackPressed: {
            // it is important to call stopGame() here, because otherwise the entities would not be deleted!
            //level1.stopGame();
            window.state = "main"
        }
    
        // load levels at runtime
        Loader {
            id: loader
            source: "levels/" + gameScene.activeLevelStrg + ".qml"
            onLoaded: {
                // do not forget to call loadItemWithCocos or the item might not be displayed correct!
                loadItemWithCocos(item)
                activeLevel = item
                item.width = gameScene.width
                item.height = gameScene.height
            }
        }
    
        // set the name of the current level, this will cause the Loader to load the corresponding level
        function setLevel(level) {
            activeLevelStrg = level
        }
    
        EntityBase {
            entityId: "ground1"
            entityType: "ground"
            height: 0
    
            preventFromRemovalFromEntityManager: true
    
            anchors {
                bottom: gameScene.bottom
                left: gameScene.left
                right: gameScene.right
            }
    
            Rectangle {
                anchors.fill: parent
                color: "blue"
            }
    
            BoxCollider {
                anchors.fill: parent
                bodyType: Body.Static // the body shouldnt move
            }
        }
    
        EntityBase {
            // left wall
            entityId: leftWall
            entityType:"leftWall"
            width: 0
            height: gameScene.height
    
            preventFromRemovalFromEntityManager: true
            anchors {
                left: gameScene.left
            }
            Rectangle {
    
                color: "blue"
                anchors.fill: parent
            }
            BoxCollider {
                anchors.fill: parent
                bodyType: Body.Static // the body shouldnt move
    
            }
        }
    
        EntityBase {
            // left wall
            entityId: rightWall
            entityType:"rightWall"
            width: 2
            height: gameScene.height
            preventFromRemovalFromEntityManager: true
            anchors {
                right: gameScene.right
                //rightMargin: gameScene.right+288
            }
            Rectangle {
    
                color: "blue"
                anchors.fill: parent
            }
            BoxCollider {
                anchors.fill: parent
                bodyType: Body.Static // the body shouldnt move
    
                // fixture.onBeginContact: collidedWithBox()
            }
        }
    
        MouseArea {
            width:320
            height:320
    
            property Body selectedBody: null
    
            property int lastY;
            property int lastX;
            property bool newMove : true;
    
    
            onPressed: {
                selectedBody = physicsWorld.bodyAt(Qt.point(mouseX, mouseY)); //nur der Collider!!!
                console.debug("selected body at position", mouseX, mouseY, ":", selectedBody);
                // if the user selected a body, this if-check is true
    
                if(selectedBody) {
    
                    console.debug("in ONPRESSED - Selected Body: ", selectedBody);
    
                    lastY = mouseY;
                    lastX = mouseX;
                    newMove=true;
    
                }
            }
    
            onPositionChanged: {
    
                //TODO wenn true und nicht bewegt wird!
    
                if (newMove===true) {
                    if (mouseX < lastX) {
                        // mouseJointWhileDragging.targetPoint = Qt.point(lastX-32, mouseY)
                        selectedBody.x = selectedBody.x-32;
    
                    } else {
                        //mouseJointWhileDragging.targetPoint = Qt.point(lastX+32, mouseY)
                        selectedBody.x = selectedBody.x+32;
                    }
                    newMove = false;
                }
            }
        }
    
        Component {
            id: mouseJoint
    
            MouseJoint {
                // make this high enough so the box with its density is moved quickly
                maxForce: 30000
                // The damping ratio. 0 = no damping, 1 = critical damping. Default is 0.7
                dampingRatio: 1
                // The response speed, default is 5
                frequencyHz: 2
            }
        }
    }
    
    
    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import VPlay 1.0
    import QtQuick 1.1
    import "../entities"
    
    Item{
    
        // the size of the Window can be changed at runtime by pressing the number keys 1-7
        // the content of the logical scene size (480x320 by default) gets scaled to the window size based on the scaleMode
        // you can set this size to any resolution you would like your project to start with, most of the times the one of your main target device
        // this resolution is for iPhone 4 & iPhone 4S
        // change this to a portrait resolution (e.g. 640x960) for games in portrait mode
    
        id:level1
    
        Block1 {
            x:32
            y:256
        }
    
        Block4 {
            x:96
            y:128
        }
    
        Block3 {
            x:128
            y:96
        }
    
        Block3 {
            x:128
            y:288
        }
    
        Block1 {
            x:320
            y:256
        }
    
        Ball {
            x:32
            y:224
        }
    
        Ball {
            x:64
            y:288
        }
    
        Ente {
            x:96
            y:98
        }
    
        Ente {
            x: 64
            y: 224
        }
    
        Auto {
            x:128
            y:256
        }
    
        Auto {
            x:96
            y:288
        }
    
        Auto {
            x:128
            y:64
        }
    
        Turm {
            x:160
            y:288
        }
    }
    

     

    #6454

    Alex
    V-Play Team

    Hi KitKat,

    the problem is the following: You are not really “moving” anything at all, all your code does is specify a new x position for the Box2DBody within the BoxCollider within the Entity. That’s why it looks like the collider is moving without the entity, well in fact you are just changing the position of the collider within the entity.

    Instead you should move the entity itself, which will cause the collider to move with it. And here is how you do that:
    The parent of the body is the boxcollider and the parent of the boxcollider is the entity. This means instead of “selectedBody.x” you need to change “selectedBody.parent.parent.x”. You could of course save the selected entity in a property variant (like the Body), to make it more readable than lots of .parent.parent calls.

    Cheers,
    Alex

    #6457

    kitKat

    THAAAANNNKKKSSS!!!! 😀

Viewing 6 posts - 1 through 6 (of 6 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