chore: 添加第三方依赖库

This commit is contained in:
yhh
2025-12-03 16:24:08 +08:00
parent cb1b171216
commit 83aee02540
172 changed files with 27480 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
import {RawCCDSolver} from "../raw";
/**
* The CCD solver responsible for resolving Continuous Collision Detection.
*
* To avoid leaking WASM resources, this MUST be freed manually with `ccdSolver.free()`
* once you are done using it.
*/
export class CCDSolver {
raw: RawCCDSolver;
/**
* Release the WASM memory occupied by this narrow-phase.
*/
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
}
constructor(raw?: RawCCDSolver) {
this.raw = raw || new RawCCDSolver();
}
}

View File

@@ -0,0 +1,13 @@
/**
* A rule applied to combine coefficients.
*
* Use this when configuring the `ColliderDesc` to specify
* how friction and restitution coefficient should be combined
* in a contact.
*/
export enum CoefficientCombineRule {
Average = 0,
Min = 1,
Multiply = 2,
Max = 3,
}

View File

@@ -0,0 +1,681 @@
import {Rotation, Vector, VectorOps, RotationOps} from "../math";
import {
RawGenericJoint,
RawImpulseJointSet,
RawRigidBodySet,
RawJointAxis,
RawJointType,
RawMotorModel,
} from "../raw";
import {RigidBody, RigidBodyHandle} from "./rigid_body";
import {RigidBodySet} from "./rigid_body_set";
// #if DIM3
import {Quaternion} from "../math";
// #endif
/**
* The integer identifier of a collider added to a `ColliderSet`.
*/
export type ImpulseJointHandle = number;
/**
* An enum grouping all possible types of joints:
*
* - `Revolute`: A revolute joint that removes all degrees of freedom between the affected
* bodies except for the rotation along one axis.
* - `Fixed`: A fixed joint that removes all relative degrees of freedom between the affected bodies.
* - `Prismatic`: A prismatic joint that removes all degrees of freedom between the affected
* bodies except for the translation along one axis.
* - `Spherical`: (3D only) A spherical joint that removes all relative linear degrees of freedom between the affected bodies.
* - `Generic`: (3D only) A joint with customizable degrees of freedom, allowing any of the 6 axes to be locked.
*/
export enum JointType {
Revolute,
Fixed,
Prismatic,
Rope,
Spring,
// #if DIM3
Spherical,
Generic,
// #endif
}
export enum MotorModel {
AccelerationBased,
ForceBased,
}
/**
* An enum representing the possible joint axes of a generic joint.
* They can be ORed together, like:
* JointAxesMask.LinX || JointAxesMask.LinY
* to get a joint that is only free in the X and Y translational (positional) axes.
*
* Possible free axes are:
*
* - `X`: X translation axis
* - `Y`: Y translation axis
* - `Z`: Z translation axis
* - `AngX`: X angular rotation axis
* - `AngY`: Y angular rotations axis
* - `AngZ`: Z angular rotation axis
*/
export enum JointAxesMask {
LinX = 1 << 0,
LinY = 1 << 1,
LinZ = 1 << 2,
AngX = 1 << 3,
AngY = 1 << 4,
AngZ = 1 << 5,
}
export class ImpulseJoint {
protected rawSet: RawImpulseJointSet; // The ImpulseJoint won't need to free this.
protected bodySet: RigidBodySet; // The ImpulseJoint wont need to free this.
handle: ImpulseJointHandle;
constructor(
rawSet: RawImpulseJointSet,
bodySet: RigidBodySet,
handle: ImpulseJointHandle,
) {
this.rawSet = rawSet;
this.bodySet = bodySet;
this.handle = handle;
}
public static newTyped(
rawSet: RawImpulseJointSet,
bodySet: RigidBodySet,
handle: ImpulseJointHandle,
): ImpulseJoint {
switch (rawSet.jointType(handle)) {
case RawJointType.Revolute:
return new RevoluteImpulseJoint(rawSet, bodySet, handle);
case RawJointType.Prismatic:
return new PrismaticImpulseJoint(rawSet, bodySet, handle);
case RawJointType.Fixed:
return new FixedImpulseJoint(rawSet, bodySet, handle);
case RawJointType.Spring:
return new SpringImpulseJoint(rawSet, bodySet, handle);
case RawJointType.Rope:
return new RopeImpulseJoint(rawSet, bodySet, handle);
// #if DIM3
case RawJointType.Spherical:
return new SphericalImpulseJoint(rawSet, bodySet, handle);
case RawJointType.Generic:
return new GenericImpulseJoint(rawSet, bodySet, handle);
// #endif
default:
return new ImpulseJoint(rawSet, bodySet, handle);
}
}
/** @internal */
public finalizeDeserialization(bodySet: RigidBodySet) {
this.bodySet = bodySet;
}
/**
* Checks if this joint is still valid (i.e. that it has
* not been deleted from the joint set yet).
*/
public isValid(): boolean {
return this.rawSet.contains(this.handle);
}
/**
* The first rigid-body this joint it attached to.
*/
public body1(): RigidBody {
return this.bodySet.get(this.rawSet.jointBodyHandle1(this.handle));
}
/**
* The second rigid-body this joint is attached to.
*/
public body2(): RigidBody {
return this.bodySet.get(this.rawSet.jointBodyHandle2(this.handle));
}
/**
* The type of this joint given as a string.
*/
public type(): JointType {
return this.rawSet.jointType(this.handle) as number as JointType;
}
// #if DIM3
/**
* The rotation quaternion that aligns this joint's first local axis to the `x` axis.
*/
public frameX1(): Rotation {
return RotationOps.fromRaw(this.rawSet.jointFrameX1(this.handle));
}
// #endif
// #if DIM3
/**
* The rotation matrix that aligns this joint's second local axis to the `x` axis.
*/
public frameX2(): Rotation {
return RotationOps.fromRaw(this.rawSet.jointFrameX2(this.handle));
}
// #endif
/**
* The position of the first anchor of this joint.
*
* The first anchor gives the position of the application point on the
* local frame of the first rigid-body it is attached to.
*/
public anchor1(): Vector {
return VectorOps.fromRaw(this.rawSet.jointAnchor1(this.handle));
}
/**
* The position of the second anchor of this joint.
*
* The second anchor gives the position of the application point on the
* local frame of the second rigid-body it is attached to.
*/
public anchor2(): Vector {
return VectorOps.fromRaw(this.rawSet.jointAnchor2(this.handle));
}
/**
* Sets the position of the first anchor of this joint.
*
* The first anchor gives the position of the application point on the
* local frame of the first rigid-body it is attached to.
*/
public setAnchor1(newPos: Vector) {
const rawPoint = VectorOps.intoRaw(newPos);
this.rawSet.jointSetAnchor1(this.handle, rawPoint);
rawPoint.free();
}
/**
* Sets the position of the second anchor of this joint.
*
* The second anchor gives the position of the application point on the
* local frame of the second rigid-body it is attached to.
*/
public setAnchor2(newPos: Vector) {
const rawPoint = VectorOps.intoRaw(newPos);
this.rawSet.jointSetAnchor2(this.handle, rawPoint);
rawPoint.free();
}
/**
* Controls whether contacts are computed between colliders attached
* to the rigid-bodies linked by this joint.
*/
public setContactsEnabled(enabled: boolean) {
this.rawSet.jointSetContactsEnabled(this.handle, enabled);
}
/**
* Indicates if contacts are enabled between colliders attached
* to the rigid-bodies linked by this joint.
*/
public contactsEnabled(): boolean {
return this.rawSet.jointContactsEnabled(this.handle);
}
}
export class UnitImpulseJoint extends ImpulseJoint {
/**
* The axis left free by this joint.
*/
protected rawAxis?(): RawJointAxis;
/**
* Are the limits enabled for this joint?
*/
public limitsEnabled(): boolean {
return this.rawSet.jointLimitsEnabled(this.handle, this.rawAxis());
}
/**
* The min limit of this joint.
*/
public limitsMin(): number {
return this.rawSet.jointLimitsMin(this.handle, this.rawAxis());
}
/**
* The max limit of this joint.
*/
public limitsMax(): number {
return this.rawSet.jointLimitsMax(this.handle, this.rawAxis());
}
/**
* Sets the limits of this joint.
*
* @param min - The minimum bound of this joints free coordinate.
* @param max - The maximum bound of this joints free coordinate.
*/
public setLimits(min: number, max: number) {
this.rawSet.jointSetLimits(this.handle, this.rawAxis(), min, max);
}
public configureMotorModel(model: MotorModel) {
this.rawSet.jointConfigureMotorModel(
this.handle,
this.rawAxis(),
model as number as RawMotorModel,
);
}
public configureMotorVelocity(targetVel: number, factor: number) {
this.rawSet.jointConfigureMotorVelocity(
this.handle,
this.rawAxis(),
targetVel,
factor,
);
}
public configureMotorPosition(
targetPos: number,
stiffness: number,
damping: number,
) {
this.rawSet.jointConfigureMotorPosition(
this.handle,
this.rawAxis(),
targetPos,
stiffness,
damping,
);
}
public configureMotor(
targetPos: number,
targetVel: number,
stiffness: number,
damping: number,
) {
this.rawSet.jointConfigureMotor(
this.handle,
this.rawAxis(),
targetPos,
targetVel,
stiffness,
damping,
);
}
}
export class FixedImpulseJoint extends ImpulseJoint {}
export class RopeImpulseJoint extends ImpulseJoint {}
export class SpringImpulseJoint extends ImpulseJoint {}
export class PrismaticImpulseJoint extends UnitImpulseJoint {
public rawAxis(): RawJointAxis {
return RawJointAxis.LinX;
}
}
export class RevoluteImpulseJoint extends UnitImpulseJoint {
public rawAxis(): RawJointAxis {
return RawJointAxis.AngX;
}
}
// #if DIM3
export class GenericImpulseJoint extends ImpulseJoint {}
export class SphericalImpulseJoint extends ImpulseJoint {
/* Unsupported by this alpha release.
public configureMotorModel(model: MotorModel) {
this.rawSet.jointConfigureMotorModel(this.handle, model);
}
public configureMotorVelocity(targetVel: Vector, factor: number) {
this.rawSet.jointConfigureBallMotorVelocity(this.handle, targetVel.x, targetVel.y, targetVel.z, factor);
}
public configureMotorPosition(targetPos: Quaternion, stiffness: number, damping: number) {
this.rawSet.jointConfigureBallMotorPosition(this.handle, targetPos.w, targetPos.x, targetPos.y, targetPos.z, stiffness, damping);
}
public configureMotor(targetPos: Quaternion, targetVel: Vector, stiffness: number, damping: number) {
this.rawSet.jointConfigureBallMotor(this.handle,
targetPos.w, targetPos.x, targetPos.y, targetPos.z,
targetVel.x, targetVel.y, targetVel.z,
stiffness, damping);
}
*/
}
// #endif
export class JointData {
anchor1: Vector;
anchor2: Vector;
axis: Vector;
frame1: Rotation;
frame2: Rotation;
jointType: JointType;
limitsEnabled: boolean;
limits: Array<number>;
axesMask: JointAxesMask;
stiffness: number;
damping: number;
length: number;
private constructor() {}
/**
* Creates a new joint descriptor that builds a Fixed joint.
*
* A fixed joint removes all the degrees of freedom between the affected bodies, ensuring their
* anchor and local frames coincide in world-space.
*
* @param anchor1 - Point where the joint is attached on the first rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param frame1 - The reference orientation of the joint wrt. the first rigid-body.
* @param anchor2 - Point where the joint is attached on the second rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param frame2 - The reference orientation of the joint wrt. the second rigid-body.
*/
public static fixed(
anchor1: Vector,
frame1: Rotation,
anchor2: Vector,
frame2: Rotation,
): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.frame1 = frame1;
res.frame2 = frame2;
res.jointType = JointType.Fixed;
return res;
}
public static spring(
rest_length: number,
stiffness: number,
damping: number,
anchor1: Vector,
anchor2: Vector,
): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.length = rest_length;
res.stiffness = stiffness;
res.damping = damping;
res.jointType = JointType.Spring;
return res;
}
public static rope(
length: number,
anchor1: Vector,
anchor2: Vector,
): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.length = length;
res.jointType = JointType.Rope;
return res;
}
// #if DIM2
/**
* Create a new joint descriptor that builds revolute joints.
*
* A revolute joint allows three relative rotational degrees of freedom
* by preventing any relative translation between the anchors of the
* two attached rigid-bodies.
*
* @param anchor1 - Point where the joint is attached on the first rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param anchor2 - Point where the joint is attached on the second rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
*/
public static revolute(anchor1: Vector, anchor2: Vector): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.jointType = JointType.Revolute;
return res;
}
/**
* Creates a new joint descriptor that builds a Prismatic joint.
*
* A prismatic joint removes all the degrees of freedom between the
* affected bodies, except for the translation along one axis.
*
* @param anchor1 - Point where the joint is attached on the first rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param anchor2 - Point where the joint is attached on the second rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param axis - Axis of the joint, expressed in the local-space of the rigid-bodies it is attached to.
*/
public static prismatic(
anchor1: Vector,
anchor2: Vector,
axis: Vector,
): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.axis = axis;
res.jointType = JointType.Prismatic;
return res;
}
// #endif
// #if DIM3
/**
* Create a new joint descriptor that builds generic joints.
*
* A generic joint allows customizing its degrees of freedom
* by supplying a mask of the joint axes that should remain locked.
*
* @param anchor1 - Point where the joint is attached on the first rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param anchor2 - Point where the joint is attached on the second rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param axis - The X axis of the joint, expressed in the local-space of the rigid-bodies it is attached to.
* @param axesMask - Mask representing the locked axes of the joint. You can use logical OR to select these from
* the JointAxesMask enum. For example, passing (JointAxesMask.AngX || JointAxesMask.AngY) will
* create a joint locked in the X and Y rotational axes.
*/
public static generic(
anchor1: Vector,
anchor2: Vector,
axis: Vector,
axesMask: JointAxesMask,
): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.axis = axis;
res.axesMask = axesMask;
res.jointType = JointType.Generic;
return res;
}
/**
* Create a new joint descriptor that builds spherical joints.
*
* A spherical joint allows three relative rotational degrees of freedom
* by preventing any relative translation between the anchors of the
* two attached rigid-bodies.
*
* @param anchor1 - Point where the joint is attached on the first rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param anchor2 - Point where the joint is attached on the second rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
*/
public static spherical(anchor1: Vector, anchor2: Vector): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.jointType = JointType.Spherical;
return res;
}
/**
* Creates a new joint descriptor that builds a Prismatic joint.
*
* A prismatic joint removes all the degrees of freedom between the
* affected bodies, except for the translation along one axis.
*
* @param anchor1 - Point where the joint is attached on the first rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param anchor2 - Point where the joint is attached on the second rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param axis - Axis of the joint, expressed in the local-space of the rigid-bodies it is attached to.
*/
public static prismatic(
anchor1: Vector,
anchor2: Vector,
axis: Vector,
): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.axis = axis;
res.jointType = JointType.Prismatic;
return res;
}
/**
* Create a new joint descriptor that builds Revolute joints.
*
* A revolute joint removes all degrees of freedom between the affected
* bodies except for the rotation along one axis.
*
* @param anchor1 - Point where the joint is attached on the first rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param anchor2 - Point where the joint is attached on the second rigid-body affected by this joint. Expressed in the
* local-space of the rigid-body.
* @param axis - Axis of the joint, expressed in the local-space of the rigid-bodies it is attached to.
*/
public static revolute(
anchor1: Vector,
anchor2: Vector,
axis: Vector,
): JointData {
let res = new JointData();
res.anchor1 = anchor1;
res.anchor2 = anchor2;
res.axis = axis;
res.jointType = JointType.Revolute;
return res;
}
// #endif
public intoRaw(): RawGenericJoint {
let rawA1 = VectorOps.intoRaw(this.anchor1);
let rawA2 = VectorOps.intoRaw(this.anchor2);
let rawAx;
let result;
let limitsEnabled = false;
let limitsMin = 0.0;
let limitsMax = 0.0;
switch (this.jointType) {
case JointType.Fixed:
let rawFra1 = RotationOps.intoRaw(this.frame1);
let rawFra2 = RotationOps.intoRaw(this.frame2);
result = RawGenericJoint.fixed(rawA1, rawFra1, rawA2, rawFra2);
rawFra1.free();
rawFra2.free();
break;
case JointType.Spring:
result = RawGenericJoint.spring(
this.length,
this.stiffness,
this.damping,
rawA1,
rawA2,
);
break;
case JointType.Rope:
result = RawGenericJoint.rope(this.length, rawA1, rawA2);
break;
case JointType.Prismatic:
rawAx = VectorOps.intoRaw(this.axis);
if (!!this.limitsEnabled) {
limitsEnabled = true;
limitsMin = this.limits[0];
limitsMax = this.limits[1];
}
// #if DIM2
result = RawGenericJoint.prismatic(
rawA1,
rawA2,
rawAx,
limitsEnabled,
limitsMin,
limitsMax,
);
// #endif
// #if DIM3
result = RawGenericJoint.prismatic(
rawA1,
rawA2,
rawAx,
limitsEnabled,
limitsMin,
limitsMax,
);
// #endif
rawAx.free();
break;
// #if DIM2
case JointType.Revolute:
result = RawGenericJoint.revolute(rawA1, rawA2);
break;
// #endif
// #if DIM3
case JointType.Generic:
rawAx = VectorOps.intoRaw(this.axis);
// implicit type cast: axesMask is a JointAxesMask bitflag enum,
// we're treating it as a u8 on the Rust side
let rawAxesMask = this.axesMask;
result = RawGenericJoint.generic(
rawA1,
rawA2,
rawAx,
rawAxesMask,
);
break;
case JointType.Spherical:
result = RawGenericJoint.spherical(rawA1, rawA2);
break;
case JointType.Revolute:
rawAx = VectorOps.intoRaw(this.axis);
result = RawGenericJoint.revolute(rawA1, rawA2, rawAx);
rawAx.free();
break;
// #endif
}
rawA1.free();
rawA2.free();
return result;
}
}

View File

@@ -0,0 +1,165 @@
import {RawImpulseJointSet} from "../raw";
import {Coarena} from "../coarena";
import {RigidBodySet} from "./rigid_body_set";
import {
RevoluteImpulseJoint,
FixedImpulseJoint,
ImpulseJoint,
ImpulseJointHandle,
JointData,
JointType,
PrismaticImpulseJoint,
// #if DIM3
SphericalImpulseJoint,
// #endif
} from "./impulse_joint";
import {IslandManager} from "./island_manager";
import {RigidBodyHandle} from "./rigid_body";
import {Collider, ColliderHandle} from "../geometry";
/**
* A set of joints.
*
* To avoid leaking WASM resources, this MUST be freed manually with `jointSet.free()`
* once you are done using it (and all the joints it created).
*/
export class ImpulseJointSet {
raw: RawImpulseJointSet;
private map: Coarena<ImpulseJoint>;
/**
* Release the WASM memory occupied by this joint set.
*/
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
if (!!this.map) {
this.map.clear();
}
this.map = undefined;
}
constructor(raw?: RawImpulseJointSet) {
this.raw = raw || new RawImpulseJointSet();
this.map = new Coarena<ImpulseJoint>();
// Initialize the map with the existing elements, if any.
if (raw) {
raw.forEachJointHandle((handle: ImpulseJointHandle) => {
this.map.set(handle, ImpulseJoint.newTyped(raw, null, handle));
});
}
}
/** @internal */
public finalizeDeserialization(bodies: RigidBodySet) {
this.map.forEach((joint) => joint.finalizeDeserialization(bodies));
}
/**
* Creates a new joint and return its integer handle.
*
* @param bodies - The set of rigid-bodies containing the bodies the joint is attached to.
* @param desc - The joint's parameters.
* @param parent1 - The handle of the first rigid-body this joint is attached to.
* @param parent2 - The handle of the second rigid-body this joint is attached to.
* @param wakeUp - Should the attached rigid-bodies be awakened?
*/
public createJoint(
bodies: RigidBodySet,
desc: JointData,
parent1: RigidBodyHandle,
parent2: RigidBodyHandle,
wakeUp: boolean,
): ImpulseJoint {
const rawParams = desc.intoRaw();
const handle = this.raw.createJoint(
rawParams,
parent1,
parent2,
wakeUp,
);
rawParams.free();
let joint = ImpulseJoint.newTyped(this.raw, bodies, handle);
this.map.set(handle, joint);
return joint;
}
/**
* Remove a joint from this set.
*
* @param handle - The integer handle of the joint.
* @param wakeUp - If `true`, the rigid-bodies attached by the removed joint will be woken-up automatically.
*/
public remove(handle: ImpulseJointHandle, wakeUp: boolean) {
this.raw.remove(handle, wakeUp);
this.unmap(handle);
}
/**
* Calls the given closure with the integer handle of each impulse joint attached to this rigid-body.
*
* @param f - The closure called with the integer handle of each impulse joint attached to the rigid-body.
*/
public forEachJointHandleAttachedToRigidBody(
handle: RigidBodyHandle,
f: (handle: ImpulseJointHandle) => void,
) {
this.raw.forEachJointAttachedToRigidBody(handle, f);
}
/**
* Internal function, do not call directly.
* @param handle
*/
public unmap(handle: ImpulseJointHandle) {
this.map.delete(handle);
}
/**
* The number of joints on this set.
*/
public len(): number {
return this.map.len();
}
/**
* Does this set contain a joint with the given handle?
*
* @param handle - The joint handle to check.
*/
public contains(handle: ImpulseJointHandle): boolean {
return this.get(handle) != null;
}
/**
* Gets the joint with the given handle.
*
* Returns `null` if no joint with the specified handle exists.
*
* @param handle - The integer handle of the joint to retrieve.
*/
public get(handle: ImpulseJointHandle): ImpulseJoint | null {
return this.map.get(handle);
}
/**
* Applies the given closure to each joint contained by this set.
*
* @param f - The closure to apply.
*/
public forEach(f: (joint: ImpulseJoint) => void) {
this.map.forEach(f);
}
/**
* Gets all joints in the list.
*
* @returns joint list.
*/
public getAll(): ImpulseJoint[] {
return this.map.getAll();
}
}

View File

@@ -0,0 +1,10 @@
export * from "./rigid_body";
export * from "./rigid_body_set";
export * from "./integration_parameters";
export * from "./impulse_joint";
export * from "./impulse_joint_set";
export * from "./multibody_joint";
export * from "./multibody_joint_set";
export * from "./coefficient_combine_rule";
export * from "./ccd_solver";
export * from "./island_manager";

View File

@@ -0,0 +1,126 @@
import {RawIntegrationParameters} from "../raw";
export class IntegrationParameters {
raw: RawIntegrationParameters;
constructor(raw?: RawIntegrationParameters) {
this.raw = raw || new RawIntegrationParameters();
}
/**
* Free the WASM memory used by these integration parameters.
*/
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
}
/**
* The timestep length (default: `1.0 / 60.0`)
*/
get dt(): number {
return this.raw.dt;
}
/**
* The Error Reduction Parameter in `[0, 1]` is the proportion of
* the positional error to be corrected at each time step (default: `0.2`).
*/
get contact_erp(): number {
return this.raw.contact_erp;
}
get lengthUnit(): number {
return this.raw.lengthUnit;
}
/**
* Normalized amount of penetration the engine wont attempt to correct (default: `0.001m`).
*
* This threshold considered by the physics engine is this value multiplied by the `lengthUnit`.
*/
get normalizedAllowedLinearError(): number {
return this.raw.normalizedAllowedLinearError;
}
/**
* The maximal normalized distance separating two objects that will generate predictive contacts (default: `0.002`).
*
* This threshold considered by the physics engine is this value multiplied by the `lengthUnit`.
*/
get normalizedPredictionDistance(): number {
return this.raw.normalizedPredictionDistance;
}
/**
* The number of solver iterations run by the constraints solver for calculating forces (default: `4`).
*/
get numSolverIterations(): number {
return this.raw.numSolverIterations;
}
/**
* Number of internal Project Gauss Seidel (PGS) iterations run at each solver iteration (default: `1`).
*/
get numInternalPgsIterations(): number {
return this.raw.numInternalPgsIterations;
}
/**
* Minimum number of dynamic bodies in each active island (default: `128`).
*/
get minIslandSize(): number {
return this.raw.minIslandSize;
}
/**
* Maximum number of substeps performed by the solver (default: `1`).
*/
get maxCcdSubsteps(): number {
return this.raw.maxCcdSubsteps;
}
set dt(value: number) {
this.raw.dt = value;
}
set contact_natural_frequency(value: number) {
this.raw.contact_natural_frequency = value;
}
set lengthUnit(value: number) {
this.raw.lengthUnit = value;
}
set normalizedAllowedLinearError(value: number) {
this.raw.normalizedAllowedLinearError = value;
}
set normalizedPredictionDistance(value: number) {
this.raw.normalizedPredictionDistance = value;
}
/**
* Sets the number of solver iterations run by the constraints solver for calculating forces (default: `4`).
*/
set numSolverIterations(value: number) {
this.raw.numSolverIterations = value;
}
/**
* Sets the number of internal Project Gauss Seidel (PGS) iterations run at each solver iteration (default: `1`).
*/
set numInternalPgsIterations(value: number) {
this.raw.numInternalPgsIterations = value;
}
set minIslandSize(value: number) {
this.raw.minIslandSize = value;
}
set maxCcdSubsteps(value: number) {
this.raw.maxCcdSubsteps = value;
}
}

View File

@@ -0,0 +1,37 @@
import {RawIslandManager} from "../raw";
import {RigidBodyHandle} from "./rigid_body";
/**
* The CCD solver responsible for resolving Continuous Collision Detection.
*
* To avoid leaking WASM resources, this MUST be freed manually with `ccdSolver.free()`
* once you are done using it.
*/
export class IslandManager {
raw: RawIslandManager;
/**
* Release the WASM memory occupied by this narrow-phase.
*/
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
}
constructor(raw?: RawIslandManager) {
this.raw = raw || new RawIslandManager();
}
/**
* Applies the given closure to the handle of each active rigid-bodies contained by this set.
*
* A rigid-body is active if it is not sleeping, i.e., if it moved recently.
*
* @param f - The closure to apply.
*/
public forEachActiveRigidBodyHandle(f: (handle: RigidBodyHandle) => void) {
this.raw.forEachActiveRigidBodyHandle(f);
}
}

View File

@@ -0,0 +1,222 @@
import {
RawImpulseJointSet,
RawJointAxis,
RawJointType,
RawMultibodyJointSet,
} from "../raw";
import {
FixedImpulseJoint,
ImpulseJointHandle,
JointType,
MotorModel,
PrismaticImpulseJoint,
RevoluteImpulseJoint,
} from "./impulse_joint";
// #if DIM3
import {Quaternion} from "../math";
import {SphericalImpulseJoint} from "./impulse_joint";
// #endif
/**
* The integer identifier of a collider added to a `ColliderSet`.
*/
export type MultibodyJointHandle = number;
export class MultibodyJoint {
protected rawSet: RawMultibodyJointSet; // The MultibodyJoint won't need to free this.
handle: MultibodyJointHandle;
constructor(rawSet: RawMultibodyJointSet, handle: MultibodyJointHandle) {
this.rawSet = rawSet;
this.handle = handle;
}
public static newTyped(
rawSet: RawMultibodyJointSet,
handle: MultibodyJointHandle,
): MultibodyJoint {
switch (rawSet.jointType(handle)) {
case RawJointType.Revolute:
return new RevoluteMultibodyJoint(rawSet, handle);
case RawJointType.Prismatic:
return new PrismaticMultibodyJoint(rawSet, handle);
case RawJointType.Fixed:
return new FixedMultibodyJoint(rawSet, handle);
// #if DIM3
case RawJointType.Spherical:
return new SphericalMultibodyJoint(rawSet, handle);
// #endif
default:
return new MultibodyJoint(rawSet, handle);
}
}
/**
* Checks if this joint is still valid (i.e. that it has
* not been deleted from the joint set yet).
*/
public isValid(): boolean {
return this.rawSet.contains(this.handle);
}
// /**
// * The unique integer identifier of the first rigid-body this joint it attached to.
// */
// public bodyHandle1(): RigidBodyHandle {
// return this.rawSet.jointBodyHandle1(this.handle);
// }
//
// /**
// * The unique integer identifier of the second rigid-body this joint is attached to.
// */
// public bodyHandle2(): RigidBodyHandle {
// return this.rawSet.jointBodyHandle2(this.handle);
// }
//
// /**
// * The type of this joint given as a string.
// */
// public type(): JointType {
// return this.rawSet.jointType(this.handle);
// }
//
// // #if DIM3
// /**
// * The rotation quaternion that aligns this joint's first local axis to the `x` axis.
// */
// public frameX1(): Rotation {
// return RotationOps.fromRaw(this.rawSet.jointFrameX1(this.handle));
// }
//
// // #endif
//
// // #if DIM3
// /**
// * The rotation matrix that aligns this joint's second local axis to the `x` axis.
// */
// public frameX2(): Rotation {
// return RotationOps.fromRaw(this.rawSet.jointFrameX2(this.handle));
// }
//
// // #endif
//
// /**
// * The position of the first anchor of this joint.
// *
// * The first anchor gives the position of the points application point on the
// * local frame of the first rigid-body it is attached to.
// */
// public anchor1(): Vector {
// return VectorOps.fromRaw(this.rawSet.jointAnchor1(this.handle));
// }
//
// /**
// * The position of the second anchor of this joint.
// *
// * The second anchor gives the position of the points application point on the
// * local frame of the second rigid-body it is attached to.
// */
// public anchor2(): Vector {
// return VectorOps.fromRaw(this.rawSet.jointAnchor2(this.handle));
// }
/**
* Controls whether contacts are computed between colliders attached
* to the rigid-bodies linked by this joint.
*/
public setContactsEnabled(enabled: boolean) {
this.rawSet.jointSetContactsEnabled(this.handle, enabled);
}
/**
* Indicates if contacts are enabled between colliders attached
* to the rigid-bodies linked by this joint.
*/
public contactsEnabled(): boolean {
return this.rawSet.jointContactsEnabled(this.handle);
}
}
export class UnitMultibodyJoint extends MultibodyJoint {
/**
* The axis left free by this joint.
*/
protected rawAxis?(): RawJointAxis;
// /**
// * Are the limits enabled for this joint?
// */
// public limitsEnabled(): boolean {
// return this.rawSet.jointLimitsEnabled(this.handle, this.rawAxis());
// }
//
// /**
// * The min limit of this joint.
// */
// public limitsMin(): number {
// return this.rawSet.jointLimitsMin(this.handle, this.rawAxis());
// }
//
// /**
// * The max limit of this joint.
// */
// public limitsMax(): number {
// return this.rawSet.jointLimitsMax(this.handle, this.rawAxis());
// }
//
// public configureMotorModel(model: MotorModel) {
// this.rawSet.jointConfigureMotorModel(this.handle, this.rawAxis(), model);
// }
//
// public configureMotorVelocity(targetVel: number, factor: number) {
// this.rawSet.jointConfigureMotorVelocity(this.handle, this.rawAxis(), targetVel, factor);
// }
//
// public configureMotorPosition(targetPos: number, stiffness: number, damping: number) {
// this.rawSet.jointConfigureMotorPosition(this.handle, this.rawAxis(), targetPos, stiffness, damping);
// }
//
// public configureMotor(targetPos: number, targetVel: number, stiffness: number, damping: number) {
// this.rawSet.jointConfigureMotor(this.handle, this.rawAxis(), targetPos, targetVel, stiffness, damping);
// }
}
export class FixedMultibodyJoint extends MultibodyJoint {}
export class PrismaticMultibodyJoint extends UnitMultibodyJoint {
public rawAxis(): RawJointAxis {
return RawJointAxis.LinX;
}
}
export class RevoluteMultibodyJoint extends UnitMultibodyJoint {
public rawAxis(): RawJointAxis {
return RawJointAxis.AngX;
}
}
// #if DIM3
export class SphericalMultibodyJoint extends MultibodyJoint {
/* Unsupported by this alpha release.
public configureMotorModel(model: MotorModel) {
this.rawSet.jointConfigureMotorModel(this.handle, model);
}
public configureMotorVelocity(targetVel: Vector, factor: number) {
this.rawSet.jointConfigureBallMotorVelocity(this.handle, targetVel.x, targetVel.y, targetVel.z, factor);
}
public configureMotorPosition(targetPos: Quaternion, stiffness: number, damping: number) {
this.rawSet.jointConfigureBallMotorPosition(this.handle, targetPos.w, targetPos.x, targetPos.y, targetPos.z, stiffness, damping);
}
public configureMotor(targetPos: Quaternion, targetVel: Vector, stiffness: number, damping: number) {
this.rawSet.jointConfigureBallMotor(this.handle,
targetPos.w, targetPos.x, targetPos.y, targetPos.z,
targetVel.x, targetVel.y, targetVel.z,
stiffness, damping);
}
*/
}
// #endif

View File

@@ -0,0 +1,157 @@
import {RawMultibodyJointSet} from "../raw";
import {Coarena} from "../coarena";
import {RigidBodySet} from "./rigid_body_set";
import {
MultibodyJoint,
MultibodyJointHandle,
RevoluteMultibodyJoint,
FixedMultibodyJoint,
PrismaticMultibodyJoint,
// #if DIM3
SphericalMultibodyJoint,
// #endif
} from "./multibody_joint";
import {ImpulseJointHandle, JointData, JointType} from "./impulse_joint";
import {IslandManager} from "./island_manager";
import {ColliderHandle} from "../geometry";
import {RigidBodyHandle} from "./rigid_body";
/**
* A set of joints.
*
* To avoid leaking WASM resources, this MUST be freed manually with `jointSet.free()`
* once you are done using it (and all the joints it created).
*/
export class MultibodyJointSet {
raw: RawMultibodyJointSet;
private map: Coarena<MultibodyJoint>;
/**
* Release the WASM memory occupied by this joint set.
*/
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
if (!!this.map) {
this.map.clear();
}
this.map = undefined;
}
constructor(raw?: RawMultibodyJointSet) {
this.raw = raw || new RawMultibodyJointSet();
this.map = new Coarena<MultibodyJoint>();
// Initialize the map with the existing elements, if any.
if (raw) {
raw.forEachJointHandle((handle: MultibodyJointHandle) => {
this.map.set(handle, MultibodyJoint.newTyped(this.raw, handle));
});
}
}
/**
* Creates a new joint and return its integer handle.
*
* @param desc - The joint's parameters.
* @param parent1 - The handle of the first rigid-body this joint is attached to.
* @param parent2 - The handle of the second rigid-body this joint is attached to.
* @param wakeUp - Should the attached rigid-bodies be awakened?
*/
public createJoint(
desc: JointData,
parent1: RigidBodyHandle,
parent2: RigidBodyHandle,
wakeUp: boolean,
): MultibodyJoint {
const rawParams = desc.intoRaw();
const handle = this.raw.createJoint(
rawParams,
parent1,
parent2,
wakeUp,
);
rawParams.free();
let joint = MultibodyJoint.newTyped(this.raw, handle);
this.map.set(handle, joint);
return joint;
}
/**
* Remove a joint from this set.
*
* @param handle - The integer handle of the joint.
* @param wake_up - If `true`, the rigid-bodies attached by the removed joint will be woken-up automatically.
*/
public remove(handle: MultibodyJointHandle, wake_up: boolean) {
this.raw.remove(handle, wake_up);
this.map.delete(handle);
}
/**
* Internal function, do not call directly.
* @param handle
*/
public unmap(handle: MultibodyJointHandle) {
this.map.delete(handle);
}
/**
* The number of joints on this set.
*/
public len(): number {
return this.map.len();
}
/**
* Does this set contain a joint with the given handle?
*
* @param handle - The joint handle to check.
*/
public contains(handle: MultibodyJointHandle): boolean {
return this.get(handle) != null;
}
/**
* Gets the joint with the given handle.
*
* Returns `null` if no joint with the specified handle exists.
*
* @param handle - The integer handle of the joint to retrieve.
*/
public get(handle: MultibodyJointHandle): MultibodyJoint | null {
return this.map.get(handle);
}
/**
* Applies the given closure to each joint contained by this set.
*
* @param f - The closure to apply.
*/
public forEach(f: (joint: MultibodyJoint) => void) {
this.map.forEach(f);
}
/**
* Calls the given closure with the integer handle of each multibody joint attached to this rigid-body.
*
* @param f - The closure called with the integer handle of each multibody joint attached to the rigid-body.
*/
public forEachJointHandleAttachedToRigidBody(
handle: RigidBodyHandle,
f: (handle: MultibodyJointHandle) => void,
) {
this.raw.forEachJointAttachedToRigidBody(handle, f);
}
/**
* Gets all joints in the list.
*
* @returns joint list.
*/
public getAll(): MultibodyJoint[] {
return this.map.getAll();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,238 @@
import {RawRigidBodySet, RawRigidBodyType} from "../raw";
import {Coarena} from "../coarena";
import {VectorOps, RotationOps} from "../math";
import {
RigidBody,
RigidBodyDesc,
RigidBodyHandle,
RigidBodyType,
} from "./rigid_body";
import {ColliderSet} from "../geometry";
import {ImpulseJointSet} from "./impulse_joint_set";
import {MultibodyJointSet} from "./multibody_joint_set";
import {IslandManager} from "./island_manager";
/**
* A set of rigid bodies that can be handled by a physics pipeline.
*
* To avoid leaking WASM resources, this MUST be freed manually with `rigidBodySet.free()`
* once you are done using it (and all the rigid-bodies it created).
*/
export class RigidBodySet {
raw: RawRigidBodySet;
private map: Coarena<RigidBody>;
/**
* Release the WASM memory occupied by this rigid-body set.
*/
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
if (!!this.map) {
this.map.clear();
}
this.map = undefined;
}
constructor(raw?: RawRigidBodySet) {
this.raw = raw || new RawRigidBodySet();
this.map = new Coarena<RigidBody>();
// deserialize
if (raw) {
raw.forEachRigidBodyHandle((handle: RigidBodyHandle) => {
this.map.set(handle, new RigidBody(raw, null, handle));
});
}
}
/**
* Internal method, do not call this explicitly.
*/
public finalizeDeserialization(colliderSet: ColliderSet) {
this.map.forEach((rb) => rb.finalizeDeserialization(colliderSet));
}
/**
* Creates a new rigid-body and return its integer handle.
*
* @param desc - The description of the rigid-body to create.
*/
public createRigidBody(
colliderSet: ColliderSet,
desc: RigidBodyDesc,
): RigidBody {
let rawTra = VectorOps.intoRaw(desc.translation);
let rawRot = RotationOps.intoRaw(desc.rotation);
let rawLv = VectorOps.intoRaw(desc.linvel);
let rawCom = VectorOps.intoRaw(desc.centerOfMass);
// #if DIM3
let rawAv = VectorOps.intoRaw(desc.angvel);
let rawPrincipalInertia = VectorOps.intoRaw(
desc.principalAngularInertia,
);
let rawInertiaFrame = RotationOps.intoRaw(
desc.angularInertiaLocalFrame,
);
// #endif
let handle = this.raw.createRigidBody(
desc.enabled,
rawTra,
rawRot,
desc.gravityScale,
desc.mass,
desc.massOnly,
rawCom,
rawLv,
// #if DIM2
desc.angvel,
desc.principalAngularInertia,
desc.translationsEnabledX,
desc.translationsEnabledY,
desc.rotationsEnabled,
// #endif
// #if DIM3
rawAv,
rawPrincipalInertia,
rawInertiaFrame,
desc.translationsEnabledX,
desc.translationsEnabledY,
desc.translationsEnabledZ,
desc.rotationsEnabledX,
desc.rotationsEnabledY,
desc.rotationsEnabledZ,
// #endif
desc.linearDamping,
desc.angularDamping,
desc.status as number as RawRigidBodyType,
desc.canSleep,
desc.sleeping,
desc.softCcdPrediction,
desc.ccdEnabled,
desc.dominanceGroup,
desc.additionalSolverIterations,
);
rawTra.free();
rawRot.free();
rawLv.free();
rawCom.free();
// #if DIM3
rawAv.free();
rawPrincipalInertia.free();
rawInertiaFrame.free();
// #endif
const body = new RigidBody(this.raw, colliderSet, handle);
body.userData = desc.userData;
this.map.set(handle, body);
return body;
}
/**
* Removes a rigid-body from this set.
*
* This will also remove all the colliders and joints attached to the rigid-body.
*
* @param handle - The integer handle of the rigid-body to remove.
* @param colliders - The set of colliders that may contain colliders attached to the removed rigid-body.
* @param impulseJoints - The set of impulse joints that may contain joints attached to the removed rigid-body.
* @param multibodyJoints - The set of multibody joints that may contain joints attached to the removed rigid-body.
*/
public remove(
handle: RigidBodyHandle,
islands: IslandManager,
colliders: ColliderSet,
impulseJoints: ImpulseJointSet,
multibodyJoints: MultibodyJointSet,
) {
// Unmap the entities that will be removed automatically because of the rigid-body removals.
for (let i = 0; i < this.raw.rbNumColliders(handle); i += 1) {
colliders.unmap(this.raw.rbCollider(handle, i));
}
impulseJoints.forEachJointHandleAttachedToRigidBody(handle, (handle) =>
impulseJoints.unmap(handle),
);
multibodyJoints.forEachJointHandleAttachedToRigidBody(
handle,
(handle) => multibodyJoints.unmap(handle),
);
// Remove the rigid-body.
this.raw.remove(
handle,
islands.raw,
colliders.raw,
impulseJoints.raw,
multibodyJoints.raw,
);
this.map.delete(handle);
}
/**
* The number of rigid-bodies on this set.
*/
public len(): number {
return this.map.len();
}
/**
* Does this set contain a rigid-body with the given handle?
*
* @param handle - The rigid-body handle to check.
*/
public contains(handle: RigidBodyHandle): boolean {
return this.get(handle) != null;
}
/**
* Gets the rigid-body with the given handle.
*
* @param handle - The handle of the rigid-body to retrieve.
*/
public get(handle: RigidBodyHandle): RigidBody | null {
return this.map.get(handle);
}
/**
* Applies the given closure to each rigid-body contained by this set.
*
* @param f - The closure to apply.
*/
public forEach(f: (body: RigidBody) => void) {
this.map.forEach(f);
}
/**
* Applies the given closure to each active rigid-bodies contained by this set.
*
* A rigid-body is active if it is not sleeping, i.e., if it moved recently.
*
* @param f - The closure to apply.
*/
public forEachActiveRigidBody(
islands: IslandManager,
f: (body: RigidBody) => void,
) {
islands.forEachActiveRigidBodyHandle((handle) => {
f(this.get(handle));
});
}
/**
* Gets all rigid-bodies in the list.
*
* @returns rigid-bodies list.
*/
public getAll(): RigidBody[] {
return this.map.getAll();
}
}