Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for group property on bodies #82

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/bodies/circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ export class Circle extends SATCircle implements BBox, BodyProps {
return this.scale;
}

/**
* group for collision filtering
*/
// Don't overwrite docs from BodyProps
get group(): number {
return this._group;
}
Expand Down
4 changes: 1 addition & 3 deletions src/bodies/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ export class Polygon extends SATPolygon implements BBox, BodyProps {
this.setScale(scale);
}

/**
* group for collision filtering
*/
// Don't overwrite docs from BodyProps
get group(): number {
return this._group;
}
Expand Down
14 changes: 14 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { Polygon } from "./bodies/polygon";
// version 4.0.0 1=1 copy
import RBush from "./external/rbush";

// Import type for documentation linking
import type { canInteract } from "./utils";

export {
Polygon as DecompPolygon,
Point as DecompPoint,
Expand Down Expand Up @@ -109,6 +112,17 @@ export interface BodyOptions {

/**
* group for collision filtering
*
* Based on {@link https://box2d.org/documentation/md_simulation.html#filtering Box2D}
* ({@link https://aurelienribon.wordpress.com/2011/07/01/box2d-tutorial-collision-filtering/ tutorial})
*
* Values in {@link BodyGroup} are predefined and used each the body type and
* should not be used for custom filtering
*
* `0b00000001 << 16` to `0b01000000 << 16` (max 0x7fffffff) are free to use for custom groups
*
* @see {@link canInteract} for how groups are used
* @default 0x7fffffff // member of all groups (can interact with everyting)
*/
group?: number;
}
Expand Down
30 changes: 30 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export function extendBody(body: Body, options: BodyOptions = {}): void {
body.isStatic = !!options.isStatic;
body.isTrigger = !!options.isTrigger;
body.padding = options.padding || 0;
// Default value should be reflected in documentation of `BodyOptions.group`
body.group = options.group ?? 0x7fffffff;

if (body.typeGroup !== BodyGroup.Circle && options.isCentered) {
Expand Down Expand Up @@ -217,8 +218,37 @@ export function intersectAABB(bodyA: BBox, bodyB: BBox): boolean {
/**
* checks if two bodies can interact (for collision filtering)
*
* Based on {@link https://box2d.org/documentation/md_simulation.html#filtering Box2D}
* ({@link https://aurelienribon.wordpress.com/2011/07/01/box2d-tutorial-collision-filtering/ tutorial})
*
* @param bodyA
* @param bodyB
*
* @example
* const body1 = { group: 0b00000000_00000000_00000001_00000000 }
* const body2 = { group: 0b11111111_11111111_00000011_00000000 }
* const body3 = { group: 0b00000010_00000000_00000100_00000000 }
*
* // Body 1 has the first custom group but cannot interact with any other groups
* // except itself because the first 16 bits are all zeros, only bodies with an
* // identical value can interact with it.
* canInteract(body1, body1) // returns true (identical groups can always interact)
* canInteract(body1, body2) // returns false
* canInteract(body1, body3) // returns false
*
* // Body 2 has the first and second group and can interact with all other
* // groups, but only if that body also can interact with is custom group.
* canInteract(body2, body1) // returns false (body1 cannot interact with others)
* canInteract(body2, body2) // returns true (identical groups can always interact)
* canInteract(body2, body3) // returns true
*
* // Body 3 has the third group but can interact with the second group.
* // This means that Body 2 and Body 3 can interact with each other but no other
* // body can interact with Body 1 because it doesn't allow interactions with
* // any other custom group.
* canInteract(body3, body1) // returns false (body1 cannot interact with others)
* canInteract(body3, body2) // returns true
* canInteract(body3, body3) // returns true (identical groups can always interact)
*/
export function canInteract(
{ group: groupA }: Body,
Expand Down
Loading