Forums

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

OverviewV-Play 2 Support (Qt 5)Collision categories in fixture.onContactBegin › Reply To: Collision categories in fixture.onContactBegin
#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, 5 months ago by  GT.

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