Forums

OverviewFelgo 2 Support (Qt 5) › Physics in animation

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #16416

    Todd

    Hey again,

     

    I am working on my animation and now i can see all the images and sprites are working fine, but now I am having trouble getting physicsWorld to work and show.

     

    This is my code for the player.qml which I want a BoxCollider for,

     

    import Felgo 3.0
    import QtQuick 2.0
    
    
    EntityBase {
    id: player
    width:30
    height: 60
        entityType: "player"
    
        property alias collider: collider
        property alias horizontalVelocity: collider.linearVelocity.x
    PhysicsWorld {
    debugDrawVisible: true
    }
        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: player.height
          width: player.width
    
          // 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
          }
        }
    }
    

     

    No errors in the code, I just cannot see any colliders and they are not registering… thanks for all your help!

    #16417

    Alex
    Felgo Team

    Hi Todd,

    please follow this tutorial to learn the basics of physics and the entity concept. The PhysicsWorld belongs in your Scene or any other container of your game world, and the entities only need colliders.

    Also have a look at the documentation if you are not sure how to use a component, we have a lot of examples in there as well, like this one: https://felgo.com/doc/vplay-physicsworld/

    Cheers,
    Alex

    #16419

    Todd

    Hey, thanks for the reply.

     

    I went through and figured it out, I can now see the box collider when running, but for some reason the box collider does not have my cannon image inside of it and im Ā not sure why, i have it the box collider anchored inside the cannon sprite image but it places the image outside of it. 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
    }
    
    
    
    
    
    
    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
    }
    
    PhysicsWorld
    {
    gravity.y: 9.81
    debugDrawVisible: true
    }
    
    Scene {
    id:scene
    
    
    
    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 {
    entityId: cannonbox
        entityType: "box"
    
    AnimatedSprite {
        id: cannon
        width: 150
        height: 212
    anchors.fill: cannon
        source: "../assets/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
    
    }
    
    BoxCollider {
        anchors.fill: cannon
        collisionTestingOnlyMode: true
    }
    
    }
    }
    
    }
    }
    }

     

    Do you know why the box collider is not anchoring with the cannon image?

    #16422

    Todd

    Quick update…

     

    So I was able to get the box collider to fit around the cannon image by just setting x and y cordinates, but no matter what I do I cannot just get an anchors.fill to work on it…. would be helpful to know why not for once I start moving the images around

    #16428

    Marcin

    Hi Todd,
    It is hard to replicate the issue because I don’t have the images you use in the code.
    If you can provide some code snippet with some files which I can just test it, would be great.
    Probably would figure it out quickly what is wrong.

    If not, there is one thing.
    Maybe it is not the issue but I saw something odd in the example you provided.
    So like you said, the collider is anchored inside the sprite animation(anchors.fill: cannon).
    But the animation is centered inside itself.
    In the sprite animation bit:

    anchors.fill: cannon

    Bu the animation sprite is named cannon.
    Didn’t you want to use there:

    anchors.fill: cannonbox

    So basically center the animation sprite in the entity?

    What I would also propose is to set entity width and height.
    The same as the sprite animation(or the frame etc. can’t tell without images and do proper test case replication).
    Hopefully this will improve things.

    #16511

    Todd

    Hey Marcin, thanks for the reply.

    Been on vacation so now I am finally looking at this again. So I tried doing what you said with centering the animation sprite inside the entity, but it will still set the box collider outside of the cannon. What can i do to easily send you the files you need to test? Also note that for some reason when I place the cannon sprite image in the scene it will not hug the upper left corner as it should without and coordinates set for it…

     

    Thanks!

    #16512

    Todd

    Hey so i messed around with it some more…

     

    I went ahead and added a box collider around the player walking, except it will not anchor around it and when i move the player the box collider follows behind….. but it (the box collider) will not ever actually be around the player

     

    this is the 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
    }
    
    
    
    
    
    
    
    
    
    
    
    
    Image{
    id:twitter
    source: "../assets/twitter.png"
    x:500
    y:200
    }
    
    PhysicsWorld
    {
    gravity.y: 9.81
    debugDrawVisible: true
    }
    
    Scene {
    id:scene
    
    
    
    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
    
    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: 80
                y: 50
    
            }
            BoxCollider {
            id:box2
                anchors.fill: walk
                bodyType: Body.Dynamic
                collisionTestingOnlyMode: true
            }
    }
    
    
    }
    }
    }
    }

     

     

    why is it doing this šŸ™

    #16524

    GĆ¼nther
    Felgo Team

    Hi!

    The reason is as follows:

    The collider, which you can see when activating debugDrawVisible of PhysicsWorld, is actually placed within the PhysicsWorld and NOT within the entity.
    The collider simply usesĀ the size and position configured within the Entity. So when e.g. the size and position changes, the actual collider within the PhysicsWorld also moves along.

    This means the following conditions are required for the colliders in PhysicsWorld to match with the Entity:

    • The entities themselves should useĀ in the same coordinate system as the PhysicsWorld. If the position of the entities is e.g. influenced by parent items, but the PhysicsWorld is not, the position of the collider won’t match.
    • If your Sprite Images apply a positionĀ offset within the entity, but the collider will match the entity, but the sprite image within the entity willĀ not match.

    This is a short outtakeĀ of your code sample with fixed entity and collider settings:

    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
          }
        }
    
    
    
        Scene {
          id:scene
    
          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
    
            Rectangle {
              id: cannon
              anchors.fill: cannonbox
              color: "blue"
            }
    
            BoxCollider {
              id:box1
              anchors.fill: parent
              collisionTestingOnlyMode: true
            }
          }
    
          EntityBase {
            id: player
            entityId:  "player"
            entityType: "box"
            width: 70
            height: 125
            x: 80
            y: 50
    
            Rectangle {
              id: walk
              width: 35
              height: 90
              color: "green"
            }
    
            BoxCollider {
              id:box2
              anchors.fill: walk
              bodyType: Body.Dynamic
              collisionTestingOnlyMode: true
            }
          }
        }
    
      }
    }
    

     

    Best,
    GĆ¼nther

    #16528

    Todd

    Gunther,

     

    Thanks so much! You are awesome!

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