Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Using fixture.onBeginContact

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #16529

    Todd

    Hey so I am now attempting to create a collision detection that will cause a linear change to launch the player upwards…

     

    The colliders show up around the entities and are anchored in them as should be,

     

    here is my code..

    import QtQuick 2.7
    import QtQuick.Window 2.2
    import Felgo 3.0
    
    GameWindow {
    id:gameWindow
    width: Screen.width
    height: Screen.height
    
    
    
    Item {
    width:Screen.width
    height:Screen.height-10
    focus: true
    
    
    Keys.onPressed: {
    
    if (event.key===Qt.Key_Right){
    event.accepted = true;
    walk.x=(walk.x) +7
    
    }
    if (event.key===Qt.Key_Left){
    event.accepted = true;
    walk.x=(walk.x) -7
    }
    }
    
    
    
    Flickable{
    width:Screen.width
    height:Screen.height
    contentHeight: Screen.height *4
    contentWidth: Screen.width
    interactive: true
    boundsBehavior: Flickable.StopAtBounds
    
    
    
    Image{
    id: box
    anchors.fill: parent
    source: "../assets/rect.png"
    sourceSize.width: Screen.width
    sourceSize.height: Screen.height*4
    }
    
    Scene {
    
    
    
    
    
    
    
    
    
    
    Image{
    id:twitter
    source: "../assets/twitter.png"
    x:128
    y:80
    width:145
    height:170
    }
    
    
    
    PhysicsWorld
    {
    gravity.y: 9.81
    debugDrawVisible: true
    }
    
    
    
    
    
    EntityManager {
         id: entityManager
         entityContainer: scene// this property is needed if you want to dynamically create entities at runtime, if you don't do this, you can remove it.
       }
    
    EntityBase {
    id: cannonbox
        entityId:  "cannonbox"
        entityType: "box"
        width: 125
        height: 190
        x:100
        y:250
    
    AnimatedSprite {
        id: cannon
        anchors.fill: cannonbox
        width: 150
        height: 212
    
        source: "../assets/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
    
    }
    
    
    
    
    BoxCollider {
    id:box1
        anchors.fill: parent
        collisionTestingOnlyMode: true
    }
    
    
    
    }
    EntityBase {
        id: player
            entityId:  "player"
            entityType: "box"
            width: 70
            height: 125
    
            AnimatedSprite {
                id: walk
                width: 35
                height: 90
    
                source: "../assets/WalkingManSpriteSheet.png"
                frameCount: 8
                frameRate: 6
                frameWidth: 41
                frameHeight:49
                loops: 100
                x: 20
                y: 230
    
            }
            BoxCollider {
            id:box2
                anchors.fill: walk
    focus: true
                collisionTestingOnlyMode: true
    
                fixture.onBeginContact: {
    
                       // for access of the collided entity and the entityType and entityId:
                       var body = other.getBody();
                       var collidedEntity = body.target;
                       var collidedEntityType = collidedEntity.entityType;
                       var collidedEntityId = collidedEntity.entityId;
                     }
            }
    }
    
    
    }
    }
    }
    }
    

     

    What do i need to do the the fixture.onBeginContact to make the linear change? I’ve tried looking at examples but I’m at a loss right now. Thanks!

    #16531

    Todd

    Still working on it, I cannot get the two entities to sense the collision. This is what I have.

     

    import QtQuick 2.7
    import QtQuick.Window 2.2
    import Felgo 3.0
    
    GameWindow {
    id:gameWindow
    width: Screen.width
    height: Screen.height
    
    
    
    Item {
    width:Screen.width
    height:Screen.height-10
    focus: true
    
    
    Keys.onPressed: {
    
    if (event.key===Qt.Key_Right){
    event.accepted = true;
    walk.x=(walk.x) +7
    
    }
    if (event.key===Qt.Key_Left){
    event.accepted = true;
    walk.x=(walk.x) -7
    }
    }
    
    
    
    Flickable{
    width:Screen.width
    height:Screen.height
    contentHeight: Screen.height *4
    contentWidth: Screen.width
    interactive: true
    boundsBehavior: Flickable.StopAtBounds
    
    
    
    Image{
    id: box
    anchors.fill: parent
    source: "../assets/rect.png"
    sourceSize.width: Screen.width
    sourceSize.height: Screen.height*4
    }
    
    Scene {
        id:scene
    
    
    
    
    
    
    
    
    
    
    Image{
    id:twitter
    source: "../assets/twitter.png"
    x:128
    y:80
    width:145
    height:170
    }
    
    
    
    PhysicsWorld
    {
    gravity.y: 9.81
    debugDrawVisible: true
    z: 10
    }
    
    
    
    
    
    EntityManager {
         id: entityManager
         entityContainer: scene// this property is needed if you want to dynamically create entities at runtime, if you don't do this, you can remove it.
       }
    
    EntityBase {
    id: cannonBox
        entityId:  "cannonbox"
        entityType: "box"
        width: 125
        height: 190
        x:100
        y:250
    
    AnimatedSprite {
        id: cannon
        anchors.fill: cannonBox
        width: 150
        height: 212
    
        source: "../assets/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
    
    }
    
    
    
    
    BoxCollider {
    id:box1
        anchors.fill: parent
        collisionTestingOnlyMode: true
        bodyType: Body.Dynamic
    }
    
    
    
    }
    EntityBase {
        id: player
            entityId:  "player"
            entityType: "box"
            width: 70
            height: 125
    
            AnimatedSprite {
                id: walk
                width: 35
                height: 90
    
                source: "../assets/WalkingManSpriteSheet.png"
                frameCount: 8
                frameRate: 6
                frameWidth: 41
                frameHeight:49
                loops: 100
                x: 20
                y: 300
    
            }
            BoxCollider {
            id:box2
                anchors.fill: walk
    focus: true
                collisionTestingOnlyMode: true
                bodyType: Body.Dynamic
    
                fixture.onBeginContact:   {
    
                    box2.linearVelocity.y = -600
    
    
                     }
            }
    }
    
    
    }
    }
    }
    }

     

    #16536

    Todd

    Another update…

    So I edited it some more, and now I have a log in the console that prints input saying the entities are detecting collisions. The problem is I have a onclicked funtion for the player so that it will fire off a linear impulse, but when I click I get an error in the console that says

    ” file:///C:/Users/todd/OneDrive/Company/MISC/finalcelebration/qml/Main.qml:166: ReferenceError: collider is not defined ”

     

    So this is my code now,

    import QtQuick 2.7
    import QtQuick.Window 2.2
    import Felgo 3.0
    
    GameWindow {
    id:gameWindow
    width: Screen.width
    height: Screen.height
    
    
    
    Item {
    width:Screen.width
    height:Screen.height-10
    focus: true
    
    
    Keys.onPressed: {
    
    if (event.key===Qt.Key_Right){
    event.accepted = true;
    walk.x=(walk.x) +7
    
    }
    if (event.key===Qt.Key_Left){
    event.accepted = true;
    walk.x=(walk.x) -7
    }
    }
    
    
    
    Flickable{
    width:Screen.width
    height:Screen.height
    contentHeight: Screen.height *4
    contentWidth: Screen.width
    interactive: true
    boundsBehavior: Flickable.StopAtBounds
    
    
    
    Image{
    id: box
    anchors.fill: parent
    source: "../assets/rect.png"
    sourceSize.width: Screen.width
    sourceSize.height: Screen.height*4
    }
    
    Scene {
        id:scene
    
    
    
    
    
    
    
    
    
    
    Image{
    id:twitter
    source: "../assets/twitter.png"
    x:128
    y:80
    width:145
    height:170
    }
    
    
    
    PhysicsWorld
    {
    gravity.y: 9.81
    debugDrawVisible: true
    z: 10
    }
    
    
    
    
    
    EntityManager {
         id: entityManager
         entityContainer: scene// this property is needed if you want to dynamically create entities at runtime, if you don't do this, you can remove it.
       }
    
    EntityBase {
    id: cannonBox
        entityId:  "cannonbox"
        entityType: "box"
        width: 125
        height: 190
        x:100
        y:250
    
    AnimatedSprite {
        id: cannon
        anchors.fill: cannonBox
        width: 150
        height: 212
    
        source: "../assets/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
    
    }
    
    
    
    
    BoxCollider {
    id:box1
        anchors.fill: parent
        collisionTestingOnlyMode: true
        bodyType: Body.Dynamic
    }
    
    
    
    }
    EntityBase {
        id: player
            entityId:  "player"
            entityType: "box"
            width: 70
            height: 125
    
            AnimatedSprite {
                id: walk
                width: 35
                height: 90
    
                source: "../assets/WalkingManSpriteSheet.png"
                frameCount: 8
                frameRate: 6
                frameWidth: 41
                frameHeight:49
                loops: 100
                x: 20
                y: 300
    
            }
            BoxCollider {
            id:box2
                anchors.fill: walk
    focus: true
                collisionTestingOnlyMode: true
                bodyType: Body.Dynamic
                linearVelocity: Qt.point(100,0)
                PropertyAnimation on x { from: 0; to: 100; duration: 1000 }
                    onXChanged: console.log("x changed to " + x)
    
                    MouseArea {
                        anchors.fill: parent
    
                        onClicked: {
                          var forwardVectorInBody = box2.body.toWorldVector(Qt.point(10, 0));
    
                          // apply the impulse from the world center, in the direction of movement
                          collider.applyLinearImpulse(forwardVectorInBody, collider.body.getWorldCenter());
                        }
                      }
    
                    fixture.onBeginContact: {
                         // retrieve properties of other colliding entity
                          var otherFixture = other
                          var otherBody = otherFixture.getBody()
                          var otherEntity = otherBody.target
                          var otherType = otherEntity.entityType
                          console.log("OTHER: "+otherFixture)
                          console.log("BODY: "+otherBody)
                          console.log("ENTITY: "+otherEntity)
                          console.log("TYPE: "+otherType)
                        }
            }
    }
    
    
    }
    }
    }
    }

     

    #16537

    Alex
    Felgo Team

    That’s because your BoxCollider has the id box2 and not collider.

    Cheers,
    Alex

    #16538

    Todd

    Thanks, no reference error, but the object will not do the linearimpulse, do you know why?

    #16549

    Günther
    Felgo Team

    Hi Todd!

    Your collider uses collisionTestingOnlyMode: true, which completely deactivates physics calculations for this body and only allows to check collisions. This also means that applying an impulse won’t work. You can only apply an impulse by removing this setting.

    Also, for the impulse to work, it is important to set the collider density to a value that will allow the body to move based on the amount of force that you apply.

    In your example a setting of  density: 0.00005 should work to see an effect when applying an impulse with Qt.point(10,0).

    Best,

    Günther

    #16553

    Todd

    Thanks, so when I comment out the collisionTestingOnlyMode, the entity will just disappear… at first thought maybe it was the z ordering but I changed that to 10 and the player still will not show up, but it still registers collisions when i move him into the cannon. Do you know what’s causing this?

    #16554

    Todd

    When I set the body to Static the player will appear where its supposed to, but if i set collisiontestingmode to false he wont be visible anymore. A Static Entity cannot be affected by a linear impulse correct? How would I solve this issue?

     

    This is the bit of code for the player..

     

    EntityBase {
        id: player
            entityId:  "player"
            entityType: "box"
            width: 70
            height: 125
    
            AnimatedSprite {
                id: walk
                width: 35
                height: 90
    z: 10
                source: "../assets/WalkingManSpriteSheet.png"
                frameCount: 8
                frameRate: 6
                frameWidth: 41
                frameHeight:49
                loops: 100
                x: 20
                y: 300
    
            }
            BoxCollider {
            id:box2
            z:10
                anchors.fill: walk
    focus: true
                collisionTestingOnlyMode: true
                bodyType: Body.Dynamic
                linearVelocity: Qt.point(100,0)
                PropertyAnimation on x { from: 0; to: 100; duration: 1000 }
                    onXChanged: console.log("x changed to " + x)
    
                    MouseArea {
                        anchors.fill: parent
    
                        onClicked: {
                          var forwardVectorInBody = box2.body.toWorldVector(Qt.point(10, 0));
    
                          // apply the impulse from the world center, in the direction of movement
                          box2.applyLinearImpulse(forwardVectorInBody, box2.body.getWorldCenter());
                        }
                      }
    
                    fixture.onBeginContact: {
                         // retrieve properties of other colliding entity
                          var otherFixture = other
                          var otherBody = otherFixture.getBody()
                          var otherEntity = otherBody.target
                          var otherType = otherEntity.entityType
                          console.log("OTHER: "+otherFixture)
                          console.log("BODY: "+otherBody)
                          console.log("ENTITY: "+otherEntity)
                          console.log("TYPE: "+otherType)
                        }
            }
    }
    
    
    }
    }
    }
    }

     

    #16559

    Todd

    Another update,

    Silly on my part, the player and cannon were falling because of gravity, but now I need help with the game window/focus.

     

    I want the screen to constantly have the player centered, it is a flickable so when the player shoots up it will not stay centered and I cannot drag that fast. I tried settting activeFocus: true in the entity part of the player, like this..

     

    EntityBase {
        id: player
            entityId:  "player"
            entityType: "box"
            width: 70
            height: 125
            activeFocus: true
    
    
            AnimatedSprite {
                id: walk
                width: 35
                height: 90
    z: 10
                source: "../assets/WalkingManSpriteSheet.png"
                frameCount: 8
                frameRate: 6
                frameWidth: 41
                frameHeight:49
                loops: 100
                x: 20
                y:15
    
    
            }
            BoxCollider {
            id:box2
    
            z:10
                anchors.fill: walk
    focus: true
                collisionTestingOnlyMode: false
                bodyType: Body.Dynamic
                linearVelocity: Qt.point(100,0)
                PropertyAnimation on x { from: 0; to: 100; duration: 1000 }
                    onXChanged: console.log("x changed to " + x)
    
                    MouseArea {
                        anchors.fill: parent
    
                        onClicked: {
                          var upwardVectorInBody = box2.body.toWorldVector(Qt.point(0, -620));
    
                          // apply the impulse from the world center, in the direction of movement
                          box2.applyLinearImpulse(upwardVectorInBody, box2.body.getWorldCenter());
                        }
                      }
    
                    fixture.onBeginContact: {
                         // retrieve properties of other colliding entity
                          var otherFixture = other
                          var otherBody = otherFixture.getBody()
                          var otherEntity = otherBody.target
                          var otherType = otherEntity.entityType
                          console.log("OTHER: "+otherFixture)
                          console.log("BODY: "+otherBody)
                          console.log("ENTITY: "+otherEntity)
                          console.log("TYPE: "+otherType)
                        }
            }
    }

     

     

    How would I achieve this?

     

     

     

    and get this error and it crashes ” Invalid property assignment: “activeFocus” is a read-only property “

    #16563

    Günther
    Felgo Team

    Hi Todd,

    I’d like to follow up via email with some solutions we can offer to fix your issues!
    However, the email address you registered with Felgo does not seem to be available anymore, do you maybe have a new email address?

    Best,
    Günther

    #16572

    Todd
    #16577

    Kool

    Hi Todd,

    I cooked something up whilst installing the latest update:

    import Felgo 3.0
    import QtQuick 2.0
    
    GameWindow {
        id: gameWindow
    
        activeScene: scene
        screenWidth: 960
        screenHeight: 640
    
        Scene {
            id: scene
    
            width: 480
            height: 320
    
            Flickable {
                id: flickable
                anchors.fill: parent
                contentHeight: content.height
                contentWidth: content.width
    
                Item {
                    height: content.height
                    width: content.width
    
                    Column {
                        id: content
                        width: flickable.width
    
                        Repeater {
                            model: 100
    
                            Rectangle {
                                height: 50
                                width: content.width
                                border {color: "black"; width: 1}
                            }
                        }
                    }
                }
    
                contentY: contentHeight - flickable.height - player.y
            }
    
            Rectangle {
                id: player
    
                x: scene.width / 2
                y: scene.height / 2
    
                color: "red"
                width: 10
                height: 10
    
                MouseArea {
                    anchors.fill: parent
                    onClicked: movement.running = true
                }
    
                MovementAnimation {
                    id: movement
                    target: player
                    running: false
                    property: "y"
                    velocity: -10
                }
            }
        }
    }
    

     

    Try clicking the player (red rectangle). You’ll see that as the player moves up, the flickable scrolls with it.

    However, if you want the player to always be centered, then the player’s y position should not change. Instead, apply the y changes (e.g. impulses, velocities) to the flickable’s contentY and you’ll get the desired effect:

    import Felgo 3.0
    import QtQuick 2.0
    
    GameWindow {
        id: gameWindow
    
        activeScene: scene
        screenWidth: 960
        screenHeight: 640
    
        Scene {
            id: scene
    
            width: 480
            height: 320
    
            Flickable {
                id: flickable
                anchors.fill: parent
                contentHeight: content.height
                contentWidth: content.width
                contentY: contentHeight - flickable.height // start at the bottom
    
                Item {
                    height: content.height
                    width: content.width
    
                    Column {
                        id: content
                        width: flickable.width
    
                        Repeater {
                            model: 100
    
                            Rectangle {
                                height: 50
                                width: content.width
                                border {color: "black"; width: 1}
                            }
                        }
                    }
                }
            }
    
            MovementAnimation {
                id: movement
                target: flickable
                running: false
                property: "contentY"
                velocity: -10
            }
    
            Rectangle {
                id: player
    
                x: scene.width / 2
                y: scene.height / 2
    
                color: "red"
                width: 10
                height: 10
    
                MouseArea {
                    anchors.fill: parent
                    onClicked: movement.running = true
                }
            }
        }
    }
    

     

     

    Hope that helps!

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

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded