Skip to content

Commit b8b3830

Browse files
authored
Improve physics API docs (#7553)
* Improve physics API docs * Lint fixes * Whitespace
1 parent 34fc37e commit b8b3830

File tree

2 files changed

+100
-11
lines changed

2 files changed

+100
-11
lines changed

src/core/wasm-module.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,26 @@ class Impl {
124124
*/
125125

126126
/**
127-
* A pure static utility class which supports immediate and lazy loading of wasm modules.
127+
* A pure static utility class which supports immediate and lazy loading of
128+
* [WebAssembly](https://developer.mozilla.org/en-US/docs/WebAssembly) modules. Note that you can
129+
* load WebAssembly modules even before instantiating your {@link AppBase} instance.
130+
*
131+
* This class is generally only needed if you are developing against the Engine directly. Editor
132+
* projects automatically load WebAssembly modules included in the project's assets.
133+
*
134+
* Do not use this class to load the Basis WebAssembly module. Instead, please refer to
135+
* {@link basisInitialize}.
136+
*
137+
* @example
138+
* // Load the Ammo.js physics engine
139+
* pc.WasmModule.setConfig('Ammo', {
140+
* glueUrl: `ammo.wasm.js`,
141+
* wasmUrl: `ammo.wasm.wasm`,
142+
* fallbackUrl: `ammo.js`
143+
* });
144+
* await new Promise((resolve) => {
145+
* pc.WasmModule.getInstance('Ammo', () => resolve());
146+
* });
128147
*/
129148
class WasmModule {
130149
/**

src/framework/components/rigid-body/system.js

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import { RigidBodyComponentData } from './data.js';
1616
let ammoRayStart, ammoRayEnd;
1717

1818
/**
19-
* Object holding the result of a successful raycast hit.
19+
* Contains the result of a successful raycast intersection with a rigid body. When a ray
20+
* intersects with a rigid body in the physics simulation, this class stores the complete
21+
* information about that intersection including the entity, the exact point of impact, the normal
22+
* at the impact point, and the fractional distance along the ray where the intersection occurred.
23+
*
24+
* Instances of this class are created and returned by {@link RigidBodyComponentSystem#raycastFirst}
25+
* and {@link RigidBodyComponentSystem#raycastAll} methods when performing physics raycasts.
2026
*
2127
* @category Physics
2228
*/
@@ -69,8 +75,20 @@ class RaycastResult {
6975
}
7076

7177
/**
72-
* Object holding the result of a contact between two rigid bodies.
78+
* Represents the detailed data of a single contact point between two rigid bodies in the physics
79+
* simulation. This class provides comprehensive information about the contact, including the
80+
* entities involved, the exact contact points in both local and world space coordinates, the
81+
* contact normal, and the collision impulse force.
82+
*
83+
* Instances of this class are created by the physics engine when collision events occur and are
84+
* passed to event handlers only through the global `contact` event on the
85+
* {@link RigidBodyComponentSystem}. Individual rigid body components receive instances of
86+
* {@link ContactResult} instead.
7387
*
88+
* @example
89+
* app.systems.rigidbody.on('contact', (result) => {
90+
* console.log(`Contact between ${result.a.name} and ${result.b.name}`);
91+
* });
7492
* @category Physics
7593
*/
7694
class SingleContactResult {
@@ -163,7 +181,33 @@ class SingleContactResult {
163181
}
164182

165183
/**
166-
* Object holding the result of a contact between two Entities.
184+
* Represents a single point of contact between two colliding rigid bodies in the physics
185+
* simulation. Each contact point stores detailed spatial information about the collision,
186+
* including both local and world space coordinates of the exact contact points on both entities,
187+
* the contact normal direction, and the collision impulse force.
188+
*
189+
* Contact points are generated by the physics engine during collision detection and are typically
190+
* accessed through a {@link ContactResult} object, which can contain multiple contact points for a
191+
* single collision between two entities. Multiple contact points commonly occur when objects
192+
* collide along edges or faces rather than at a single point.
193+
*
194+
* The impulse property can be particularly useful for gameplay mechanics that need to respond
195+
* differently based on the force of impact, such as damage calculations or sound effect volume.
196+
*
197+
* @example
198+
* // Access contact points from a collision event
199+
* entity.collision.on('contact', (result) => {
200+
* // Get the first contact point
201+
* const contact = result.contacts[0];
202+
*
203+
* // Get the contact position in world space
204+
* const worldPos = contact.point;
205+
*
206+
* // Check how hard the collision was
207+
* if (contact.impulse > 10) {
208+
* console.log("That was a hard impact!");
209+
* }
210+
* });
167211
*
168212
* @category Physics
169213
*/
@@ -197,15 +241,16 @@ class ContactPoint {
197241
pointOther;
198242

199243
/**
200-
* The normal vector of the contact on the other entity, in world space.
244+
* The normal vector of the contact on the other entity, in world space. This vector points
245+
* away from the surface of the other entity at the point of contact.
201246
*
202247
* @type {Vec3}
203248
*/
204249
normal;
205250

206251
/**
207252
* The total accumulated impulse applied by the constraint solver during the last sub-step.
208-
* Describes how hard two objects collide.
253+
* This value represents how hard two objects collided. Higher values indicate stronger impacts.
209254
*
210255
* @type {number}
211256
*/
@@ -238,7 +283,24 @@ class ContactPoint {
238283
}
239284

240285
/**
241-
* Object holding the result of a contact between two Entities.
286+
* Represents a collection of contact points between two entities in a physics collision.
287+
* When rigid bodies collide, this object stores the entity involved in the collision and
288+
* an array of specific contact points where the collision occurred. This information is
289+
* used by the physics system to resolve collisions and notify components through events.
290+
*
291+
* Instances of this class are passed to event handlers for the `contact` and `collisionstart`
292+
* events on individual {@link RigidBodyComponent} and {@link CollisionComponent} instances.
293+
*
294+
* Unlike {@link SingleContactResult} which is used for global contact events, ContactResult
295+
* objects provide information about collision from the perspective of one entity, with
296+
* information about which other entity was involved and all points of contact.
297+
*
298+
* Please refer to the following event documentation for more information:
299+
*
300+
* - {@link CollisionComponent.EVENT_CONTACT}
301+
* - {@link CollisionComponent.EVENT_COLLISIONSTART}
302+
* - {@link RigidBodyComponent.EVENT_CONTACT}
303+
* - {@link RigidBodyComponent.EVENT_COLLISIONSTART}
242304
*
243305
* @category Physics
244306
*/
@@ -273,10 +335,15 @@ class ContactResult {
273335
const _schema = ['enabled'];
274336

275337
/**
276-
* The RigidBodyComponentSystem maintains the dynamics world for simulating rigid bodies, it also
277-
* controls global values for the world such as gravity. Note: The RigidBodyComponentSystem is only
278-
* valid if 3D Physics is enabled in your application. You can enable this in the application
279-
* settings for your project.
338+
* The RigidBodyComponentSystem manages the physics simulation for all rigid body components
339+
* in the application. It creates and maintains the underlying Ammo.js physics world, handles
340+
* physics object creation and destruction, performs physics raycasting, detects and reports
341+
* collisions, and updates the transforms of entities with rigid bodies after each physics step.
342+
*
343+
* The system controls global physics settings like gravity and provides methods for raycasting
344+
* and collision detection.
345+
*
346+
* This system is only functional if your application has loaded the Ammo.js {@link WasmModule}.
280347
*
281348
* @category Physics
282349
*/
@@ -310,6 +377,9 @@ class RigidBodyComponentSystem extends ComponentSystem {
310377
* [0, -9.81, 0] which is an approximation of the gravitational force on Earth.
311378
*
312379
* @type {Vec3}
380+
* @example
381+
* // Set the gravity in the physics world to simulate a planet with low gravity
382+
* app.systems.rigidbody.gravity = new pc.Vec3(0, -3.7, 0);
313383
*/
314384
gravity = new Vec3(0, -9.81, 0);
315385

0 commit comments

Comments
 (0)