|
| 1 | +import { computed, Directive, effect, inject, InjectionToken, input } from '@angular/core'; |
| 2 | +import { InteractionGroups } from '@dimforge/rapier3d-compat'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Calculates an InteractionGroup bitmask for use in the `collisionGroups` or `solverGroups` |
| 6 | + * properties of RigidBody or Collider components. The first argument represents a list of |
| 7 | + * groups the entity is in (expressed as numbers from 0 to 15). The second argument is a list |
| 8 | + * of groups that will be filtered against. When it is omitted, all groups are filtered against. |
| 9 | + * |
| 10 | + * @example |
| 11 | + * A RigidBody that is member of group 0 and will collide with everything from groups 0 and 1: |
| 12 | + * |
| 13 | + * ```tsx |
| 14 | + * <RigidBody collisionGroups={interactionGroups([0], [0, 1])} /> |
| 15 | + * ``` |
| 16 | + * |
| 17 | + * A RigidBody that is member of groups 0 and 1 and will collide with everything else: |
| 18 | + * |
| 19 | + * ```tsx |
| 20 | + * <RigidBody collisionGroups={interactionGroups([0, 1])} /> |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * A RigidBody that is member of groups 0 and 1 and will not collide with anything: |
| 24 | + * |
| 25 | + * ```tsx |
| 26 | + * <RigidBody collisionGroups={interactionGroups([0, 1], [])} /> |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * Please note that Rapier needs interaction filters to evaluate to true between _both_ colliding |
| 30 | + * entities for collision events to trigger. |
| 31 | + * |
| 32 | + * @param memberships Groups the collider is a member of. (Values can range from 0 to 15.) |
| 33 | + * @param filters Groups the interaction group should filter against. (Values can range from 0 to 15.) |
| 34 | + * @returns An InteractionGroup bitmask. |
| 35 | + */ |
| 36 | +export function interactionGroups(memberships: number | number[], filters?: number | number[]): InteractionGroups { |
| 37 | + return (bitmask(memberships) << 16) + (filters !== undefined ? bitmask(filters) : 0b1111_1111_1111_1111); |
| 38 | +} |
| 39 | + |
| 40 | +function bitmask(groups: number | number[]): InteractionGroups { |
| 41 | + return [groups].flat().reduce((acc, layer) => acc | (1 << layer), 0); |
| 42 | +} |
| 43 | + |
| 44 | +export const COLLISION_GROUPS_HANDLER = new InjectionToken< |
| 45 | + () => undefined | ((interactionGroups: InteractionGroups) => void) |
| 46 | +>('COLLISION_GROUPS_HANDLER'); |
| 47 | + |
| 48 | +@Directive({ selector: 'ngt-object3D[interactionGroups]' }) |
| 49 | +export class NgtrInteractionGroups { |
| 50 | + inputs = input.required<[number | number[], (number | number[])?]>({ alias: 'interactionGroups' }); |
| 51 | + interactionGroups = computed(() => { |
| 52 | + const [memberships, filters] = this.inputs(); |
| 53 | + return interactionGroups(memberships, filters); |
| 54 | + }); |
| 55 | + |
| 56 | + constructor() { |
| 57 | + const collisionGroupsHandlerFn = inject(COLLISION_GROUPS_HANDLER, { host: true, optional: true }); |
| 58 | + |
| 59 | + effect(() => { |
| 60 | + if (!collisionGroupsHandlerFn) return; |
| 61 | + const handler = collisionGroupsHandlerFn(); |
| 62 | + if (!handler) return; |
| 63 | + handler(this.interactionGroups()); |
| 64 | + }); |
| 65 | + } |
| 66 | +} |
0 commit comments