Forums

OverviewFelgo 2 Support (Qt 5) › Physics with application

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #16325

    Todd

    Hey again,

    I am trying to implement physics into my game but I am having some trouble…

    So basically I have a sprite of a player walking across the screen and I want them to vertically raise with a lot of velocity on contact with a cannon. My problem is getting physicsWorld and colliders working, as well as gravity.

    I’ve coded in physicsWorld into one of my QML files, in this case I put it in the gameScene.qml which is this,

    import QtQuick 2.7
    import QtQuick.Window 2.2
    import Felgo 3.0
    import "entities"
    
    
    
    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: "artwork/rect.png"
    sourceSize.width: Screen.width
    sourceSize.height: Screen.height*4
    }
    
    
    AnimatedSprite {
        id: walk
        width: 100
        height: 200
    
        source: "artwork/WalkingManSpriteSheet.png"
        frameCount: 8
        frameRate: 6
        frameWidth: 41
        frameHeight:49
        loops: 100
        x: 200
        y: 50
    
    }
    
    
    
    Image{
    id:twitter
    source: "artwork/twitter.png"
    x:500
    y:200
    }
    AnimatedSprite {
        id: cannon
        width: 300
        height: 425
    
        source: "artwork/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
        x:300
        y:90
    }
    
    
    }
    }

    I dont get any error codes but i am not able to see colliders, what am i doing wrong? I tried referencing examples and tutorials, thanks in advance.

     

    P.S. – I made a separate folder called entities that contains the player.qml which is the AnimatedSprite with id: walk, but once i do that and delete the sprite code from gameScene.qml it wont show up at all but no errors in the output….? Even though i have the import statement for entities. This is the player.qml in case that will help with anything.

     

    import Felgo 3.0
    import QtQuick 2.0
    
    EntityBase {
    id: player
    
        entityType: "player"
    
        property alias collider: collider
        property alias horizontalVelocity: collider.linearVelocity.x
    
        AnimatedSprite {
            id: walk
            width: 100
            height: 200
            anchors.centerIn: parent
            source: "artwork/WalkingManSpriteSheet.png"
            frameCount: 8
            frameRate: 6
            frameWidth: 41
            frameHeight:49
            loops: 100
            x: 200
            y: 50
    
    }
        BoxCollider {
          id: collider
          height: parent.height
          width: 30
          anchors.horizontalCenter: parent.horizontalCenter
          // this collider must be dynamic because we are moving it by applying forces and impulses
          bodyType: Body.Dynamic // this is the default value but I wanted to mention it ;)
          fixedRotation: true // we are running, not rolling...
          bullet: true // for super accurate collision detection, use this sparingly, because it's quite performance greedy
          sleepingAllowed: false
          // apply the horizontal value of the TwoAxisController as force to move the player left and right
          force: Qt.point(controller.xAxis*170*32,0)
          // limit the horizontal velocity
          onLinearVelocityChanged: {
            if(linearVelocity.x > 170) linearVelocity.x = 170
            if(linearVelocity.x < -170) linearVelocity.x = -170
          }
        }
    }

     

    #16331

    Günther
    Felgo Team

    Hi Todd!

    You did not include the PhysicsWorld in your code sample, but do you set the debugDrawVisible to true for PhysicsWorld?
    This allows to show the usually invisible colliders of bodies registered with the PhysicsWorld.

    Also I see a potential issue in your Player.qml: The player entity does not have a width and height set, which also sets the BoxCollider height to 0 as it references parent.height. Do you see the player after setting the correct size and including Player { … } in your game scene?

    Best,
    Günther

     

    #16413

    Todd

    Hey, so I made a few changes, and im still not able to see anything when the application is running, except for a blank grey window. I am using a Felgo empty game template.

     

    My main.qml file is this….

    import QtQuick 2.7
    import QtQuick.Window 2.2
    import Felgo 3.0
    
    GameWindow {
    width: 320
    height: 480
    
    
    
    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
    }
    
    
    AnimatedSprite {
        id: walk
        width: 100
        height: 200
    
        source: "assets/WalkingManSpriteSheet.png"
        frameCount: 8
        frameRate: 6
        frameWidth: 41
        frameHeight:49
        loops: 100
        x: 200
        y: 50
    
    }
    
    
    
    Image{
    id:twitter
    source: "assets/twitter.png"
    x:500
    y:200
    }
    AnimatedSprite {
        id: cannon
        width: 300
        height: 425
    
        source: "assets/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
        x:300
        y:90
    }
    
    
    }
    }
    }
    

     

     

    I also have a folder named entities with a qml file inside of it named player.qml which is this

    import Felgo 3.0
    import QtQuick 2.0
    
    EntityBase {
    id: player
    
        entityType: "player"
    
        property alias collider: collider
        property alias horizontalVelocity: collider.linearVelocity.x
    
        AnimatedSprite {
            id: walk
            width: 100
            height: 200
            anchors.centerIn: parent
            source: "assets/WalkingManSpriteSheet.png"
            frameCount: 8
            frameRate: 6
            frameWidth: 41
            frameHeight:49
            loops: 100
            x: 200
            y: 50
    
    }
        BoxCollider {
          id: collider
          height: parent.height
          width: 30
          anchors.horizontalCenter: parent.horizontalCenter
          // this collider must be dynamic because we are moving it by applying forces and impulses
          bodyType: Body.Dynamic // this is the default value but I wanted to mention it ;)
          fixedRotation: true // we are running, not rolling...
          bullet: true // for super accurate collision detection, use this sparingly, because it's quite performance greedy
          sleepingAllowed: false
          // apply the horizontal value of the TwoAxisController as force to move the player left and right
          force: Qt.point(controller.xAxis*170*32,0)
          // limit the horizontal velocity
          onLinearVelocityChanged: {
            if(linearVelocity.x > 170) linearVelocity.x = 170
            if(linearVelocity.x < -170) linearVelocity.x = -170
          }
        }
    }

     

    after running the application this is the output..

     

    Starting C:\Users\toddp_000\OneDrive\Company\MISC\build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug\debug\finalcelebration.exe...
    QML debugging is enabled. Only use this in a safe environment.
    NOTE: you are running a development build in Debug Mode. When you are ready to publish your app to the app stores, switch to Release mode and enable a publish build. You can enable a publish build in the config.json file in your qml folder by setting the "stage" property to "publish". For more information see: https://felgo.com/doc/vplay-publishing/
    qml: no explicit license key set, but a userLicenseKey is found - checking for its validity instead
    qml: no explicit license key set, but a userLicenseKey is found - checking for its validity instead
    file:///C:/Users/toddp_000/OneDrive/Company/MISC/build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug/qml/Main.qml:64:1: QML Image: Cannot open: file:///C:/Users/toddp_000/OneDrive/Company/MISC/build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug/qml/assets/twitter.png
    file:///C:/Users/toddp_000/OneDrive/Company/MISC/build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug/qml/Main.qml:37:1: QML Image: Cannot open: file:///C:/Users/toddp_000/OneDrive/Company/MISC/build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug/qml/assets/rect.png
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    <Unknown File>: QML Sprite: Cannot open: file:///C:/Users/toddp_000/OneDrive/Company/MISC/build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug/qml/assets/WalkingManSpriteSheet.png
    <Unknown File>: QML Sprite: Cannot open: file:///C:/Users/toddp_000/OneDrive/Company/MISC/build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug/qml/assets/cannon.png
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
    qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
    qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
    qt.network.ssl: QSslSocket: cannot call unresolved function SSL_CTX_new
    qt.network.ssl: QSslSocket: cannot call unresolved function SSL_library_init
    qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
    qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
    C:\Users\toddp_000\OneDrive\Company\MISC\build-finalcelebration-Desktop_Qt_5_8_0_MinGW_32bit2-Debug\debug\finalcelebration.exe exited with code 0

     

     

    I have my asset images inside of the assets folder in the program…. any suggestions would be appreciated, thanks so much

     

    #16414

    Alex
    Felgo Team

    Hi Todd,

    as you can see from the log output, your provided path is [something]/qml/assets/[something] while it should be [something]/assets/[something].

    Your QML files are in the qml directory, and since you provide relative paths you need to navigate one folder back before navigating into the assets folder, like this “../assets/WalkingManSpriteSheet.png”.

    Cheers,
    Alex

    #16415

    Todd

    Thank you, it all works perfect now!

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