/** * The RigidBodyComponentSystem manages the physics simulation for all rigid body components * in the application. It creates and maintains the underlying Ammo.js physics world, handles * physics object creation and destruction, performs physics raycasting, detects and reports * collisions, and updates the transforms of entities with rigid bodies after each physics step. * * The system controls global physics settings like gravity and provides methods for raycasting * and collision detection. * * This system is only functional if your application has loaded the Ammo.js {@link WasmModule}. * * @category Physics */ export class RigidBodyComponentSystem extends ComponentSystem { /** * Fired when a contact occurs between two rigid bodies. The handler is passed a * {@link SingleContactResult} object containing details of the contact between the two bodies. * * @event * @example * app.systems.rigidbody.on('contact', (result) => { * console.log(`Contact between ${result.a.name} and ${result.b.name}`); * }); */ static EVENT_CONTACT: string; /** @ignore */ maxSubSteps: number; /** * @type {number} * @ignore */ fixedTimeStep: number; /** * The world space vector representing global gravity in the physics simulation. Defaults to * [0, -9.81, 0] which is an approximation of the gravitational force on Earth. * * @example * // Set the gravity in the physics world to simulate a planet with low gravity * app.systems.rigidbody.gravity = new pc.Vec3(0, -3.7, 0); */ gravity: Vec3; /** * @type {Float32Array} * @private */ private _gravityFloat32; /** * @type {RigidBodyComponent[]} * @private */ private _dynamic; /** * @type {RigidBodyComponent[]} * @private */ private _kinematic; /** * @type {Trigger[]} * @private */ private _triggers; /** * @type {CollisionComponent[]} * @private */ private _compounds; id: string; _stats: { fps: number; ms: number; dt: number; updateStart: number; updateTime: number; renderStart: number; renderTime: number; physicsStart: number; physicsTime: number; scriptUpdateStart: number; scriptUpdate: number; scriptPostUpdateStart: number; scriptPostUpdate: number; animUpdateStart: number; animUpdate: number; cullTime: number; sortTime: number; skinTime: number; morphTime: number; instancingTime: number; triangles: number; gsplats: number; gsplatSort: number; gsplatBufferCopy: number; otherPrimitives: number; shaders: number; materials: number; cameras: number; shadowMapUpdates: number; shadowMapTime: number; depthMapTime: number; forwardTime: number; lightClustersTime: number; lightClusters: number; _timeToCountFrames: number; _fpsAccum: number; }; ComponentType: typeof RigidBodyComponent; contactPointPool: ObjectPool; contactResultPool: ObjectPool; singleContactResultPool: ObjectPool; collisions: {}; frameCollisions: {}; /** * Called once Ammo has been loaded. Responsible for creating the physics world. * * @ignore */ onLibraryLoaded(): void; collisionConfiguration: any; dispatcher: any; overlappingPairCache: any; solver: any; dynamicsWorld: any; initializeComponentData(component: any, data: any): void; cloneComponent(entity: any, clone: any): import("../component.js").Component; onBeforeRemove(entity: any, component: any): void; addBody(body: any, group: any, mask: any): void; removeBody(body: any): void; createBody(mass: any, shape: any, transform: any): any; destroyBody(body: any): void; /** * Raycast the world and return the first entity the ray hits. Fire a ray into the world from * start to end, if the ray hits an entity with a collision component, it returns a * {@link RaycastResult}, otherwise returns null. * * @param {Vec3} start - The world space point where the ray starts. * @param {Vec3} end - The world space point where the ray ends. * @param {object} [options] - The additional options for the raycasting. * @param {number} [options.filterCollisionGroup] - Collision group to apply to the raycast. * @param {number} [options.filterCollisionMask] - Collision mask to apply to the raycast. * @param {any[]} [options.filterTags] - Tags filters. Defined the same way as a {@link Tags#has} * query but within an array. * @param {Function} [options.filterCallback] - Custom function to use to filter entities. * Must return true to proceed with result. Takes one argument: the entity to evaluate. * * @returns {RaycastResult|null} The result of the raycasting or null if there was no hit. */ raycastFirst(start: Vec3, end: Vec3, options?: { filterCollisionGroup?: number; filterCollisionMask?: number; filterTags?: any[]; filterCallback?: Function; }): RaycastResult | null; /** * Raycast the world and return all entities the ray hits. It returns an array of * {@link RaycastResult}, one for each hit. If no hits are detected, the returned array will be * of length 0. Results are sorted by distance with closest first. * * @param {Vec3} start - The world space point where the ray starts. * @param {Vec3} end - The world space point where the ray ends. * @param {object} [options] - The additional options for the raycasting. * @param {boolean} [options.sort] - Whether to sort raycast results based on distance with closest * first. Defaults to false. * @param {number} [options.filterCollisionGroup] - Collision group to apply to the raycast. * @param {number} [options.filterCollisionMask] - Collision mask to apply to the raycast. * @param {any[]} [options.filterTags] - Tags filters. Defined the same way as a {@link Tags#has} * query but within an array. * @param {Function} [options.filterCallback] - Custom function to use to filter entities. * Must return true to proceed with result. Takes the entity to evaluate as argument. * * @returns {RaycastResult[]} An array of raycast hit results (0 length if there were no hits). * * @example * // Return all results of a raycast between 0, 2, 2 and 0, -2, -2 * const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2)); * @example * // Return all results of a raycast between 0, 2, 2 and 0, -2, -2 * // where hit entity is tagged with `bird` OR `mammal` * const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2), { * filterTags: [ "bird", "mammal" ] * }); * @example * // Return all results of a raycast between 0, 2, 2 and 0, -2, -2 * // where hit entity has a `camera` component * const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2), { * filterCallback: (entity) => entity && entity.camera * }); * @example * // Return all results of a raycast between 0, 2, 2 and 0, -2, -2 * // where hit entity is tagged with (`carnivore` AND `mammal`) OR (`carnivore` AND `reptile`) * // and the entity has an `anim` component * const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2), { * filterTags: [ * [ "carnivore", "mammal" ], * [ "carnivore", "reptile" ] * ], * filterCallback: (entity) => entity && entity.anim * }); */ raycastAll(start: Vec3, end: Vec3, options?: { sort?: boolean; filterCollisionGroup?: number; filterCollisionMask?: number; filterTags?: any[]; filterCallback?: Function; }): RaycastResult[]; /** * Stores a collision between the entity and other in the contacts map and returns true if it * is a new collision. * * @param {Entity} entity - The entity. * @param {Entity} other - The entity that collides with the first entity. * @returns {boolean} True if this is a new collision, false otherwise. * @private */ private _storeCollision; _createContactPointFromAmmo(contactPoint: any): ContactPoint; _createReverseContactPointFromAmmo(contactPoint: any): ContactPoint; _createSingleContactResult(a: any, b: any, contactPoint: any): SingleContactResult; _createContactResult(other: any, contacts: any): ContactResult; /** * Removes collisions that no longer exist from the collisions list and fires collisionend * events to the related entities. * * @private */ private _cleanOldCollisions; /** * Removes any stored collision keyed to the given entity. Called when a collision component is * removed so the persistent collisions map does not retain a destroyed entity. A new entity * that later reuses the same GUID (for example after reloading the same scene) would otherwise * inherit the stale entry and never fire `triggerleave` / `collisionend`, because the cached * entity no longer has a trigger or body. * * @param {Entity} entity - The entity whose stored collision should be removed. * @ignore */ clearEntityCollisions(entity: Entity): void; /** * Returns true if the entity has a contact event attached and false otherwise. * * @param {Entity} entity - Entity to test. * @returns {boolean} True if the entity has a contact and false otherwise. * @private */ private _hasContactEvent; /** * Checks for collisions and fires collision events. * * @param {number} world - The pointer to the dynamics world that invoked this callback. * @param {number} timeStep - The amount of simulation time processed in the last simulation tick. * @private */ private _checkForCollisions; onUpdate(dt: any): void; } import { ComponentSystem } from '../system.js'; import { Vec3 } from '../../../core/math/vec3.js'; import { RigidBodyComponent } from './component.js'; import { ContactPoint } from './contact-point.js'; import { ObjectPool } from '../../../core/object-pool.js'; import { ContactResult } from './contact-result.js'; import { SingleContactResult } from './single-contact-result.js'; import { RaycastResult } from './raycast-result.js'; import type { Entity } from '../../entity.js';