Skip to content

Commit 6c3f3c9

Browse files
authored
Expose usePhysics hook (#229)
* feat(physics): introduce usePhysics hook and enhance physics context documentation - Added the `usePhysics` hook to provide access to physics state information, including whether physics is enabled and loaded, as well as any loading errors. - Updated the physics context type definitions with detailed JSDoc comments for better clarity. - Enhanced documentation for the `usePhysics` hook with usage examples and return value descriptions. - Exported the `usePhysics` hook and its type from the physics context module. * feat(physics): add usePhysics hook to access physics engine state
1 parent e7c7559 commit 6c3f3c9

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

.changeset/open-berries-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@playcanvas/react": minor
3+
---
4+
5+
Exposes a new usePhysics hook to determine physics engine state

packages/docs/content/docs/api/hooks/index.mdx

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,70 @@ useFrame((dt) => {
155155
useAppEvent('update', (dt) => {
156156
// your frame logic
157157
});
158-
```
158+
```
159+
160+
## usePhysics
161+
162+
The `usePhysics` hook provides access to physics context information, allowing you to check the physics state and handle physics-related errors. This hook is particularly useful when working with physics-enabled applications.
163+
164+
```jsx
165+
import { usePhysics } from '@playcanvas/react/hooks'
166+
167+
const PhysicsStatus = () => {
168+
const { isPhysicsEnabled, isPhysicsLoaded, physicsError } = usePhysics();
169+
170+
if (physicsError) {
171+
return <div>Physics error: {physicsError.message}</div>;
172+
}
173+
174+
if (!isPhysicsLoaded) {
175+
return <div>Loading physics...</div>;
176+
}
177+
178+
return <div>Physics is ready!</div>;
179+
};
180+
```
181+
182+
### Return Value
183+
184+
The hook returns an object with the following properties:
185+
186+
- **`isPhysicsEnabled`**: `boolean` - Whether physics is enabled on the application
187+
- **`isPhysicsLoaded`**: `boolean` - Whether the physics library has been successfully loaded
188+
- **`physicsError`**: `Error | null` - The error that occurred when loading physics, if any
189+
190+
### Usage with Physics Components
191+
192+
This hook is commonly used alongside physics components like `RigidBody` and `Collision` to provide user feedback about physics state:
193+
194+
```jsx
195+
import { usePhysics } from '@playcanvas/react/hooks'
196+
197+
const PhysicsAwareComponent = () => {
198+
const { isPhysicsEnabled, isPhysicsLoaded, physicsError } = usePhysics();
199+
200+
if (!isPhysicsEnabled) {
201+
console.warn('Physics is not enabled on this application');
202+
return null;
203+
}
204+
205+
if (physicsError) {
206+
console.error('Failed to load physics', physicsError);
207+
return null;
208+
}
209+
210+
if (!isPhysicsLoaded) {
211+
console.log('Loading physics library...');
212+
return null;
213+
}
214+
215+
return (
216+
<Entity>
217+
<RigidBody type="dynamic" />
218+
<Collision type="box" />
219+
</Entity>
220+
);
221+
};
222+
```
223+
224+
**Note**: This hook requires the application to have physics enabled via the `usePhysics` prop on the `Application` component.

packages/lib/src/contexts/physics-context.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import React, { createContext, useContext, useEffect, useState } from 'react';
22
import { AppBase } from 'playcanvas';
33

4-
interface PhysicsContextType {
4+
/**
5+
* Physics context type containing physics state information.
6+
*/
7+
export interface PhysicsContextType {
8+
/**
9+
* Whether physics is enabled on the application.
10+
*/
511
isPhysicsEnabled: boolean;
12+
/**
13+
* Whether the physics library has been successfully loaded.
14+
*/
615
isPhysicsLoaded: boolean;
16+
/**
17+
* The error that occurred when loading physics, if any.
18+
*/
719
physicsError: Error | null;
820
}
921

@@ -16,6 +28,30 @@ const PhysicsContext = createContext<PhysicsContextType>({
1628
// Track how many Application instances are using physics
1729
let physicsInstanceCount = 0;
1830

31+
/**
32+
* Hook to access physics context information.
33+
*
34+
* @returns Physics context containing physics state information
35+
*
36+
* @example
37+
* ```tsx
38+
* import { usePhysics } from '@playcanvas/react/hooks';
39+
*
40+
* const MyComponent = () => {
41+
* const { isPhysicsEnabled, isPhysicsLoaded, physicsError } = usePhysics();
42+
*
43+
* if (physicsError) {
44+
* return <div>Physics error: {physicsError.message}</div>;
45+
* }
46+
*
47+
* if (!isPhysicsLoaded) {
48+
* return <div>Loading physics...</div>;
49+
* }
50+
*
51+
* return <div>Physics is ready!</div>;
52+
* };
53+
* ```
54+
*/
1955
export const usePhysics = () => useContext(PhysicsContext);
2056

2157
interface PhysicsProviderProps {

packages/lib/src/hooks/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export { useParent, ParentContext } from './use-parent.tsx';
77
export { useMaterial } from './use-material.tsx'
88
export { useAsset, useSplat, useTexture, useEnvAtlas, useModel, useFont } from './use-asset.ts';
99
export { useFrame, useAppEvent } from './use-app-event.ts';
10-
export type { AssetResult } from './use-asset.ts';
10+
export type { AssetResult } from './use-asset.ts';
11+
export { usePhysics, type PhysicsContextType } from '../contexts/physics-context.tsx';

0 commit comments

Comments
 (0)