feat(rapier2d): 新增Rapier2D WASM绑定包
This commit is contained in:
386
packages/rapier2d/src/control/character_controller.ts
Normal file
386
packages/rapier2d/src/control/character_controller.ts
Normal file
@@ -0,0 +1,386 @@
|
||||
import {RawKinematicCharacterController, RawCharacterCollision} from "../raw";
|
||||
import {Rotation, Vector, VectorOps} from "../math";
|
||||
import {
|
||||
BroadPhase,
|
||||
Collider,
|
||||
ColliderSet,
|
||||
InteractionGroups,
|
||||
NarrowPhase,
|
||||
Shape,
|
||||
} from "../geometry";
|
||||
import {QueryFilterFlags, World} from "../pipeline";
|
||||
import {IntegrationParameters, RigidBody, RigidBodySet} from "../dynamics";
|
||||
|
||||
/**
|
||||
* A collision between the character and an obstacle hit on its path.
|
||||
*/
|
||||
export class CharacterCollision {
|
||||
/** The collider involved in the collision. Null if the collider no longer exists in the physics world. */
|
||||
public collider: Collider | null;
|
||||
/** The translation delta applied to the character before this collision took place. */
|
||||
public translationDeltaApplied: Vector;
|
||||
/** The translation delta the character would move after this collision if there is no other obstacles. */
|
||||
public translationDeltaRemaining: Vector;
|
||||
/** The time-of-impact between the character and the obstacles. */
|
||||
public toi: number;
|
||||
/** The world-space contact point on the collider when the collision happens. */
|
||||
public witness1: Vector;
|
||||
/** The local-space contact point on the character when the collision happens. */
|
||||
public witness2: Vector;
|
||||
/** The world-space outward contact normal on the collider when the collision happens. */
|
||||
public normal1: Vector;
|
||||
/** The local-space outward contact normal on the character when the collision happens. */
|
||||
public normal2: Vector;
|
||||
}
|
||||
|
||||
/**
|
||||
* A character controller for controlling kinematic bodies and parentless colliders by hitting
|
||||
* and sliding against obstacles.
|
||||
*/
|
||||
export class KinematicCharacterController {
|
||||
private raw: RawKinematicCharacterController;
|
||||
private rawCharacterCollision: RawCharacterCollision;
|
||||
|
||||
private params: IntegrationParameters;
|
||||
private broadPhase: BroadPhase;
|
||||
private narrowPhase: NarrowPhase;
|
||||
private bodies: RigidBodySet;
|
||||
private colliders: ColliderSet;
|
||||
private _applyImpulsesToDynamicBodies: boolean;
|
||||
private _characterMass: number | null;
|
||||
|
||||
constructor(
|
||||
offset: number,
|
||||
params: IntegrationParameters,
|
||||
broadPhase: BroadPhase,
|
||||
narrowPhase: NarrowPhase,
|
||||
bodies: RigidBodySet,
|
||||
colliders: ColliderSet,
|
||||
) {
|
||||
this.params = params;
|
||||
this.bodies = bodies;
|
||||
this.colliders = colliders;
|
||||
this.broadPhase = broadPhase;
|
||||
this.narrowPhase = narrowPhase;
|
||||
this.raw = new RawKinematicCharacterController(offset);
|
||||
this.rawCharacterCollision = new RawCharacterCollision();
|
||||
this._applyImpulsesToDynamicBodies = false;
|
||||
this._characterMass = null;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public free() {
|
||||
if (!!this.raw) {
|
||||
this.raw.free();
|
||||
this.rawCharacterCollision.free();
|
||||
}
|
||||
|
||||
this.raw = undefined;
|
||||
this.rawCharacterCollision = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* The direction that goes "up". Used to determine where the floor is, and the floor’s angle.
|
||||
*/
|
||||
public up(): Vector {
|
||||
return this.raw.up();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the direction that goes "up". Used to determine where the floor is, and the floor’s angle.
|
||||
*/
|
||||
public setUp(vector: Vector) {
|
||||
let rawVect = VectorOps.intoRaw(vector);
|
||||
return this.raw.setUp(rawVect);
|
||||
rawVect.free();
|
||||
}
|
||||
|
||||
public applyImpulsesToDynamicBodies(): boolean {
|
||||
return this._applyImpulsesToDynamicBodies;
|
||||
}
|
||||
|
||||
public setApplyImpulsesToDynamicBodies(enabled: boolean) {
|
||||
this._applyImpulsesToDynamicBodies = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the custom value of the character mass, if it was set by `this.setCharacterMass`.
|
||||
*/
|
||||
public characterMass(): number | null {
|
||||
return this._characterMass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the mass of the character to be used for impulse resolution if `self.applyImpulsesToDynamicBodies`
|
||||
* is set to `true`.
|
||||
*
|
||||
* If no character mass is set explicitly (or if it is set to `null`) it is automatically assumed to be equal
|
||||
* to the mass of the rigid-body the character collider is attached to; or equal to 0 if the character collider
|
||||
* isn’t attached to any rigid-body.
|
||||
*
|
||||
* @param mass - The mass to set.
|
||||
*/
|
||||
public setCharacterMass(mass: number | null) {
|
||||
this._characterMass = mass;
|
||||
}
|
||||
|
||||
/**
|
||||
* A small gap to preserve between the character and its surroundings.
|
||||
*
|
||||
* This value should not be too large to avoid visual artifacts, but shouldn’t be too small
|
||||
* (must not be zero) to improve numerical stability of the character controller.
|
||||
*/
|
||||
public offset(): number {
|
||||
return this.raw.offset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a small gap to preserve between the character and its surroundings.
|
||||
*
|
||||
* This value should not be too large to avoid visual artifacts, but shouldn’t be too small
|
||||
* (must not be zero) to improve numerical stability of the character controller.
|
||||
*/
|
||||
public setOffset(value: number) {
|
||||
this.raw.setOffset(value);
|
||||
}
|
||||
|
||||
/// Increase this number if your character appears to get stuck when sliding against surfaces.
|
||||
///
|
||||
/// This is a small distance applied to the movement toward the contact normals of shapes hit
|
||||
/// by the character controller. This helps shape-casting not getting stuck in an always-penetrating
|
||||
/// state during the sliding calculation.
|
||||
///
|
||||
/// This value should remain fairly small since it can introduce artificial "bumps" when sliding
|
||||
/// along a flat surface.
|
||||
public normalNudgeFactor(): number {
|
||||
return this.raw.normalNudgeFactor();
|
||||
}
|
||||
|
||||
/// Increase this number if your character appears to get stuck when sliding against surfaces.
|
||||
///
|
||||
/// This is a small distance applied to the movement toward the contact normals of shapes hit
|
||||
/// by the character controller. This helps shape-casting not getting stuck in an always-penetrating
|
||||
/// state during the sliding calculation.
|
||||
///
|
||||
/// This value should remain fairly small since it can introduce artificial "bumps" when sliding
|
||||
/// along a flat surface.
|
||||
public setNormalNudgeFactor(value: number) {
|
||||
this.raw.setNormalNudgeFactor(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is sliding against obstacles enabled?
|
||||
*/
|
||||
public slideEnabled(): boolean {
|
||||
return this.raw.slideEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable sliding against obstacles.
|
||||
*/
|
||||
public setSlideEnabled(enabled: boolean) {
|
||||
this.raw.setSlideEnabled(enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum step height a character can automatically step over.
|
||||
*/
|
||||
public autostepMaxHeight(): number | null {
|
||||
return this.raw.autostepMaxHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
* The minimum width of free space that must be available after stepping on a stair.
|
||||
*/
|
||||
public autostepMinWidth(): number | null {
|
||||
return this.raw.autostepMinWidth();
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the character automatically step over dynamic bodies too?
|
||||
*/
|
||||
public autostepIncludesDynamicBodies(): boolean | null {
|
||||
return this.raw.autostepIncludesDynamicBodies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is automatically stepping over small objects enabled?
|
||||
*/
|
||||
public autostepEnabled(): boolean {
|
||||
return this.raw.autostepEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enabled automatically stepping over small objects.
|
||||
*
|
||||
* @param maxHeight - The maximum step height a character can automatically step over.
|
||||
* @param minWidth - The minimum width of free space that must be available after stepping on a stair.
|
||||
* @param includeDynamicBodies - Can the character automatically step over dynamic bodies too?
|
||||
*/
|
||||
public enableAutostep(
|
||||
maxHeight: number,
|
||||
minWidth: number,
|
||||
includeDynamicBodies: boolean,
|
||||
) {
|
||||
this.raw.enableAutostep(maxHeight, minWidth, includeDynamicBodies);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable automatically stepping over small objects.
|
||||
*/
|
||||
public disableAutostep() {
|
||||
return this.raw.disableAutostep();
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum angle (radians) between the floor’s normal and the `up` vector that the
|
||||
* character is able to climb.
|
||||
*/
|
||||
public maxSlopeClimbAngle(): number {
|
||||
return this.raw.maxSlopeClimbAngle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum angle (radians) between the floor’s normal and the `up` vector that the
|
||||
* character is able to climb.
|
||||
*/
|
||||
public setMaxSlopeClimbAngle(angle: number) {
|
||||
this.raw.setMaxSlopeClimbAngle(angle);
|
||||
}
|
||||
|
||||
/**
|
||||
* The minimum angle (radians) between the floor’s normal and the `up` vector before the
|
||||
* character starts to slide down automatically.
|
||||
*/
|
||||
public minSlopeSlideAngle(): number {
|
||||
return this.raw.minSlopeSlideAngle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the minimum angle (radians) between the floor’s normal and the `up` vector before the
|
||||
* character starts to slide down automatically.
|
||||
*/
|
||||
public setMinSlopeSlideAngle(angle: number) {
|
||||
this.raw.setMinSlopeSlideAngle(angle);
|
||||
}
|
||||
|
||||
/**
|
||||
* If snap-to-ground is enabled, should the character be automatically snapped to the ground if
|
||||
* the distance between the ground and its feet are smaller than the specified threshold?
|
||||
*/
|
||||
public snapToGroundDistance(): number | null {
|
||||
return this.raw.snapToGroundDistance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables automatically snapping the character to the ground if the distance between
|
||||
* the ground and its feet are smaller than the specified threshold.
|
||||
*/
|
||||
public enableSnapToGround(distance: number) {
|
||||
this.raw.enableSnapToGround(distance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables automatically snapping the character to the ground.
|
||||
*/
|
||||
public disableSnapToGround() {
|
||||
this.raw.disableSnapToGround();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is automatically snapping the character to the ground enabled?
|
||||
*/
|
||||
public snapToGroundEnabled(): boolean {
|
||||
return this.raw.snapToGroundEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the movement the given collider is able to execute after hitting and sliding on obstacles.
|
||||
*
|
||||
* @param collider - The collider to move.
|
||||
* @param desiredTranslationDelta - The desired collider movement.
|
||||
* @param filterFlags - Flags for excluding whole subsets of colliders from the obstacles taken into account.
|
||||
* @param filterGroups - Groups for excluding colliders with incompatible collision groups from the obstacles
|
||||
* taken into account.
|
||||
* @param filterPredicate - Any collider for which this closure returns `false` will be excluded from the
|
||||
* obstacles taken into account.
|
||||
*/
|
||||
public computeColliderMovement(
|
||||
collider: Collider,
|
||||
desiredTranslationDelta: Vector,
|
||||
filterFlags?: QueryFilterFlags,
|
||||
filterGroups?: InteractionGroups,
|
||||
filterPredicate?: (collider: Collider) => boolean,
|
||||
) {
|
||||
let rawTranslationDelta = VectorOps.intoRaw(desiredTranslationDelta);
|
||||
this.raw.computeColliderMovement(
|
||||
this.params.dt,
|
||||
this.broadPhase.raw,
|
||||
this.narrowPhase.raw,
|
||||
this.bodies.raw,
|
||||
this.colliders.raw,
|
||||
collider.handle,
|
||||
rawTranslationDelta,
|
||||
this._applyImpulsesToDynamicBodies,
|
||||
this._characterMass,
|
||||
filterFlags,
|
||||
filterGroups,
|
||||
this.colliders.castClosure(filterPredicate),
|
||||
);
|
||||
rawTranslationDelta.free();
|
||||
}
|
||||
|
||||
/**
|
||||
* The movement computed by the last call to `this.computeColliderMovement`.
|
||||
*/
|
||||
public computedMovement(): Vector {
|
||||
return VectorOps.fromRaw(this.raw.computedMovement());
|
||||
}
|
||||
|
||||
/**
|
||||
* The result of ground detection computed by the last call to `this.computeColliderMovement`.
|
||||
*/
|
||||
public computedGrounded(): boolean {
|
||||
return this.raw.computedGrounded();
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of collisions against obstacles detected along the path of the last call
|
||||
* to `this.computeColliderMovement`.
|
||||
*/
|
||||
public numComputedCollisions(): number {
|
||||
return this.raw.numComputedCollisions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the collision against one of the obstacles detected along the path of the last
|
||||
* call to `this.computeColliderMovement`.
|
||||
*
|
||||
* @param i - The i-th collision will be returned.
|
||||
* @param out - If this argument is set, it will be filled with the collision information.
|
||||
*/
|
||||
public computedCollision(
|
||||
i: number,
|
||||
out?: CharacterCollision,
|
||||
): CharacterCollision | null {
|
||||
if (!this.raw.computedCollision(i, this.rawCharacterCollision)) {
|
||||
return null;
|
||||
} else {
|
||||
let c = this.rawCharacterCollision;
|
||||
out = out ?? new CharacterCollision();
|
||||
out.translationDeltaApplied = VectorOps.fromRaw(
|
||||
c.translationDeltaApplied(),
|
||||
);
|
||||
out.translationDeltaRemaining = VectorOps.fromRaw(
|
||||
c.translationDeltaRemaining(),
|
||||
);
|
||||
out.toi = c.toi();
|
||||
out.witness1 = VectorOps.fromRaw(c.worldWitness1());
|
||||
out.witness2 = VectorOps.fromRaw(c.worldWitness2());
|
||||
out.normal1 = VectorOps.fromRaw(c.worldNormal1());
|
||||
out.normal2 = VectorOps.fromRaw(c.worldNormal2());
|
||||
out.collider = this.colliders.get(c.handle());
|
||||
return out;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
packages/rapier2d/src/control/index.ts
Normal file
3
packages/rapier2d/src/control/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./character_controller";
|
||||
export * from "./pid_controller";
|
||||
|
||||
153
packages/rapier2d/src/control/pid_controller.ts
Normal file
153
packages/rapier2d/src/control/pid_controller.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import {RawPidController} from "../raw";
|
||||
import {Rotation, RotationOps, Vector, VectorOps} from "../math";
|
||||
import {Collider, ColliderSet, InteractionGroups, Shape} from "../geometry";
|
||||
import {QueryFilterFlags, World} from "../pipeline";
|
||||
import {IntegrationParameters, RigidBody, RigidBodySet} from "../dynamics";
|
||||
|
||||
// TODO: unify with the JointAxesMask
|
||||
/**
|
||||
* An enum representing the possible joint axes controlled by a PidController.
|
||||
* They can be ORed together, like:
|
||||
* PidAxesMask.LinX || PidAxesMask.LinY
|
||||
* to get a pid controller that only constraints the translational X and Y axes.
|
||||
*
|
||||
* Possible axes are:
|
||||
*
|
||||
* - `X`: X translation axis
|
||||
* - `Y`: Y translation axis
|
||||
* - `Z`: Z translation axis
|
||||
* - `AngX`: X angular rotation axis (3D only)
|
||||
* - `AngY`: Y angular rotation axis (3D only)
|
||||
* - `AngZ`: Z angular rotation axis
|
||||
*/
|
||||
export enum PidAxesMask {
|
||||
None = 0,
|
||||
LinX = 1 << 0,
|
||||
LinY = 1 << 1,
|
||||
LinZ = 1 << 2,
|
||||
AngZ = 1 << 5,
|
||||
AllLin = PidAxesMask.LinX | PidAxesMask.LinY,
|
||||
AllAng = PidAxesMask.AngZ,
|
||||
All = PidAxesMask.AllLin | PidAxesMask.AllAng,
|
||||
}
|
||||
|
||||
/**
|
||||
* A controller for controlling dynamic bodies using the
|
||||
* Proportional-Integral-Derivative correction model.
|
||||
*/
|
||||
export class PidController {
|
||||
private raw: RawPidController;
|
||||
|
||||
private params: IntegrationParameters;
|
||||
private bodies: RigidBodySet;
|
||||
|
||||
constructor(
|
||||
params: IntegrationParameters,
|
||||
bodies: RigidBodySet,
|
||||
kp: number,
|
||||
ki: number,
|
||||
kd: number,
|
||||
axes: PidAxesMask,
|
||||
) {
|
||||
this.params = params;
|
||||
this.bodies = bodies;
|
||||
this.raw = new RawPidController(kp, ki, kd, axes);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public free() {
|
||||
if (!!this.raw) {
|
||||
this.raw.free();
|
||||
}
|
||||
|
||||
this.raw = undefined;
|
||||
}
|
||||
|
||||
public setKp(kp: number, axes: PidAxesMask) {
|
||||
this.raw.set_kp(kp, axes);
|
||||
}
|
||||
|
||||
public setKi(ki: number, axes: PidAxesMask) {
|
||||
this.raw.set_kp(ki, axes);
|
||||
}
|
||||
|
||||
public setKd(kd: number, axes: PidAxesMask) {
|
||||
this.raw.set_kp(kd, axes);
|
||||
}
|
||||
|
||||
public setAxes(axes: PidAxesMask) {
|
||||
this.raw.set_axes_mask(axes);
|
||||
}
|
||||
|
||||
public resetIntegrals() {
|
||||
this.raw.reset_integrals();
|
||||
}
|
||||
|
||||
public applyLinearCorrection(
|
||||
body: RigidBody,
|
||||
targetPosition: Vector,
|
||||
targetLinvel: Vector,
|
||||
) {
|
||||
let rawPos = VectorOps.intoRaw(targetPosition);
|
||||
let rawVel = VectorOps.intoRaw(targetLinvel);
|
||||
this.raw.apply_linear_correction(
|
||||
this.params.dt,
|
||||
this.bodies.raw,
|
||||
body.handle,
|
||||
rawPos,
|
||||
rawVel,
|
||||
);
|
||||
rawPos.free();
|
||||
rawVel.free();
|
||||
}
|
||||
|
||||
public applyAngularCorrection(
|
||||
body: RigidBody,
|
||||
targetRotation: number,
|
||||
targetAngVel: number,
|
||||
) {
|
||||
this.raw.apply_angular_correction(
|
||||
this.params.dt,
|
||||
this.bodies.raw,
|
||||
body.handle,
|
||||
targetRotation,
|
||||
targetAngVel,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public linearCorrection(
|
||||
body: RigidBody,
|
||||
targetPosition: Vector,
|
||||
targetLinvel: Vector,
|
||||
): Vector {
|
||||
let rawPos = VectorOps.intoRaw(targetPosition);
|
||||
let rawVel = VectorOps.intoRaw(targetLinvel);
|
||||
let correction = this.raw.linear_correction(
|
||||
this.params.dt,
|
||||
this.bodies.raw,
|
||||
body.handle,
|
||||
rawPos,
|
||||
rawVel,
|
||||
);
|
||||
rawPos.free();
|
||||
rawVel.free();
|
||||
|
||||
return VectorOps.fromRaw(correction);
|
||||
}
|
||||
|
||||
public angularCorrection(
|
||||
body: RigidBody,
|
||||
targetRotation: number,
|
||||
targetAngVel: number,
|
||||
): number {
|
||||
return this.raw.angular_correction(
|
||||
this.params.dt,
|
||||
this.bodies.raw,
|
||||
body.handle,
|
||||
targetRotation,
|
||||
targetAngVel,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
481
packages/rapier2d/src/control/ray_cast_vehicle_controller.ts
Normal file
481
packages/rapier2d/src/control/ray_cast_vehicle_controller.ts
Normal file
@@ -0,0 +1,481 @@
|
||||
import {RawDynamicRayCastVehicleController} from "../raw";
|
||||
import {Vector, VectorOps} from "../math";
|
||||
import {
|
||||
BroadPhase,
|
||||
Collider,
|
||||
ColliderSet,
|
||||
InteractionGroups,
|
||||
NarrowPhase,
|
||||
} from "../geometry";
|
||||
import {QueryFilterFlags} from "../pipeline";
|
||||
import {RigidBody, RigidBodyHandle, RigidBodySet} from "../dynamics";
|
||||
|
||||
/**
|
||||
* A character controller to simulate vehicles using ray-casting for the wheels.
|
||||
*/
|
||||
export class DynamicRayCastVehicleController {
|
||||
private raw: RawDynamicRayCastVehicleController;
|
||||
private broadPhase: BroadPhase;
|
||||
private narrowPhase: NarrowPhase;
|
||||
private bodies: RigidBodySet;
|
||||
private colliders: ColliderSet;
|
||||
private _chassis: RigidBody;
|
||||
|
||||
constructor(
|
||||
chassis: RigidBody,
|
||||
broadPhase: BroadPhase,
|
||||
narrowPhase: NarrowPhase,
|
||||
bodies: RigidBodySet,
|
||||
colliders: ColliderSet,
|
||||
) {
|
||||
this.raw = new RawDynamicRayCastVehicleController(chassis.handle);
|
||||
this.broadPhase = broadPhase;
|
||||
this.narrowPhase = narrowPhase;
|
||||
this.bodies = bodies;
|
||||
this.colliders = colliders;
|
||||
this._chassis = chassis;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public free() {
|
||||
if (!!this.raw) {
|
||||
this.raw.free();
|
||||
}
|
||||
|
||||
this.raw = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the vehicle’s velocity based on its suspension, engine force, and brake.
|
||||
*
|
||||
* This directly updates the velocity of its chassis rigid-body.
|
||||
*
|
||||
* @param dt - Time increment used to integrate forces.
|
||||
* @param filterFlags - Flag to exclude categories of objects from the wheels’ ray-cast.
|
||||
* @param filterGroups - Only colliders compatible with these groups will be hit by the wheels’ ray-casts.
|
||||
* @param filterPredicate - Callback to filter out which collider will be hit by the wheels’ ray-casts.
|
||||
*/
|
||||
public updateVehicle(
|
||||
dt: number,
|
||||
filterFlags?: QueryFilterFlags,
|
||||
filterGroups?: InteractionGroups,
|
||||
filterPredicate?: (collider: Collider) => boolean,
|
||||
) {
|
||||
this.raw.update_vehicle(
|
||||
dt,
|
||||
this.broadPhase.raw,
|
||||
this.narrowPhase.raw,
|
||||
this.bodies.raw,
|
||||
this.colliders.raw,
|
||||
filterFlags,
|
||||
filterGroups,
|
||||
this.colliders.castClosure(filterPredicate),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The current forward speed of the vehicle.
|
||||
*/
|
||||
public currentVehicleSpeed(): number {
|
||||
return this.raw.current_vehicle_speed();
|
||||
}
|
||||
|
||||
/**
|
||||
* The rigid-body used as the chassis.
|
||||
*/
|
||||
public chassis(): RigidBody {
|
||||
return this._chassis;
|
||||
}
|
||||
|
||||
/**
|
||||
* The chassis’ local _up_ direction (`0 = x, 1 = y, 2 = z`).
|
||||
*/
|
||||
get indexUpAxis(): number {
|
||||
return this.raw.index_up_axis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the chassis’ local _up_ direction (`0 = x, 1 = y, 2 = z`).
|
||||
*/
|
||||
set indexUpAxis(axis: number) {
|
||||
this.raw.set_index_up_axis(axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* The chassis’ local _forward_ direction (`0 = x, 1 = y, 2 = z`).
|
||||
*/
|
||||
get indexForwardAxis(): number {
|
||||
return this.raw.index_forward_axis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the chassis’ local _forward_ direction (`0 = x, 1 = y, 2 = z`).
|
||||
*/
|
||||
set setIndexForwardAxis(axis: number) {
|
||||
this.raw.set_index_forward_axis(axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new wheel attached to this vehicle.
|
||||
* @param chassisConnectionCs - The position of the wheel relative to the chassis.
|
||||
* @param directionCs - The direction of the wheel’s suspension, relative to the chassis. The ray-casting will
|
||||
* happen following this direction to detect the ground.
|
||||
* @param axleCs - The wheel’s axle axis, relative to the chassis.
|
||||
* @param suspensionRestLength - The rest length of the wheel’s suspension spring.
|
||||
* @param radius - The wheel’s radius.
|
||||
*/
|
||||
public addWheel(
|
||||
chassisConnectionCs: Vector,
|
||||
directionCs: Vector,
|
||||
axleCs: Vector,
|
||||
suspensionRestLength: number,
|
||||
radius: number,
|
||||
) {
|
||||
let rawChassisConnectionCs = VectorOps.intoRaw(chassisConnectionCs);
|
||||
let rawDirectionCs = VectorOps.intoRaw(directionCs);
|
||||
let rawAxleCs = VectorOps.intoRaw(axleCs);
|
||||
|
||||
this.raw.add_wheel(
|
||||
rawChassisConnectionCs,
|
||||
rawDirectionCs,
|
||||
rawAxleCs,
|
||||
suspensionRestLength,
|
||||
radius,
|
||||
);
|
||||
|
||||
rawChassisConnectionCs.free();
|
||||
rawDirectionCs.free();
|
||||
rawAxleCs.free();
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of wheels attached to this vehicle.
|
||||
*/
|
||||
public numWheels(): number {
|
||||
return this.raw.num_wheels();
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Access to wheel properties.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* Getters + setters
|
||||
*/
|
||||
/**
|
||||
* The position of the i-th wheel, relative to the chassis.
|
||||
*/
|
||||
public wheelChassisConnectionPointCs(i: number): Vector | null {
|
||||
return VectorOps.fromRaw(this.raw.wheel_chassis_connection_point_cs(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the position of the i-th wheel, relative to the chassis.
|
||||
*/
|
||||
public setWheelChassisConnectionPointCs(i: number, value: Vector) {
|
||||
let rawValue = VectorOps.intoRaw(value);
|
||||
this.raw.set_wheel_chassis_connection_point_cs(i, rawValue);
|
||||
rawValue.free();
|
||||
}
|
||||
|
||||
/**
|
||||
* The rest length of the i-th wheel’s suspension spring.
|
||||
*/
|
||||
public wheelSuspensionRestLength(i: number): number | null {
|
||||
return this.raw.wheel_suspension_rest_length(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the rest length of the i-th wheel’s suspension spring.
|
||||
*/
|
||||
public setWheelSuspensionRestLength(i: number, value: number) {
|
||||
this.raw.set_wheel_suspension_rest_length(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum distance the i-th wheel suspension can travel before and after its resting length.
|
||||
*/
|
||||
public wheelMaxSuspensionTravel(i: number): number | null {
|
||||
return this.raw.wheel_max_suspension_travel(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum distance the i-th wheel suspension can travel before and after its resting length.
|
||||
*/
|
||||
public setWheelMaxSuspensionTravel(i: number, value: number) {
|
||||
this.raw.set_wheel_max_suspension_travel(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The i-th wheel’s radius.
|
||||
*/
|
||||
public wheelRadius(i: number): number | null {
|
||||
return this.raw.wheel_radius(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the i-th wheel’s radius.
|
||||
*/
|
||||
public setWheelRadius(i: number, value: number) {
|
||||
this.raw.set_wheel_radius(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The i-th wheel’s suspension stiffness.
|
||||
*
|
||||
* Increase this value if the suspension appears to not push the vehicle strong enough.
|
||||
*/
|
||||
public wheelSuspensionStiffness(i: number): number | null {
|
||||
return this.raw.wheel_suspension_stiffness(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the i-th wheel’s suspension stiffness.
|
||||
*
|
||||
* Increase this value if the suspension appears to not push the vehicle strong enough.
|
||||
*/
|
||||
public setWheelSuspensionStiffness(i: number, value: number) {
|
||||
this.raw.set_wheel_suspension_stiffness(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The i-th wheel’s suspension’s damping when it is being compressed.
|
||||
*/
|
||||
public wheelSuspensionCompression(i: number): number | null {
|
||||
return this.raw.wheel_suspension_compression(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The i-th wheel’s suspension’s damping when it is being compressed.
|
||||
*/
|
||||
public setWheelSuspensionCompression(i: number, value: number) {
|
||||
this.raw.set_wheel_suspension_compression(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The i-th wheel’s suspension’s damping when it is being released.
|
||||
*
|
||||
* Increase this value if the suspension appears to overshoot.
|
||||
*/
|
||||
public wheelSuspensionRelaxation(i: number): number | null {
|
||||
return this.raw.wheel_suspension_relaxation(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the i-th wheel’s suspension’s damping when it is being released.
|
||||
*
|
||||
* Increase this value if the suspension appears to overshoot.
|
||||
*/
|
||||
public setWheelSuspensionRelaxation(i: number, value: number) {
|
||||
this.raw.set_wheel_suspension_relaxation(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum force applied by the i-th wheel’s suspension.
|
||||
*/
|
||||
public wheelMaxSuspensionForce(i: number): number | null {
|
||||
return this.raw.wheel_max_suspension_force(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum force applied by the i-th wheel’s suspension.
|
||||
*/
|
||||
public setWheelMaxSuspensionForce(i: number, value: number) {
|
||||
this.raw.set_wheel_max_suspension_force(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum amount of braking impulse applied on the i-th wheel to slow down the vehicle.
|
||||
*/
|
||||
public wheelBrake(i: number): number | null {
|
||||
return this.raw.wheel_brake(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum amount of braking impulse applied on the i-th wheel to slow down the vehicle.
|
||||
*/
|
||||
public setWheelBrake(i: number, value: number) {
|
||||
this.raw.set_wheel_brake(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The steering angle (radians) for the i-th wheel.
|
||||
*/
|
||||
public wheelSteering(i: number): number | null {
|
||||
return this.raw.wheel_steering(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the steering angle (radians) for the i-th wheel.
|
||||
*/
|
||||
public setWheelSteering(i: number, value: number) {
|
||||
this.raw.set_wheel_steering(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The forward force applied by the i-th wheel on the chassis.
|
||||
*/
|
||||
public wheelEngineForce(i: number): number | null {
|
||||
return this.raw.wheel_engine_force(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the forward force applied by the i-th wheel on the chassis.
|
||||
*/
|
||||
public setWheelEngineForce(i: number, value: number) {
|
||||
this.raw.set_wheel_engine_force(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The direction of the i-th wheel’s suspension, relative to the chassis.
|
||||
*
|
||||
* The ray-casting will happen following this direction to detect the ground.
|
||||
*/
|
||||
public wheelDirectionCs(i: number): Vector | null {
|
||||
return VectorOps.fromRaw(this.raw.wheel_direction_cs(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the direction of the i-th wheel’s suspension, relative to the chassis.
|
||||
*
|
||||
* The ray-casting will happen following this direction to detect the ground.
|
||||
*/
|
||||
public setWheelDirectionCs(i: number, value: Vector) {
|
||||
let rawValue = VectorOps.intoRaw(value);
|
||||
this.raw.set_wheel_direction_cs(i, rawValue);
|
||||
rawValue.free();
|
||||
}
|
||||
|
||||
/**
|
||||
* The i-th wheel’s axle axis, relative to the chassis.
|
||||
*
|
||||
* The axis index defined as 0 = X, 1 = Y, 2 = Z.
|
||||
*/
|
||||
public wheelAxleCs(i: number): Vector | null {
|
||||
return VectorOps.fromRaw(this.raw.wheel_axle_cs(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the i-th wheel’s axle axis, relative to the chassis.
|
||||
*
|
||||
* The axis index defined as 0 = X, 1 = Y, 2 = Z.
|
||||
*/
|
||||
public setWheelAxleCs(i: number, value: Vector) {
|
||||
let rawValue = VectorOps.intoRaw(value);
|
||||
this.raw.set_wheel_axle_cs(i, rawValue);
|
||||
rawValue.free();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter controlling how much traction the tire has.
|
||||
*
|
||||
* The larger the value, the more instantaneous braking will happen (with the risk of
|
||||
* causing the vehicle to flip if it’s too strong).
|
||||
*/
|
||||
public wheelFrictionSlip(i: number): number | null {
|
||||
return this.raw.wheel_friction_slip(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the parameter controlling how much traction the tire has.
|
||||
*
|
||||
* The larger the value, the more instantaneous braking will happen (with the risk of
|
||||
* causing the vehicle to flip if it’s too strong).
|
||||
*/
|
||||
public setWheelFrictionSlip(i: number, value: number) {
|
||||
this.raw.set_wheel_friction_slip(i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The multiplier of friction between a tire and the collider it’s on top of.
|
||||
*
|
||||
* The larger the value, the stronger side friction will be.
|
||||
*/
|
||||
public wheelSideFrictionStiffness(i: number): number | null {
|
||||
return this.raw.wheel_side_friction_stiffness(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The multiplier of friction between a tire and the collider it’s on top of.
|
||||
*
|
||||
* The larger the value, the stronger side friction will be.
|
||||
*/
|
||||
public setWheelSideFrictionStiffness(i: number, value: number) {
|
||||
this.raw.set_wheel_side_friction_stiffness(i, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Getters only.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The i-th wheel’s current rotation angle (radians) on its axle.
|
||||
*/
|
||||
public wheelRotation(i: number): number | null {
|
||||
return this.raw.wheel_rotation(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The forward impulses applied by the i-th wheel on the chassis.
|
||||
*/
|
||||
public wheelForwardImpulse(i: number): number | null {
|
||||
return this.raw.wheel_forward_impulse(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The side impulses applied by the i-th wheel on the chassis.
|
||||
*/
|
||||
public wheelSideImpulse(i: number): number | null {
|
||||
return this.raw.wheel_side_impulse(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The force applied by the i-th wheel suspension.
|
||||
*/
|
||||
public wheelSuspensionForce(i: number): number | null {
|
||||
return this.raw.wheel_suspension_force(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The (world-space) contact normal between the i-th wheel and the floor.
|
||||
*/
|
||||
public wheelContactNormal(i: number): Vector | null {
|
||||
return VectorOps.fromRaw(this.raw.wheel_contact_normal_ws(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* The (world-space) point hit by the wheel’s ray-cast for the i-th wheel.
|
||||
*/
|
||||
public wheelContactPoint(i: number): Vector | null {
|
||||
return VectorOps.fromRaw(this.raw.wheel_contact_point_ws(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* The suspension length for the i-th wheel.
|
||||
*/
|
||||
public wheelSuspensionLength(i: number): number | null {
|
||||
return this.raw.wheel_suspension_length(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The (world-space) starting point of the ray-cast for the i-th wheel.
|
||||
*/
|
||||
public wheelHardPoint(i: number): Vector | null {
|
||||
return VectorOps.fromRaw(this.raw.wheel_hard_point_ws(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the i-th wheel in contact with the ground?
|
||||
*/
|
||||
public wheelIsInContact(i: number): boolean {
|
||||
return this.raw.wheel_is_in_contact(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* The collider hit by the ray-cast for the i-th wheel.
|
||||
*/
|
||||
public wheelGroundObject(i: number): Collider | null {
|
||||
return this.colliders.get(this.raw.wheel_ground_object(i));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user