playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
144 lines (143 loc) • 5.32 kB
TypeScript
/**
* @import { Entity } from '../entity.js'
* @import { Quat } from '../../core/math/quat.js'
* @import { Vec3 } from '../../core/math/vec3.js'
*/
/**
* The base class for a rigid body owned by a {@link PhysicsWorld}. Backends subclass it and
* override every method. The base implementation is inert: setters do nothing and getters
* leave their out-parameters untouched, so callers fall back to their own cached values -
* matching the engine's behavior when no physics library is loaded.
*
* @ignore
*/
export class PhysicsBody {
/**
* The entity this body simulates. Surfaced by raycast and contact results.
*
* @type {Entity|null}
*/
entity: Entity | null;
/**
* The backend-native body object - btRigidBody when the Ammo backend is active, null
* otherwise. Surfaced by RigidBodyComponent#body.
*
* @type {object|null}
*/
nativeBody: object | null;
/**
* @param {number} friction - The friction value used when contacts occur.
*/
setFriction(friction: number): void;
/**
* @param {number} friction - The torsional friction orthogonal to the contact point.
*/
setRollingFriction(friction: number): void;
/**
* @param {number} restitution - The amount of energy preserved on collision.
*/
setRestitution(restitution: number): void;
/**
* Sets both damping values in one call.
*
* @param {number} linear - The rate at which the body loses linear velocity.
* @param {number} angular - The rate at which the body loses angular velocity.
*/
setDamping(linear: number, angular: number): void;
/**
* @param {Vec3} factor - The scaling factor for linear movement per axis.
*/
setLinearFactor(factor: Vec3): void;
/**
* @param {Vec3} factor - The scaling factor for angular movement per axis.
*/
setAngularFactor(factor: Vec3): void;
/**
* @param {Vec3} velocity - The world space linear velocity.
*/
setLinearVelocity(velocity: Vec3): void;
/**
* Reads the body's linear velocity into an out-parameter. Inert backends leave it
* untouched.
*
* @param {Vec3} velocity - The vector to write the linear velocity to.
*/
getLinearVelocity(velocity: Vec3): void;
/**
* @param {Vec3} velocity - The world space angular velocity.
*/
setAngularVelocity(velocity: Vec3): void;
/**
* Reads the body's angular velocity into an out-parameter. Inert backends leave it
* untouched.
*
* @param {Vec3} velocity - The vector to write the angular velocity to.
*/
getAngularVelocity(velocity: Vec3): void;
/**
* Sets the body mass and recomputes its inertia from the current collision shape.
*
* @param {number} mass - The new mass.
*/
setMass(mass: number): void;
/**
* Returns true if the body is actively simulating, i.e. not sleeping.
*
* @returns {boolean} True if the body is active.
*/
isActive(): boolean;
/**
* Forcibly wakes the body.
*/
activate(): void;
/**
* Teleports the body to a new world space pose and wakes it. Backends also refresh any
* interpolation state so the pose read back by {@link PhysicsBody#getTransform} is the
* teleport target even on frames that run zero fixed substeps.
*
* @param {Vec3} position - The world space position.
* @param {Quat} rotation - The world space rotation.
*/
setTransform(position: Vec3, rotation: Quat): void;
/**
* Reads the simulated pose to present to the scene (the interpolated transform on backends
* that interpolate). Inert backends leave the out-parameters untouched.
*
* @param {Vec3} position - The vector to write the world space position to.
* @param {Quat} rotation - The quaternion to write the world space rotation to.
*/
getTransform(position: Vec3, rotation: Quat): void;
/**
* Drives a kinematic body towards a world space pose for the next simulation step.
*
* @param {Vec3} position - The world space position.
* @param {Quat} rotation - The world space rotation.
*/
setKinematicTarget(position: Vec3, rotation: Quat): void;
/**
* Applies a force at a point relative to the body's origin. The body is not woken - callers
* activate first, matching the engine's established call order.
*
* @param {Vec3} force - The world space force.
* @param {Vec3} relativePoint - The world space offset from the body origin.
*/
applyForce(force: Vec3, relativePoint: Vec3): void;
/**
* @param {Vec3} torque - The world space torque.
*/
applyTorque(torque: Vec3): void;
/**
* Applies an impulse at a point relative to the body's origin.
*
* @param {Vec3} impulse - The world space impulse.
* @param {Vec3} relativePoint - The local space offset from the body origin.
*/
applyImpulse(impulse: Vec3, relativePoint: Vec3): void;
/**
* @param {Vec3} torque - The world space torque impulse.
*/
applyTorqueImpulse(torque: Vec3): void;
}
import type { Entity } from '../entity.js';
import type { Vec3 } from '../../core/math/vec3.js';
import type { Quat } from '../../core/math/quat.js';