Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Collision Groups in 2D Physics

    2D Blocks
    1
    1
    208
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

      When 2 objects touch with each other, so long as they both have the “object” type (as opposed to “sensor”) and they are not “movable” types, they will collide.

      However, sometimes we need to disable collision between 2 objects. For example, suppose we are shooting balls at some target box. We want all the balls to collide with the box, but we may not want the balls to collide with each other.

      You can use “collision groups” to control the collision between each pair of objects. For each object, you specify 2 things:

      • Group Membership: which collision groups does this object belong to;
      • Group Filter: with which collision groups does this object collide;

       
       

      Setting Group Membership

      You can use the following block to add an object to a collision group or remove this object from a group:

      32ea6316-1e32-4219-b23d-66fbc09653bb-image.png

       
      In the dropdown, you can pick a group number from 0 to 15. So overall there can be at most 16 different groups. In most games, we only need to use 2 to 3 groups.

      You can add an object to more than one group as well. You just need to run the “add to collision group” block multiple times.

      If you do not add an object to any group, then it is assumed to belong to all groups. That is why when we are not using collision groups, all objects in the game would collide with all other objects.

       
       

      Setting Group Filter

      After adding an object to collision groups, we have to specify its group filter, otherwise it will not collide with any object. You can use the following 2 blocks to control which group the object would collide with.

      a5369cc4-7c50-4093-ac53-e44fc9d7c86c-image.png

       
      Again, you can pick from groups 0 to 15 to specify which group this object will collide with.

       
       

      Mutual Selection

      Note that for 2 objects to trigger a collision between them, both of them need to choose to collide with the other’s group.

      For example, suppose we have 2 objects, A in group 0 and B in group 1. They will not collide with each other unless we also set their group filters (who they will collide with).

      Suppose we set A to collide with group 1, it will still not collide with B. That’s because we have not set B to collide with group 0 yet.

      If we set A to collide with group 1, and also set B to collide with group 0, then they will collide when they touch each other.

       
       

      Example 1 - No Collision Groups

      In this project, we have 3 sprites: “blue ball”, “yellow ball” and a box. The box is “fixed”, and the 2 balls are “dynamic”.

      Suppose at the beginning we do not use collision groups in their code:

      blue ball:

      0d33ee81-a5a4-4daf-b732-916452cbb77a-image.png

       
      yellow ball:

      93c75e65-55d6-4f70-b5df-9449e96e5e6b-image.png

       
      box:

      6ac0b7e8-e473-49a0-9189-da8cf5e6376c-image.png

       
      We’ll see that they will collide with each other like this:

      v4.gif

       
       

      Example 2 - Set Group Membership

      Now suppose we set the 2 balls to group 0, and set the box to group 1. Since we have not set their group filters (who they will collide with), these 3 objects will not collide with each other:

      v5.gif

       
       

      Example 3 - Set Group Filters

      Now suppose we set the blue ball and yellow ball to collide with group 1, and set the box to collide with group 0, then the 2 balls will not collide with each other, but both of them will collide with the box:

      v6.gif

       
      Here is the shared project:

      https://play.creaticode.com/projects/1c513a60bc3f07a5a309b4de

       
       

      Collision Group of the World’s 4 Borders

      You can also set the group membership and filter for the 4 borders of the physics world using the following block. Usually, to get precise control over the collision behaviors, it is recommended that you add your own world borders using boxes.

      c5616d15-f969-4996-8a46-c5bcd3accdc1-image.png

      1 Reply Last reply Reply Quote 0
      • First post
        Last post