PhysicsProperties

View source

Stores the collision and mass properties for a physics body component.

Attach PhysicsProperties alongside a Collider2D to describe how a body participates in collision detection and response. Inverse mass and inverse inertia control linear and angular response. A zero inverse mass makes the body immovable by collision impulses. A zero inverse inertia prevents angular response.

group and mask are BigInt bit fields. Two bodies pass the collision filter only when both intersections are non-zero: a.group & b.mask and b.group & a.mask. The default values enable all lower 32 filter bits. sleep causes a pair to be skipped when both bodies are sleeping. For an accepted contact, the lower restitution and lower kinetic-friction value from the pair is used.

A new instance has these defaults:

  • invinertia: 0
  • invmass: 0
  • mask: 0xffffffffn
  • group: 0xffffffffn
  • sleep: false
  • restitution: 0.7
  • kineticFriction: 0.6.

The component stores the supplied values as-is; it does not clamp material values or validate physical ranges.

Examples

import { World } from '@wimaengine/ecs'
import { Collider2D, PhysicsProperties } from '@wimaengine/narrowphase'

const world = new World()
const properties = new PhysicsProperties()
properties.invmass = 1 / 2
properties.group = 0b01n
properties.mask = 0b01n
properties.restitution = 0.4

const body = world.spawn([
  Collider2D.rectangle(1, 1),
  properties
])

Constructor


constructor()

Properties


invinertia: number

View source

Inverse moment of inertia used for angular collision response.

Default: 0

invmass: number

View source

Inverse mass used for linear collision response; 0 makes a body immovable by collision impulses.

Default: 0

mask: bigint

View source

Collision-category bits tested against another body’s group.

Default: 0xffffffffn

group: bigint

View source

Collision-category bits tested against another body’s mask.

Default: 0xffffffffn

sleep: boolean

View source

Whether this body is sleeping; two sleeping bodies are skipped as a pair.

Default: false

restitution: number

View source

Bounciness used by collision response; contacts use the lower pair value.

Default: 0.7

kineticFriction: number

View source

Kinetic friction used by collision response; contacts use the lower pair value.

Default: 0.6

Methods


staticcopy(source: PhysicsProperties, target?: PhysicsProperties): PhysicsProperties

View source

Copies all seven fields from source into target.

When target is omitted, the method creates a new PhysicsProperties. Values are assigned as-is, including BigInt filter fields.

Parameters

NameDescriptionDefault
source

Properties to copy.

—
target?

Object to update.

...

Returns

The updated target.

staticclone(target: PhysicsProperties): PhysicsProperties

View source

Creates a new component with the same seven field values as target.

Parameters

NameDescriptionDefault
target

Properties to clone.

—

Returns

A new component containing the copied values.

staticserialize(value: PhysicsProperties): PhysicsPropertiesSerial

View source

Converts a component to its plain serialized field object.

BigInt mask and group values remain BigInt values; this method does not convert them to numbers or strings.

Parameters

NameDescriptionDefault
value

Component to serialize.

—

Returns

The seven serialized fields.

staticdeserialize(value: PhysicsPropertiesSerial, out?: PhysicsProperties): PhysicsProperties

View source

Writes serialized fields into a component.

The method does not validate value before reading it. Call PhysicsProperties.validateSerial first when input comes from an external source. If out is omitted, a new component is created; otherwise the supplied component is updated and returned.

Parameters

NameDescriptionDefault
value

Serialized fields to read.

—
out?

Component to update.

...

Returns

The updated output component.

staticvalidateSerial(value: unknown): value is PhysicsPropertiesSerial

View source

Checks whether a non-null object has all serialized fields with the required primitive types.

invinertia, invmass, restitution, and kineticFriction must be numbers. mask and group must be BigInts, and sleep must be a boolean. The check does not enforce finite numbers, bit width, sign, or any other physical constraints. It checks field presence with the in operator, so inherited fields also satisfy the presence check.

Parameters

NameDescriptionDefault
value

Candidate serialized value.

—

Returns

true when all seven fields are present with the required types.