createRigidBody2D

View sourcecreateRigidBody2D(x?: number, y?: number, a?: number, mass?: number, restitution?: number, kineticfriction?: number, mask?: bigint, group?: bigint): [Position2D, Orientation2D, Scale2D, GlobalTransform2D, Velocity2D, Rotation2D, Acceleration2D, Torque2D, PhysicsHitbox, PhysicsProperties]

Creates the ordered component tuple for a 2D physics body.

The returned tuple contains, in order, the components from createMovable2D: Position2D, Orientation2D, Scale2D, GlobalTransform2D, Velocity2D, Rotation2D, Acceleration2D, and Torque2D; it then adds a fresh PhysicsHitbox and PhysicsProperties. Pass the tuple directly to World#spawn or spread it into a larger component array.

x and y initialize the position, and a is an angle in radians used to initialize the orientation. The movable components otherwise use their zero or identity defaults. A non-zero mass becomes 1 / mass in PhysicsProperties.invmass; a mass of 0 produces invmass = 0, making the body immovable by collision impulses. restitution and kineticfriction default to the current values of PhysicsSettings when the factory is called. mask and group are copied to the body’s collision filter fields.

Every call creates new components. This factory does not add a Collider2D; attach one when the body should participate in collision detection.

Examples

import { World } from '@wimaengine/ecs'
import { Collider2D } from '@wimaengine/narrowphase'
import { createRigidBody2D } from '@wimaengine/physics'

const world = new World()
const body = world.spawn([
  ...createRigidBody2D(10, 20, Math.PI / 4, 2),
  Collider2D.rectangle(2, 1)
])

Parameters

NameDescriptionDefault
x?

Initial x position.

0
y?

Initial y position.

0
a?

Initial orientation in radians.

0
mass?

Mass converted to inverse mass; 0 creates an immovable body.

1
restitution?

Bounciness stored in PhysicsProperties.

PhysicsSettings.restitution
kineticfriction?

Kinetic friction stored in PhysicsProperties.

PhysicsSettings.kineticFriction
mask?

Collision mask bits.

0xFFFFFFFFn
group?

Collision group bits.

0xFFFFFFFFn

Returns

Ordered, fresh components for World#spawn.