@rapierphysicsplugin/server 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# @rapierphysicsplugin/server
|
|
2
|
+
|
|
3
|
+
Server-authoritative physics simulation server powered by [Rapier](https://rapier.rs/). Runs the physics world, accepts client connections over WebSocket, and broadcasts state updates to all connected clients.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @rapierphysicsplugin/server @rapierphysicsplugin/shared
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { loadRapier, ComputeBackend } from '@rapierphysicsplugin/shared';
|
|
15
|
+
import { PhysicsServer } from '@rapierphysicsplugin/server';
|
|
16
|
+
|
|
17
|
+
// 1. Load Rapier WASM (SIMD for best performance)
|
|
18
|
+
const RAPIER = await loadRapier({ backend: ComputeBackend.WASM_SIMD });
|
|
19
|
+
|
|
20
|
+
// 2. Create and start the server
|
|
21
|
+
const server = new PhysicsServer(RAPIER);
|
|
22
|
+
await server.start(8080);
|
|
23
|
+
|
|
24
|
+
// 3. Create a room
|
|
25
|
+
const roomManager = server.getRoomManager();
|
|
26
|
+
roomManager.createRoom('my-room');
|
|
27
|
+
|
|
28
|
+
// 4. Graceful shutdown
|
|
29
|
+
process.on('SIGINT', () => {
|
|
30
|
+
server.stop();
|
|
31
|
+
process.exit(0);
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Clients connect via WebSocket, join a room, and the server handles everything automatically — physics stepping, state broadcasting, input processing, and body synchronization.
|
|
36
|
+
|
|
37
|
+
## How It Works
|
|
38
|
+
|
|
39
|
+
1. **Client connects** via WebSocket and sends a `JOIN_ROOM` message
|
|
40
|
+
2. **Server sends** a full state snapshot and replays existing body/constraint descriptors
|
|
41
|
+
3. **Physics loop** runs at 60 Hz (`SERVER_TICK_RATE`)
|
|
42
|
+
4. **State broadcasts** are sent at 20 Hz (`BROADCAST_RATE`) with delta compression
|
|
43
|
+
5. **Client inputs** (forces, impulses, velocity changes) are applied to the physics world each tick
|
|
44
|
+
6. **Collision events** are batched and broadcast to all clients in the room
|
|
45
|
+
|
|
46
|
+
## Pre-loading Bodies
|
|
47
|
+
|
|
48
|
+
You can load an initial state into a room before clients connect:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const room = roomManager.createRoom('my-room');
|
|
52
|
+
|
|
53
|
+
room.loadInitialState([
|
|
54
|
+
{
|
|
55
|
+
id: 'ground',
|
|
56
|
+
shape: 'box',
|
|
57
|
+
halfExtents: { x: 10, y: 0.5, z: 10 },
|
|
58
|
+
motionType: 'static',
|
|
59
|
+
position: { x: 0, y: -0.5, z: 0 },
|
|
60
|
+
friction: 0.8,
|
|
61
|
+
restitution: 0.3,
|
|
62
|
+
},
|
|
63
|
+
]);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## SIMD Backend
|
|
67
|
+
|
|
68
|
+
For best performance, use the SIMD backend. Set via environment variable:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
PHYSICS_BACKEND=wasm-simd node server.js
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Or pass it directly:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const RAPIER = await loadRapier({ backend: ComputeBackend.WASM_SIMD });
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Falls back to `wasm-compat` automatically if SIMD is unavailable.
|
|
81
|
+
|
|
82
|
+
## Architecture
|
|
83
|
+
|
|
84
|
+
| Class | Purpose |
|
|
85
|
+
|-------|---------|
|
|
86
|
+
| `PhysicsServer` | WebSocket server, routes messages to rooms |
|
|
87
|
+
| `PhysicsWorld` | Rapier world wrapper — steps physics, applies inputs, generates snapshots |
|
|
88
|
+
| `Room` | Manages a physics world + connected clients, handles body sync |
|
|
89
|
+
| `RoomManager` | Creates and retrieves rooms |
|
|
90
|
+
| `SimulationLoop` | Runs the physics tick loop at 60 Hz |
|
|
91
|
+
| `StateManager` | Generates full snapshots and delta-compressed state updates |
|
|
92
|
+
| `InputBuffer` | Per-client input queue, executes inputs in tick order |
|
|
93
|
+
| `ClientConnection` | Per-client WebSocket wrapper |
|
|
94
|
+
|
|
95
|
+
## Dependencies
|
|
96
|
+
|
|
97
|
+
- `@dimforge/rapier3d-compat` — Rapier WASM physics engine
|
|
98
|
+
- `@dimforge/rapier3d-simd-compat` (optional) — SIMD-accelerated build
|
|
99
|
+
- `ws` — WebSocket server
|
|
100
|
+
- `@rapierphysicsplugin/shared` — types and serialization
|