Comments (4)
Going to close this for now! Let me know if you have any other issues!
from excalibur.
@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.
@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.
Thanks a lot for your answer! Will deep into docs =)
from excalibur.
Related Issues (20)
- NuGeT package install does not add Excalibur files to .NET project HOT 2
- Add getGlobalOldPos() helper to Actors HOT 2
- Support anchor in pixels HOT 5
- Child entity should inherit transform.coordPlane of parent HOT 1
- Scene transitions may not start in correct state when fixedUpdateFps is set HOT 2
- Feature request - Suppress canvas injection from the Engine constructor HOT 5
- ParticleEmitter ignores z-index HOT 2
- Typo in documentation for Vector.rotate() HOT 1
- Code examples on website are shown in light and dark mode always HOT 5
- Wrong hex color values HOT 2
- excaliburjs.com/docs "Edit this page" link is generated incorrectly HOT 3
- Optimise Side.fromDirection(direction: Vector): Side HOT 1
- Optimise BoundingBox rayCast methods HOT 1
- Optimise BoundingBox.contains(val: any): boolean
- Correct the BoundingBox.intersect(other: BoundingBox): Vector comments positions HOT 3
- Simplify BoundingBox.intersect(other: BoundingBox): Vector A LOT HOT 8
- Tag BoudingBox.getPoints() as a readonly array return HOT 2
- Passing sceneInstance to "Engine.director.getSceneName" does not work it.
- bug(actions): repeatForever doesn't like negative offsets
- v0.29.2 introduces Graphics (Polygon / Circle) glitch in combination with Animations HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from excalibur.