Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setup Ignore Collisions between Body and Motor Colliders?
#5
(30-04-2024, 12:40 PM)josemendez Wrote: It depends on how many characters you need to simulate simultaneously. You'll only ever need to dynamically change the filtering system at runtime if you have more than 16 characters on screen, since that's the maximum amount of categories you can have. Otherwise you can just give a different category to each character (doesn't matter which one), and set its mask to "everything except its own category".

If you really must have more than 16 characters simultaneously (which is probably too many if they all have to be simulated) you can just group them by proximity using a regular grid, for example. Assuming each character has a maximum size/radius of X meters:

- Take the position of the character, divide it by X and take its integer part. This gives you coordinates of a cell the character is in. Characters in cells not adjacent to it are guaranteed to be more than X meters apart, so you know a character can only ever collide with characters in its same cell or cells adjacent to it.

- Whenever a character moves to a new cell (that is, its integer coordinates change), take the categories of all characters found in adjacent cells and assign the first free one to it.

This method allows you to have up to 16 characters per cell neighborhood and is fairly straightforward to implement.

Wonderful, thank you!
I use this code to do CollideWithEverythingExcept(int excludedFlag) and the logic works fine now.
Yes I shouldn't worry too much about the character count since not much characters having simulation on at the same time around the Player.

Code:
        int CollideWithEverythingExcept(int excludedFlag)
        {
            // Ensure the excluded flag is within the valid range (0-15)
            if (excludedFlag < 0 || excludedFlag > 15)
            {
                return -1;
            }

            int allFlags = ObiUtils.CollideWithEverything;

            // Clear the bit corresponding to the excluded flag
            int result = allFlags & ~(1 << excludedFlag);

            return result;
        }

        public void ChangeMotorCategory(int id) {
            if (id == motorCategory) return;

            Debug.Log($"change motor category of {character.name.ColorMe(Color.green)} to {id.ColorMe(Color.yellow)}");
            motorCategory = id;
            var mask = CollideWithEverythingExcept(id);
            motorObiCollider.Filter = ObiUtils.MakeFilter(mask, id);
            //bodies and motor will collide with everything except its own
            if (hasSoftbodies) {
                foreach (var b in softbodieDatas) {
                    //Debug.Log($"set softbody {b.softbody} mask to {mask}");
                    b.softbody.SetFilterCategory(id);
                    b.softbody.SetFilterMask(mask);
                }
            }
        }
Reply


Messages In This Thread
RE: Setup Ignore Collisions between Body and Motor Colliders? - by spikebor - 30-04-2024, 07:15 PM