Games [XUI2D] To Collide or Not To Collide

Discussion in 'Game Development' started by Erel, Sep 12, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User


    The purpose of this example is to demonstrate the usage of mask bits and category bits properties.

    Collision rules:

    - Collisions happen between dynamic bodies and other bodies (including other dynamic bodies).
    - The collisions actually happen between two fixtures of two bodies. Bodies can have multiple fixtures with different properties.
    - If a fixture is set to be a sensor (is sensor property) then the collision will be detected but there will not be a physical collision.

    Now for mask bits and category bits. The formal rule sounds a bit complicated so we will start with an example.
    The default mask bits is 65535 (0xFFFF).
    0xFFFF in base 2 is 1111 1111 1111 1111.
    Category bits should be a power of 2. This means that it is a number with a single high (1) bit. The default value is 1.

    If the same bit is high in both the mask bits and the category bits then we have a collision.

    In our example the second ground body mask bits value is 0 = 0000 0000 0000 0000. This means that it will never collide with other bodies. It is better to set the mask bits to 0 than using a sensor unless you are actually interested in detecting the collision.

    The third ground body mask bits value is 65529 = 0xFFFF - 4 - 2 = 1111 1111 1111 1001
    This means that it will collide with all bodies except bodies with category bits of 2 (10) or 4 (100).

    The formal rule is:
    For a collision to happen: (FixtureA.MaskBits & FixtureB.CategoryBits) <> 0 AND (FixtureA.CategoryBits & FixtureB.MaskBits) <> 0.
    & - bitwise and.

    Note that you can change the filter bits at runtime:
    The above code is from Mario example. When Mario is hit we want to let Mario fall out of the screen.

    There are two ways to detect collisions:
    1. Call Body.GetContactList to get the current contacts. In most cases you will want to pass True for the TouchingOnly parameter. The items in the list returned are B2Contact items.

    2. Handle the world contact events: BeginContact, PreSolve, PostSolve and EndContact. Note that PreSolve and PostSolve are not called for sensors.
    The events are a bit more complicated to handle but are more powerful as they allow you to modify the contact behavior and they also include more information about the contact. See the walking character example.
    The world is "locked" during these events. Don't add or destroy anything during these events. You can use X2.AddFutureTask to run something after the events complete.
    Nice tutorial about box2d collisions:

    The example is included in the examples pack:
    Last edited: Sep 12, 2018
  2. Peter Simpson

    Peter Simpson Expert Licensed User

    I had to read this 3 times, now it's time to make a cuppa tea, and then I'll read it again.

    Thank you for the info @Erel...
    Last edited: Sep 12, 2018
    Toky Olivier likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Run the project. Reading is not enough...
  4. inakigarm

    inakigarm Well-Known Member Licensed User

    Thanks for this example !; I was thinking to open a Post asking for this (I had read but didn't understand well)
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice