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

Forums

OverviewFelgo 3 Support (Qt 5) › Boxcollider and Movementanimation

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #15910

    Rob

    Hi,

    I’m creating a Car like this

    EntityBase {
        id: car
        entityType: "car"
    
        MultiResolutionImage{
            id: carpic
            source: pickCar(1,7) //this function just picks a diffrent sprite
            anchors.fill: parent
        }
    
        DropShadow{
            anchors.fill: carpic
            horizontalOffset: 5
            verticalOffset: 10
            radius: 3
            color: "#aa000000"
            source: carpic
        }
    
        MovementAnimation {
          id: animationrightleft
          target: parent
          property: "x"
          velocity: generateRandomValueBetween(-50,-100)
          minPropertyValue: 0 
          onLimitReached: {
              car.removeEntity()
          }
        }
    
        BoxCollider{
            anchors.centerIn: carpic
            categories: Box.Category1
            collidesWith: Box.Category2 // Cars can't collide with eachother let only collide with ball
        }
    }

    and im creating them dynamically in my Scene. Problem is the Boxcolliders just appear on top of the screen. They are moving with the cars but on the wrong position. How can i center them correctly on the cars?

     

    Thanks in advance

    Rob

    #15912

    Günther
    Felgo Team

    Hi Rob!

    There’s a small difference between the BoxCollider and the Car entity item:

    • The actual physics collider that you see at the wrong position registers with and is part of the PhysicsWorld in your project (positioned relative to the PhysicsWorld)
    • The QML entity item that is linked with the collider is treated as a normal QML item (positioned relative to the parent QML item)

    This means, that the position of the Car is also affected by the position of all parent items in your QML hierarchy, while the collider is not. If put in other works, you can say they are in different coordinate systems. (PhysicsWorld v Entity Parent Item)

    To avoid this problem you can

    • Remove the additional position offset of the entity (e.g. by avoiding position modifications in parent items of the entity or by placing the entity directly into your Scene besides the PhysicsWorld)
    • Take the relative position of the Car’s parents into account for positioning the collider

    You can also have a look at Item::mapFromItem and Item::mapToItem position mapping functions to calculate position of items in different coordinate systems.

    Hope this helps!

    Best,
    Günther

     

    #15921

    Rob

    Thanks with this insight I could solve the problem

     

    Best

    Rob

    #17062

    Barb

    Can you please give an example how to map coordinates from PhysicsWorld to parent-relative coordinates with the suggested functions?
    In my case I have a BoxCollider (Body.Dynamic) that shall move to the position of a moving Image collectorPlayer. It works for my default resolution but as soon as the scene is scaled to a different screen ratio there is a misplacement of the collider by exactly the sum of the borders added to the scene to fit the game window.

     

    PhysicsWorld does not have gravity or anything else set, setting its x, y, width, height to those of the scene did not work as adaption of the coordinates system.

    I need to calculate Image positions independent from PhysicsWorld, so I see the only solution in replacement of the BoxCollider PhysicsWorld coordinates to parent-relative coordinates like x: collectorPlayer.x, width: collectorPlayer.width etc. instead of using anchors, this was my attempt 3 of the attached snippets. Any solution adapting one of my 3 attempts would be great. Thanks.

     

    //original attempt
            BoxCollider {
              categories: Box.Category2
              collidesWith: Box.Category1
              anchors.fill: collectorPlayer
              bodyType: Body.Dynamic
              collisionTestingOnlyMode: true
              fixture.onBeginContact: { 
                  //...
              }
            }
    
    
    //second attempt 
    BoxCollider {
              id: playerCollider
              categories: Box.Category2
              //may collide with the fruit but not with the ground
              collidesWith: Box.Category1
              x: collectorPlayer.x
              y: collectorPlayer.y + (gameWindow.height-scene.height)
              width: collectorPlayer.width
              height: collectorPlayer.height
              bodyType: Body.Dynamic
              collisionTestingOnlyMode: true
    ....

     

    //3rd attempt - probably the best idea
     BoxCollider {
              id: playerCollider
              categories: Box.Category2
              //may collide with the fruit but not with the ground
              collidesWith: Box.Category1
              x: collectorPlayer.x
              y: playerCollider.mapToItem(scene, collectorPlayer.x, collectorPlayer.y).y
              width: collectorPlayer.width
              height: collectorPlayer.height
              bodyType: Body.Dynamic
              collisionTestingOnlyMode: true
    ....

     

    #17082

    Günther
    Felgo Team

    Hi Barb!

    I guess you have the PhysicsWorld placed within the GameWindow, and the Entity with your BoxCollider placed within your Scene?
    This would explain the collider position mismatch when the GameWindow size changes.

    Is there a reason why you do not add the PhysicsWorld as well as the Entity to your Scene, so you won’t have to worry about the position offset?
    That would be the easiest solution and my first suggestion.

    Best,
    Günther

     

    #17084

    Barb

    thank you for the reply. I also tried moving the PhysicsWorld to within the scene as it also made sense to me according to your reply from February. didn’t change anything though. rescaling works for resolution changes as long as screen ratio is the same as for the original scene. should I set anything within PhysicsWorld like anchors?

     

    #17087

    Günther
    Felgo Team

    If both the PhysicsWorld and the Entity are within your Scene, the entity position and collider position should match up.

    You can have a look at the FlappyBird demo for example:

    • Open GameScene.qml
    • Set debugDrawVisible: true for PhysicsWorld { } to see the colliders
    • Run the game and resize the window, the bird collider always matches the position of the player bird entity

    If you compare the demo source code with your Scene and Entity implementation, do you see any difference that might cause your position mismatch?

    Best,
    Günther from Felgo

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