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

Forums

OverviewFelgo 3 Support (Qt 5) › Box Collider Offset

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

    Darren

    Hi Guys,

    I’m having a problem with Box Collider.

    Issue 1. 

    Full display of problem is shown on this YouTube link: Video showing problem

    I create an entity with a BoxCollider:

     BoxCollider {
            id: collider
            width: image.width * .75
            height: image.height * .75
            anchors.centerIn: staticObject
            bodyType: Body.Static
            categories: ColliderCategories.solidBackgroundObject
        }

    When I create my entity without specifying a rotation, it works fine and the BoxCollider is centred in it.

    When I rotate my entity dynamically (by using the spin entity arrows I implemented into my level editor) – it still works fine and the BoxCollider rotates with the entity.

    PROBLEM: When I create my entity and specify a rotation then the BoxCollider is offset from the entity.  The BoxCollider is in the correct rotation, but is offset.

    My PhysicsWorld and my EntityManager are both in the same container:

    Item{
            id: container
          transform: Rotation { origin.x: camera.zoom * (playerTank.x + playerTank.width/2); origin.y: camera.zoom * (playerTank.y + playerTank.height/2) ; angle:  container.rotateAngle;}
          property real rotateAngle: (rotationFree) ? 0 : -playerTank.rotation-90
    
          EntityManager {
              id: entityManager
              entityContainer: container
              // required for LevelEditor, so the entities can be created by entityType
              poolingEnabled: true
    
              dynamicCreationEntityList: [  Qt.resolvedUrl("../entities/Background.qml"),
                  Qt.resolvedUrl("../entities/TankTrack.qml"),Qt.resolvedUrl("../entities/Tree.qml"),
                  Qt.resolvedUrl("../entities/BaddyTank.qml"),Qt.resolvedUrl("../entities/Tank.qml"),
                  Qt.resolvedUrl("../entities/TankBullet.qml"),Qt.resolvedUrl("../entities/Target.qml"),
                  Qt.resolvedUrl("../entities/Waypoint.qml"),Qt.resolvedUrl("../entities/MuzzleFlash.qml"),
                  Qt.resolvedUrl("../entities/DamageSmokeParticle.qml"),Qt.resolvedUrl("../entities/ExhaustParticle.qml"),
                  Qt.resolvedUrl("../entities/SpawnPoint.qml"),Qt.resolvedUrl("../entities/Scenery.qml"),
                  Qt.resolvedUrl("../entities/BaddyTankLight.qml"),Qt.resolvedUrl("../entities/Turret.qml"),
                  Qt.resolvedUrl("../entities/ImageOnly.qml"),Qt.resolvedUrl("../entities/StaticObject.qml"),
                  Qt.resolvedUrl("../entities/EntitySupervisor.qml")]
    
          }
    
          BackgroundEdges{/*Outside edges of water around background entities*/}
          // use a physics world because we need collision detection
          PhysicsWorld {
                id: world
                updatesPerSecondForPhysics: 60
                debugDrawVisible : true
          }
    }//Container

     

     

    Issue 2:

    Video link displaying problem

    I have a BoxCollider within an entity that is only for collision detection and not for physics movement.  When I rotate the entity, the BoxCollider offsets itself.

    collisionTestingOnlyMode is set to true.

    If I change collisionTestingOnlyMode to false, then the BoxCollider centres perfectly.  However I want it to only be for collision testing.

    How do I keep the BoxCollider centred and stay in collisionTestingMode?

    #18090

    Alex
    Felgo Team

    Hi Darren,

    my first guess is that those issues could be fixed when you remove the anchoring of the colliders, and rather use x/y positioning if necessary. Since we updated to the latest Box2D version, anchoring is not recommended for colliders in all cases.

    Let me know if this helps, else we’ll have to dig deeper and might need some sample code.

    Cheers,
    Alex

    #18120

    Darren

    Hi Alex,

    Thank you for your swift support as ever!

    I just did a quick experiment with my static object (the first issue listed above).  I substituted the anchoring for the below:

    BoxCollider {
            id: collider
            width: image.width * .75
            height: image.height * .75
            x: 0 //staticObject.x
            y: 0 //staticObject.y
            transformOrigin: Item.Center
            //anchors.centerIn: staticObject
            bodyType: Body.Static
            categories: ColliderCategories.solidBackgroundObject
        }

    As you can see I’ve made the x and y co-ordinates of the collider = 0.  Because it is in the co-ordinate system of the staticObject – which the collider is part of.

    This does put the collider in the right place, however it has not fixed the issue.  If the object is rotated, when I destroy and re-create it, then the collider is still offset – the same way that it is when using anchoring.

    Any other ideas?

    Cheers

    Darren.

    #18123

    Alex
    Felgo Team

    Hi Darren,

    could you create a minimum example that contains the issue so we can debug this faster? You can post it here if it is really small or alternatively send it to support@felgo.com

    Cheers,
    Alex

    #18134

    Darren

    Thanks Alex – I will try to re-create the problem on a simple example and send it to you.  I had a little play with it last night and couldn’t get it to re-create, so need to delve into my code some more.  I’ll get back to you.

    Cheers

    Darren.

    #18135

    Alex
    Felgo Team

    Alright, keep us updated! 🙂

    Cheers,
    Alex

    #18150

    Darren

    Ok Alex, so I worked out what the problem was.  Though I haven’t yet worked out a suitable solution.

    The issue is this:

    • My staticObject has a number of different images that can be associated with it.  I store the image name within variationType.
    • When the staticObject is created, a call to onEntityCreated looks at which variationType is being used.  Depending on this, it then sets the image, width and height of the staticObject.
    • I think that the problem is that by the time onEntityCreated is called, the boxCollider has already been created.  Therefore when the call to onEntityCreated changes the size of the object, the boxCollider is now in the wrong place.

    I have tested it and if I specify the correct width and height at the point where the object is created, then the boxCollider is always centred in the object.

     

    So I now need a way to either set the width and height before the boxCollider is created, or to move the boxCollider after the width and height have been set. I want to keep the specification for the width and height within the object itself and don’t want to have to maintain a list outside of width/heights outside of the object as I think that will get messy.

    Does that make sense and do you have any suggestions?

    Thank you

    Darren.

    #18160

    Alex
    Felgo Team

    Hm, is there any chance that you assign the size using a binding instead of doing it in onEntityCreated?

    Another possibility is that you create the collider dynamically in the onEntityCreated slot, using the createObject method. You can define your collider in a seperate Qml file (like in the example of the link) or using a Component:

    Component {
      id: yourCollider
      BoxCollider {
        // ...
      }
    }

    Cheers,
    Alex

    #18165

    Darren

    Thanks Alex…

    I had thought about the binding idea, however I thought my solution looked unwieldy and not nice.

    Creating the collider dynamically within the onEntityCreated slot works perfectly though!  I have kept my collider in the same file, as a component. It’s easier as it isn’t very big and it logically sits with the rest of the object.

    The collider now aligns perfectly to the object.

    I’m not sure if that will resolve my 2nd issue, but I’m not too worried about that one as it isn’t critical to the game… so I’ll leave that one for now.

    Thank you for your help – great idea re the dynamic collider creation!

    Cheers

    Darren.

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