Forums

OverviewV-Play 2 Support (Qt 5) › Collision categories in fixture.onContactBegin

Tagged: 

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

    Kool

    Suppose I have the following entity:

     

    EntityBase {
        id: platform
        entityType: "anEntity"
    
        width: 100;
        height: 100;
    
        BoxCollider {
            id: collider
    
            bodyType: Body.Dynamic
            collisionTestingOnlyMode: true
    
            categories: Box.Category1
            collidesWith: Box.Category2 | Box.Category3
    
            fixture.onBeginContact: {
               timer.start();
    
               if (contact begin with category 2) {
    
               }
               else {
    
               }
            }
            fixture.onEndContact: {
                if (contact end with category 2) {
    
                }
                else {
    
                }
            }
            fixture.onContactChanged: {
            	if (now contacting with category 1 and category 2) {
    
            	}
            }
        }
    }

    The collider states only contact with Category 2 and Category 3. Is there a quick and easy way to retrieve which category the entity is colliding with? I’m currently having to do the following:

            fixture.onContactBegin: {
                var fixture = other
                var body = other.getBody()
                var entity = body.target
    
                if (entity.entityType === "AnotherEntity") {
                    // do something
                }
           }

    This doesn’t feel clean and I’m wondering if there’s an easier way to get which category the entity is colliding with.

     

    Cheers!

    #12822

    G√ľnther
    V-Play Team

    Hi!
    Good question! ūüėČ

    The information which Category (or even Categories) an entity collides with is not calculated automatically.
    But you can easily retrieve this information yourself, as you have access to both fixtures of the involved entities.

    For example, like this:

    Scene {
     // ...
    EntityBase {
          id: entity1 
          
          // ...
    
          BoxCollider {
            collidesWith: Box.Category1 | Box.Category3 | Box.Category4 | Box.Category7
            fixture.onBeginContact: {
    
              // this collider settings
              console.log("Fixture: "+fixture)
              console.log("Categories: "+fixture.categories)
              console.log("CollidesWidth: "+fixture.collidesWith)
    
              // other collider settings
              console.log("OFixture: "+other)
              console.log("Categories: "+other.categories)
              console.log("CollidesWidth: "+other.collidesWith)
    
              // check which categories collided
              var collidedCategories = fixture.collidesWith & other.categories
              console.log("collidedCategories: "+collidedCategories)
    
              // check for a specific category
              if(collidedCategories & Box.Category1)
                console.log("Category1")
              if(collidedCategories & Box.Category3)
                console.log("Category3")
              if(collidedCategories & Box.Category4)
                console.log("Category4")
               if(collidedCategories & Box.Category7)
                console.log("Category7")
            }
          }
        }
    
        EntityBase {
          id: entity2
          
          // ...
    
          BoxCollider {
            anchors.fill: parent
            bodyType: Body.Static
            categories: Box.Category2 | Box.Category3 | Box.Category5 | Box.Category7
          }
        }
    }

     

    You could also provide a function to be able to do the check quickly:

    EntityBase {
          id: entity1
          
          // ...
    
          BoxCollider {
            id: collider
            collidesWith: Box.Category1 | Box.Category3 | Box.Category4 | Box.Category7
    
            function checkContactWith(other, category) {
              return fixture.collidesWith & other.categories & category
            }
    
            fixture.onBeginContact: {
              if(collider.checkContactWith(other, Box.Category1))
                console.log("Category1")
    
              // ...
            }
          }
        }

     

    Cheers,
    G√ľnther

     

     

     

    • This reply was modified 2 years, 4 months ago by  GT.
    #14919

    Caeser

    Hi!

    This is a good way to filter collisions, but what if i wanted to to know the entity properties of entityType,entityId, and variation when a collision happens.

    How can I further dig for this information after finding out the category?

     

     

    #14920

    Caeser

    When I to retrieve the information, of collideed fixture with the <i>other</i> argument. I get undefined arguments

     

    Here is the Snippet

     

    
                fixture.onBeginContact: {
    
    
                    var body = other.getBody();
                    var collidedEntity = body.target;
                    var collidedEntityType = collidedEntity.entityType;
                    var collidedEntityId = collidedEntity.entityId;
    
                    console.log("body",body)
                    console.log("collidedEntity",collidedEntity)
                    console.log("collidedEntityType",collidedEntityType)
                    console.log("collidedEntityId",collidedEntityId)
    
    
                    // check for a specific category
                    if(checkContactWith(other, Box.Category4)){
                        enemys.destroy()
                    }
                    if (checkContactWith(other, Box.Category1)){
                        console.log("Hit Left Player: ",card_type)
                        
                       
                    }
                    if (checkContactWith(other, Box.Category2)){
                        console.log("Hit Right Player: ",card_type)
                       
                  
                    }

    Application Out Put is

     

    qrc:///qml/VPlay/physics/ColliderBase.qml:317:13: Unable to assign [undefined] to bool
    qml: body Box2DBody(0x2c2fce08)
    qml: collidedEntity QQuickRectangle(0x374b78e0)
    qml: collidedEntityType undefined
    qml: collidedEntityId undefined
    qml: Hit Left Player:  0
    qrc:///qml/VPlay/physics/ColliderBase.qml:317:13: Unable to assign [undefined] to bool
    qml: body Box2DBody(0x2c2fce08)
    qml: collidedEntity QQuickRectangle(0x374b78e0)
    qml: collidedEntityType undefined
    qml: collidedEntityId undefined
    qml: Hit Left Player:  0
    qrc:///qml/VPlay/physics/ColliderBase.qml:317:13: Unable to assign [undefined] to bool
    qrc:///qml/VPlay/physics/ColliderBase.qml:317:13: Unable to assign [undefined] to bool
    qml: body Box2DBody(0x37569930)
    qml: collidedEntity Wall_QMLTYPE_184(0x374b7eb0, "wall_0")
    qml: collidedEntityType wall
    qml: collidedEntityId wall_0
    qml: body Box2DBody(0x2c2fd1c0)
    qml: collidedEntity QQuickRectangle(0x374b7a78)
    qml: collidedEntityType undefined
    qml: collidedEntityId undefined
    qml: Hit Right Player:  1

     

    Apart from that I keep getting this

    qrc:///qml/VPlay/physics/ColliderBase.qml:317:13: Unable to assign [undefined] to bool

    Every time I create a new entity.

    #14926

    G√ľnther
    V-Play Team

    Hi Caeser!

    Can you make sure that you derived your entities from EntityBase?

    The output

    qml: collidedEntity QQuickRectangle(0x374b78e0)
    

    indicates that the collision happened with a plain QML Rectangle, which does not offer the entityType or entityId properties of EntityBase.

    Let me know this helps!

    Best,
    G√ľnther

    #14927

    Caeser

    Hi Gunther,

    I am actually using EntityBase

    Here is my qml file sample

    EntityBase {
    
        id:enemys
        entityType: "enemy_card" 
         
    
        Rectangle{
    
            id:card
            border.width: 2
            border.color: "#ffffff"
            x: cardX
            radius: 6
            y: cardY
            width: 50
            height: 70
    
            Text {
                id:  text1
                color: "#ffffff"
                text: cardText
                
            }
    
            Image {
                id: bottom_symbol
                width: 18
                height: 18
                x: 15
                y: 44
                source: symbolSource
            }
    
            BoxCollider {
                id:the_collider
                anchors.fill: card
                collisionTestingOnlyMode: false
                categories: Box.Category3
                collidesWith: Box.Category4 | Box.Category1 | Box.Category2
               friction: 0
                bodyType: Body.Dynamic
                bullet: false
                angularDamping: 0
                linearDamping: gameover ? 3 : 0
                fixedRotation: false
    
    
            }
    
    
    
        }
    
    
    
    
    }// EntityBase
    

     

    #14930

    G√ľnther
    V-Play Team

    Hi Caeser,

    I see, thank you for the code!

    While it is true that you are using EntityBase for the entity, the BoxCollider is parented to the Rectangle within the entity, which you get when accessing the other.getBody().target in the collision.

    We recommend to always parent colliders to the entity (no additional items in the hierarchy between collider and entity), which also helps to avoid issues with collider and entity positioning. The representation of the collider within the physics world ignores any displacement applied by additional items in the hierarchy (e.g. the cardX and cardY settings are ignored by the physics world). You can verify this by setting PhysicsWorld::debugDrawVisible to true.

    Best,
    G√ľnther

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