Giter VIP home page Giter VIP logo

Comments (4)

eonarheim avatar eonarheim commented on June 23, 2024 1

Going to close this for now! Let me know if you have any other issues!

from excalibur.

eonarheim avatar eonarheim commented on June 23, 2024

@ikiselev1989 This is a great question!

Generally speaking collision groups do not collide with themselves, except for the special built-in group called CollisionGroup.All

In your example the group playersCanCollideWith does collide with the playersGroup, floorGroup, and enemyGroup

console.log(playersCanCollideWith.canCollide(playersGroup)); // true
console.log(playersCanCollideWith.canCollide(floorGroup)); // true
console.log(playersCanCollideWith.canCollide(enemyGroup)); // true

A way to think about collision groups is as different "categories" (other engines like Unity call collision groups "layers"). Each group is represented by a 32 bit integer represented by a power of 2. CollisionGroupManager works by assigning a unique power of 2 to each group (meaning only 32 groups are possible).

So the groups in binary in your example are:

playersGroup = 0b0001
npcGroup     = 0b0010
floorGroup   = 0b0100
enemyGroup   = 0b1000

Each group also contains a mask, this mask has the bit position of the groups it can collided with set to 1. The player group is the least significant bit (furthest right) if a mask has that bit set to 1 it means it collides with the player group.

By default the mask for a collision group is set to collide with every group apart from itself.

playersGroupMask = 0b11111111_11111111_11111111_11111110
npcGroupMask     = 0b11111111_11111111_11111111_11111101
floorGroupMask   = 0b11111111_11111111_11111111_11111011
enemyGroupMask   = 0b11111111_11111111_11111111_11110111

The canCollide logic basically performs a bitwise AND between the group and the target layer's mask, so if we say playersGroup.canCollide(playersGroup) that is 0b0001 & 0b11111111_11111111_11111111_11111110 = 0 meaning they can't collide.

The CollisionGroup.collidesWith(...) helper makes a new group that is all of the categories OR'd together then invertedSo in the example above creating the const playersCanCollideWith = CollisionGroup.collidesWith([ playersGroup, floorGroup, enemyGroup, ]) group

playersCanCollideWith = ~(0b0001 | 0b0100 | 0b1000) = 0b0010
playersCanCollideWithMask = ~(0b0010) = 0b11111111_11111111_11111111_11111101

Notice the final group is actually equivalent to the npcGroup, which has the mask bits set for player, floor, and enemy. This means playersCanCollideWith can collide with player, floor, and enemy.

Apologies for the long explanation, hopefully that makes sense.

Documentation/API Question

I'll endeavor to improve the documentation here, and perhaps Excalibur needs to think about the API because it causes confusion. Perhaps we should rename the type CollisionGroup to something like CollisionLayer or maybe retool the API to provide more clarity? Open to suggestions! @jedeen @kamranayub @alanag13 Thoughts?

from excalibur.

eonarheim avatar eonarheim commented on June 23, 2024

@ikiselev1989 I've updated the docs with a more thorough explaination https://excaliburjs.com/docs/collisiongroups/

The team also spoke offline and we are potentially renaming this feature to CollisionFilter

from excalibur.

ikiselev1989 avatar ikiselev1989 commented on June 23, 2024

Thanks a lot for your answer! Will deep into docs =)

from excalibur.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.