@newkrok/nape-js 3.7.6 → 3.8.0
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 +24 -1
- package/dist/ConvexResult-CYFNMlT2.d.cts +2690 -0
- package/dist/ConvexResult-CYFNMlT2.d.ts +2690 -0
- package/dist/chunk-3TXNIYBK.js +2 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +120 -2670
- package/dist/index.d.ts +120 -2670
- package/dist/index.js +1 -1
- package/dist/serialization/index.cjs +2 -0
- package/dist/serialization/index.d.cts +230 -0
- package/dist/serialization/index.d.ts +230 -0
- package/dist/serialization/index.js +2 -0
- package/package.json +11 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { T as Space } from '../ConvexResult-CYFNMlT2.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Snapshot types for the nape-js serialization API (P37).
|
|
5
|
+
*
|
|
6
|
+
* All types are plain JSON-serializable objects — no class instances, no
|
|
7
|
+
* circular references, no functions.
|
|
8
|
+
*/
|
|
9
|
+
/** Schema version — bumped on breaking snapshot format changes. */
|
|
10
|
+
declare const SNAPSHOT_VERSION = 1;
|
|
11
|
+
interface Vec2Data {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
}
|
|
15
|
+
interface MaterialData {
|
|
16
|
+
elasticity: number;
|
|
17
|
+
dynamicFriction: number;
|
|
18
|
+
staticFriction: number;
|
|
19
|
+
density: number;
|
|
20
|
+
rollingFriction: number;
|
|
21
|
+
}
|
|
22
|
+
interface FluidPropertiesData {
|
|
23
|
+
density: number;
|
|
24
|
+
viscosity: number;
|
|
25
|
+
gravity: Vec2Data | null;
|
|
26
|
+
}
|
|
27
|
+
interface InteractionFilterData {
|
|
28
|
+
collisionGroup: number;
|
|
29
|
+
collisionMask: number;
|
|
30
|
+
sensorGroup: number;
|
|
31
|
+
sensorMask: number;
|
|
32
|
+
fluidGroup: number;
|
|
33
|
+
fluidMask: number;
|
|
34
|
+
}
|
|
35
|
+
interface CircleShapeData {
|
|
36
|
+
type: "circle";
|
|
37
|
+
radius: number;
|
|
38
|
+
localCOM: Vec2Data;
|
|
39
|
+
material: MaterialData;
|
|
40
|
+
filter: InteractionFilterData;
|
|
41
|
+
sensorEnabled: boolean;
|
|
42
|
+
fluidEnabled: boolean;
|
|
43
|
+
fluidProperties: FluidPropertiesData | null;
|
|
44
|
+
}
|
|
45
|
+
interface PolygonShapeData {
|
|
46
|
+
type: "polygon";
|
|
47
|
+
localVerts: Vec2Data[];
|
|
48
|
+
material: MaterialData;
|
|
49
|
+
filter: InteractionFilterData;
|
|
50
|
+
sensorEnabled: boolean;
|
|
51
|
+
fluidEnabled: boolean;
|
|
52
|
+
fluidProperties: FluidPropertiesData | null;
|
|
53
|
+
}
|
|
54
|
+
type ShapeData = CircleShapeData | PolygonShapeData;
|
|
55
|
+
type BodyTypeData = "DYNAMIC" | "STATIC" | "KINEMATIC";
|
|
56
|
+
type MassModeData = "DEFAULT" | "FIXED" | "FIXED_GROUP";
|
|
57
|
+
type InertiaModeData = "DEFAULT" | "FIXED" | "FIXED_GROUP";
|
|
58
|
+
type GravMassModeData = "DEFAULT" | "FIXED" | "SCALED";
|
|
59
|
+
interface BodyData {
|
|
60
|
+
/** Stable numeric ID used to reference this body from constraints. */
|
|
61
|
+
id: number;
|
|
62
|
+
type: BodyTypeData;
|
|
63
|
+
position: Vec2Data;
|
|
64
|
+
rotation: number;
|
|
65
|
+
velocity: Vec2Data;
|
|
66
|
+
angularVel: number;
|
|
67
|
+
kinematicVel: Vec2Data;
|
|
68
|
+
kinAngVel: number;
|
|
69
|
+
surfaceVel: Vec2Data;
|
|
70
|
+
force: Vec2Data;
|
|
71
|
+
torque: number;
|
|
72
|
+
massMode: MassModeData;
|
|
73
|
+
/** Only present when massMode is "FIXED". */
|
|
74
|
+
mass: number | null;
|
|
75
|
+
inertiaMode: InertiaModeData;
|
|
76
|
+
/** Only present when inertiaMode is "FIXED". */
|
|
77
|
+
inertia: number | null;
|
|
78
|
+
gravMassMode: GravMassModeData;
|
|
79
|
+
gravMassScale: number;
|
|
80
|
+
allowMovement: boolean;
|
|
81
|
+
allowRotation: boolean;
|
|
82
|
+
bullet: boolean;
|
|
83
|
+
shapes: ShapeData[];
|
|
84
|
+
userData: Record<string, unknown> | null;
|
|
85
|
+
}
|
|
86
|
+
/** Properties shared by every constraint type. */
|
|
87
|
+
interface ConstraintBaseData {
|
|
88
|
+
/** Index into SpaceSnapshot.bodies, or null for a static world anchor. */
|
|
89
|
+
body1Id: number | null;
|
|
90
|
+
/** Index into SpaceSnapshot.bodies, or null for a static world anchor. */
|
|
91
|
+
body2Id: number | null;
|
|
92
|
+
active: boolean;
|
|
93
|
+
ignore: boolean;
|
|
94
|
+
stiff: boolean;
|
|
95
|
+
frequency: number;
|
|
96
|
+
damping: number;
|
|
97
|
+
maxForce: number;
|
|
98
|
+
maxError: number;
|
|
99
|
+
breakUnderForce: boolean;
|
|
100
|
+
breakUnderError: boolean;
|
|
101
|
+
removeOnBreak: boolean;
|
|
102
|
+
userData: Record<string, unknown> | null;
|
|
103
|
+
}
|
|
104
|
+
interface PivotJointData extends ConstraintBaseData {
|
|
105
|
+
type: "PivotJoint";
|
|
106
|
+
anchor1: Vec2Data;
|
|
107
|
+
anchor2: Vec2Data;
|
|
108
|
+
}
|
|
109
|
+
interface DistanceJointData extends ConstraintBaseData {
|
|
110
|
+
type: "DistanceJoint";
|
|
111
|
+
anchor1: Vec2Data;
|
|
112
|
+
anchor2: Vec2Data;
|
|
113
|
+
jointMin: number;
|
|
114
|
+
jointMax: number;
|
|
115
|
+
}
|
|
116
|
+
interface AngleJointData extends ConstraintBaseData {
|
|
117
|
+
type: "AngleJoint";
|
|
118
|
+
jointMin: number;
|
|
119
|
+
jointMax: number;
|
|
120
|
+
ratio: number;
|
|
121
|
+
}
|
|
122
|
+
interface MotorJointData extends ConstraintBaseData {
|
|
123
|
+
type: "MotorJoint";
|
|
124
|
+
rate: number;
|
|
125
|
+
ratio: number;
|
|
126
|
+
}
|
|
127
|
+
interface LineJointData extends ConstraintBaseData {
|
|
128
|
+
type: "LineJoint";
|
|
129
|
+
anchor1: Vec2Data;
|
|
130
|
+
anchor2: Vec2Data;
|
|
131
|
+
direction: Vec2Data;
|
|
132
|
+
jointMin: number;
|
|
133
|
+
jointMax: number;
|
|
134
|
+
}
|
|
135
|
+
interface PulleyJointData extends ConstraintBaseData {
|
|
136
|
+
type: "PulleyJoint";
|
|
137
|
+
anchor1: Vec2Data;
|
|
138
|
+
anchor2: Vec2Data;
|
|
139
|
+
anchor3: Vec2Data;
|
|
140
|
+
anchor4: Vec2Data;
|
|
141
|
+
jointMin: number;
|
|
142
|
+
jointMax: number;
|
|
143
|
+
ratio: number;
|
|
144
|
+
}
|
|
145
|
+
interface WeldJointData extends ConstraintBaseData {
|
|
146
|
+
type: "WeldJoint";
|
|
147
|
+
anchor1: Vec2Data;
|
|
148
|
+
anchor2: Vec2Data;
|
|
149
|
+
phase: number;
|
|
150
|
+
}
|
|
151
|
+
type ConstraintData = PivotJointData | DistanceJointData | AngleJointData | MotorJointData | LineJointData | PulleyJointData | WeldJointData;
|
|
152
|
+
interface CompoundData {
|
|
153
|
+
/** Body IDs belonging to this compound. */
|
|
154
|
+
bodyIds: number[];
|
|
155
|
+
/** Constraint indices (into SpaceSnapshot.constraints) belonging to this compound. */
|
|
156
|
+
constraintIndices: number[];
|
|
157
|
+
/** Child compound indices (into SpaceSnapshot.compounds). */
|
|
158
|
+
childIndices: number[];
|
|
159
|
+
}
|
|
160
|
+
interface SpaceSnapshot {
|
|
161
|
+
version: typeof SNAPSHOT_VERSION;
|
|
162
|
+
gravity: Vec2Data;
|
|
163
|
+
worldLinearDrag: number;
|
|
164
|
+
worldAngularDrag: number;
|
|
165
|
+
sortContacts: boolean;
|
|
166
|
+
broadphase: "SWEEP_AND_PRUNE" | "DYNAMIC_AABB_TREE";
|
|
167
|
+
bodies: BodyData[];
|
|
168
|
+
constraints: ConstraintData[];
|
|
169
|
+
compounds: CompoundData[];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* spaceToJSON — converts a live Space into a plain JSON-serializable SpaceSnapshot.
|
|
174
|
+
*
|
|
175
|
+
* UserConstraint instances are skipped (not serializable).
|
|
176
|
+
* userData values are included only when JSON.stringify can round-trip them.
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Serialize the complete state of a Space into a plain JSON-serializable object.
|
|
181
|
+
*
|
|
182
|
+
* - Bodies (position, velocity, shapes, mass, etc.) are fully captured.
|
|
183
|
+
* - Constraints are captured except for UserConstraint (not serializable).
|
|
184
|
+
* - Compounds are captured as groupings of body IDs and constraint indices.
|
|
185
|
+
* - Arbiters (collision contacts) are NOT captured — they are reconstructed
|
|
186
|
+
* by the engine on the next simulation step.
|
|
187
|
+
* - userData is included only for fields that survive a JSON round-trip.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { spaceToJSON, spaceFromJSON } from '@newkrok/nape-js/serialization';
|
|
192
|
+
*
|
|
193
|
+
* const snapshot = spaceToJSON(space);
|
|
194
|
+
* const json = JSON.stringify(snapshot);
|
|
195
|
+
* // ... later:
|
|
196
|
+
* const restored = spaceFromJSON(JSON.parse(json));
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
declare function spaceToJSON(space: Space): SpaceSnapshot;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* spaceFromJSON — reconstructs a Space from a SpaceSnapshot.
|
|
203
|
+
*
|
|
204
|
+
* All bodies are created fresh; constraints are wired up after bodies so that
|
|
205
|
+
* body references are resolved correctly.
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Reconstruct a Space from a SpaceSnapshot.
|
|
210
|
+
*
|
|
211
|
+
* The returned Space is fully configured: bodies, shapes, constraints, and
|
|
212
|
+
* compounds are all added and ready for simulation. Call `space.step(dt)` to
|
|
213
|
+
* start simulating.
|
|
214
|
+
*
|
|
215
|
+
* @throws If the snapshot version is incompatible.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { spaceToJSON, spaceFromJSON } from '@newkrok/nape-js/serialization';
|
|
220
|
+
*
|
|
221
|
+
* const snapshot = spaceToJSON(space);
|
|
222
|
+
* const json = JSON.stringify(snapshot);
|
|
223
|
+
* // ... send over network / save to disk ...
|
|
224
|
+
* const restored = spaceFromJSON(JSON.parse(json));
|
|
225
|
+
* restored.step(1 / 60);
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
declare function spaceFromJSON(snapshot: SpaceSnapshot): Space;
|
|
229
|
+
|
|
230
|
+
export { type AngleJointData, type BodyData, type BodyTypeData, type CircleShapeData, type CompoundData, type ConstraintBaseData, type ConstraintData, type DistanceJointData, type FluidPropertiesData, type GravMassModeData, type InertiaModeData, type InteractionFilterData, type LineJointData, type MassModeData, type MaterialData, type MotorJointData, type PivotJointData, type PolygonShapeData, type PulleyJointData, SNAPSHOT_VERSION, type ShapeData, type SpaceSnapshot, type Vec2Data, type WeldJointData, spaceFromJSON, spaceToJSON };
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { T as Space } from '../ConvexResult-CYFNMlT2.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Snapshot types for the nape-js serialization API (P37).
|
|
5
|
+
*
|
|
6
|
+
* All types are plain JSON-serializable objects — no class instances, no
|
|
7
|
+
* circular references, no functions.
|
|
8
|
+
*/
|
|
9
|
+
/** Schema version — bumped on breaking snapshot format changes. */
|
|
10
|
+
declare const SNAPSHOT_VERSION = 1;
|
|
11
|
+
interface Vec2Data {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
}
|
|
15
|
+
interface MaterialData {
|
|
16
|
+
elasticity: number;
|
|
17
|
+
dynamicFriction: number;
|
|
18
|
+
staticFriction: number;
|
|
19
|
+
density: number;
|
|
20
|
+
rollingFriction: number;
|
|
21
|
+
}
|
|
22
|
+
interface FluidPropertiesData {
|
|
23
|
+
density: number;
|
|
24
|
+
viscosity: number;
|
|
25
|
+
gravity: Vec2Data | null;
|
|
26
|
+
}
|
|
27
|
+
interface InteractionFilterData {
|
|
28
|
+
collisionGroup: number;
|
|
29
|
+
collisionMask: number;
|
|
30
|
+
sensorGroup: number;
|
|
31
|
+
sensorMask: number;
|
|
32
|
+
fluidGroup: number;
|
|
33
|
+
fluidMask: number;
|
|
34
|
+
}
|
|
35
|
+
interface CircleShapeData {
|
|
36
|
+
type: "circle";
|
|
37
|
+
radius: number;
|
|
38
|
+
localCOM: Vec2Data;
|
|
39
|
+
material: MaterialData;
|
|
40
|
+
filter: InteractionFilterData;
|
|
41
|
+
sensorEnabled: boolean;
|
|
42
|
+
fluidEnabled: boolean;
|
|
43
|
+
fluidProperties: FluidPropertiesData | null;
|
|
44
|
+
}
|
|
45
|
+
interface PolygonShapeData {
|
|
46
|
+
type: "polygon";
|
|
47
|
+
localVerts: Vec2Data[];
|
|
48
|
+
material: MaterialData;
|
|
49
|
+
filter: InteractionFilterData;
|
|
50
|
+
sensorEnabled: boolean;
|
|
51
|
+
fluidEnabled: boolean;
|
|
52
|
+
fluidProperties: FluidPropertiesData | null;
|
|
53
|
+
}
|
|
54
|
+
type ShapeData = CircleShapeData | PolygonShapeData;
|
|
55
|
+
type BodyTypeData = "DYNAMIC" | "STATIC" | "KINEMATIC";
|
|
56
|
+
type MassModeData = "DEFAULT" | "FIXED" | "FIXED_GROUP";
|
|
57
|
+
type InertiaModeData = "DEFAULT" | "FIXED" | "FIXED_GROUP";
|
|
58
|
+
type GravMassModeData = "DEFAULT" | "FIXED" | "SCALED";
|
|
59
|
+
interface BodyData {
|
|
60
|
+
/** Stable numeric ID used to reference this body from constraints. */
|
|
61
|
+
id: number;
|
|
62
|
+
type: BodyTypeData;
|
|
63
|
+
position: Vec2Data;
|
|
64
|
+
rotation: number;
|
|
65
|
+
velocity: Vec2Data;
|
|
66
|
+
angularVel: number;
|
|
67
|
+
kinematicVel: Vec2Data;
|
|
68
|
+
kinAngVel: number;
|
|
69
|
+
surfaceVel: Vec2Data;
|
|
70
|
+
force: Vec2Data;
|
|
71
|
+
torque: number;
|
|
72
|
+
massMode: MassModeData;
|
|
73
|
+
/** Only present when massMode is "FIXED". */
|
|
74
|
+
mass: number | null;
|
|
75
|
+
inertiaMode: InertiaModeData;
|
|
76
|
+
/** Only present when inertiaMode is "FIXED". */
|
|
77
|
+
inertia: number | null;
|
|
78
|
+
gravMassMode: GravMassModeData;
|
|
79
|
+
gravMassScale: number;
|
|
80
|
+
allowMovement: boolean;
|
|
81
|
+
allowRotation: boolean;
|
|
82
|
+
bullet: boolean;
|
|
83
|
+
shapes: ShapeData[];
|
|
84
|
+
userData: Record<string, unknown> | null;
|
|
85
|
+
}
|
|
86
|
+
/** Properties shared by every constraint type. */
|
|
87
|
+
interface ConstraintBaseData {
|
|
88
|
+
/** Index into SpaceSnapshot.bodies, or null for a static world anchor. */
|
|
89
|
+
body1Id: number | null;
|
|
90
|
+
/** Index into SpaceSnapshot.bodies, or null for a static world anchor. */
|
|
91
|
+
body2Id: number | null;
|
|
92
|
+
active: boolean;
|
|
93
|
+
ignore: boolean;
|
|
94
|
+
stiff: boolean;
|
|
95
|
+
frequency: number;
|
|
96
|
+
damping: number;
|
|
97
|
+
maxForce: number;
|
|
98
|
+
maxError: number;
|
|
99
|
+
breakUnderForce: boolean;
|
|
100
|
+
breakUnderError: boolean;
|
|
101
|
+
removeOnBreak: boolean;
|
|
102
|
+
userData: Record<string, unknown> | null;
|
|
103
|
+
}
|
|
104
|
+
interface PivotJointData extends ConstraintBaseData {
|
|
105
|
+
type: "PivotJoint";
|
|
106
|
+
anchor1: Vec2Data;
|
|
107
|
+
anchor2: Vec2Data;
|
|
108
|
+
}
|
|
109
|
+
interface DistanceJointData extends ConstraintBaseData {
|
|
110
|
+
type: "DistanceJoint";
|
|
111
|
+
anchor1: Vec2Data;
|
|
112
|
+
anchor2: Vec2Data;
|
|
113
|
+
jointMin: number;
|
|
114
|
+
jointMax: number;
|
|
115
|
+
}
|
|
116
|
+
interface AngleJointData extends ConstraintBaseData {
|
|
117
|
+
type: "AngleJoint";
|
|
118
|
+
jointMin: number;
|
|
119
|
+
jointMax: number;
|
|
120
|
+
ratio: number;
|
|
121
|
+
}
|
|
122
|
+
interface MotorJointData extends ConstraintBaseData {
|
|
123
|
+
type: "MotorJoint";
|
|
124
|
+
rate: number;
|
|
125
|
+
ratio: number;
|
|
126
|
+
}
|
|
127
|
+
interface LineJointData extends ConstraintBaseData {
|
|
128
|
+
type: "LineJoint";
|
|
129
|
+
anchor1: Vec2Data;
|
|
130
|
+
anchor2: Vec2Data;
|
|
131
|
+
direction: Vec2Data;
|
|
132
|
+
jointMin: number;
|
|
133
|
+
jointMax: number;
|
|
134
|
+
}
|
|
135
|
+
interface PulleyJointData extends ConstraintBaseData {
|
|
136
|
+
type: "PulleyJoint";
|
|
137
|
+
anchor1: Vec2Data;
|
|
138
|
+
anchor2: Vec2Data;
|
|
139
|
+
anchor3: Vec2Data;
|
|
140
|
+
anchor4: Vec2Data;
|
|
141
|
+
jointMin: number;
|
|
142
|
+
jointMax: number;
|
|
143
|
+
ratio: number;
|
|
144
|
+
}
|
|
145
|
+
interface WeldJointData extends ConstraintBaseData {
|
|
146
|
+
type: "WeldJoint";
|
|
147
|
+
anchor1: Vec2Data;
|
|
148
|
+
anchor2: Vec2Data;
|
|
149
|
+
phase: number;
|
|
150
|
+
}
|
|
151
|
+
type ConstraintData = PivotJointData | DistanceJointData | AngleJointData | MotorJointData | LineJointData | PulleyJointData | WeldJointData;
|
|
152
|
+
interface CompoundData {
|
|
153
|
+
/** Body IDs belonging to this compound. */
|
|
154
|
+
bodyIds: number[];
|
|
155
|
+
/** Constraint indices (into SpaceSnapshot.constraints) belonging to this compound. */
|
|
156
|
+
constraintIndices: number[];
|
|
157
|
+
/** Child compound indices (into SpaceSnapshot.compounds). */
|
|
158
|
+
childIndices: number[];
|
|
159
|
+
}
|
|
160
|
+
interface SpaceSnapshot {
|
|
161
|
+
version: typeof SNAPSHOT_VERSION;
|
|
162
|
+
gravity: Vec2Data;
|
|
163
|
+
worldLinearDrag: number;
|
|
164
|
+
worldAngularDrag: number;
|
|
165
|
+
sortContacts: boolean;
|
|
166
|
+
broadphase: "SWEEP_AND_PRUNE" | "DYNAMIC_AABB_TREE";
|
|
167
|
+
bodies: BodyData[];
|
|
168
|
+
constraints: ConstraintData[];
|
|
169
|
+
compounds: CompoundData[];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* spaceToJSON — converts a live Space into a plain JSON-serializable SpaceSnapshot.
|
|
174
|
+
*
|
|
175
|
+
* UserConstraint instances are skipped (not serializable).
|
|
176
|
+
* userData values are included only when JSON.stringify can round-trip them.
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Serialize the complete state of a Space into a plain JSON-serializable object.
|
|
181
|
+
*
|
|
182
|
+
* - Bodies (position, velocity, shapes, mass, etc.) are fully captured.
|
|
183
|
+
* - Constraints are captured except for UserConstraint (not serializable).
|
|
184
|
+
* - Compounds are captured as groupings of body IDs and constraint indices.
|
|
185
|
+
* - Arbiters (collision contacts) are NOT captured — they are reconstructed
|
|
186
|
+
* by the engine on the next simulation step.
|
|
187
|
+
* - userData is included only for fields that survive a JSON round-trip.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { spaceToJSON, spaceFromJSON } from '@newkrok/nape-js/serialization';
|
|
192
|
+
*
|
|
193
|
+
* const snapshot = spaceToJSON(space);
|
|
194
|
+
* const json = JSON.stringify(snapshot);
|
|
195
|
+
* // ... later:
|
|
196
|
+
* const restored = spaceFromJSON(JSON.parse(json));
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
declare function spaceToJSON(space: Space): SpaceSnapshot;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* spaceFromJSON — reconstructs a Space from a SpaceSnapshot.
|
|
203
|
+
*
|
|
204
|
+
* All bodies are created fresh; constraints are wired up after bodies so that
|
|
205
|
+
* body references are resolved correctly.
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Reconstruct a Space from a SpaceSnapshot.
|
|
210
|
+
*
|
|
211
|
+
* The returned Space is fully configured: bodies, shapes, constraints, and
|
|
212
|
+
* compounds are all added and ready for simulation. Call `space.step(dt)` to
|
|
213
|
+
* start simulating.
|
|
214
|
+
*
|
|
215
|
+
* @throws If the snapshot version is incompatible.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { spaceToJSON, spaceFromJSON } from '@newkrok/nape-js/serialization';
|
|
220
|
+
*
|
|
221
|
+
* const snapshot = spaceToJSON(space);
|
|
222
|
+
* const json = JSON.stringify(snapshot);
|
|
223
|
+
* // ... send over network / save to disk ...
|
|
224
|
+
* const restored = spaceFromJSON(JSON.parse(json));
|
|
225
|
+
* restored.step(1 / 60);
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
declare function spaceFromJSON(snapshot: SpaceSnapshot): Space;
|
|
229
|
+
|
|
230
|
+
export { type AngleJointData, type BodyData, type BodyTypeData, type CircleShapeData, type CompoundData, type ConstraintBaseData, type ConstraintData, type DistanceJointData, type FluidPropertiesData, type GravMassModeData, type InertiaModeData, type InteractionFilterData, type LineJointData, type MassModeData, type MaterialData, type MotorJointData, type PivotJointData, type PolygonShapeData, type PulleyJointData, SNAPSHOT_VERSION, type ShapeData, type SpaceSnapshot, type Vec2Data, type WeldJointData, spaceFromJSON, spaceToJSON };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{$ as S,H as v,T as k,X as j,_ as T,aa as N,ba as L,ca as A,da as O,ea as _,fa as U,ga as R,ha as P,ia as G,ka as W,la as X,ma as q,na as z,oa as Y,pa as H,qa as K}from"../chunk-3TXNIYBK.js";var J=1;function s(t){return{x:t.x,y:t.y}}function $(t){if(t==null)return null;try{let e=JSON.stringify(t);return e==="{}"?null:JSON.parse(e)}catch{return null}}function ot(t){return{elasticity:t.elasticity,dynamicFriction:t.dynamicFriction,staticFriction:t.staticFriction,density:t.density,rollingFriction:t.rollingFriction}}function et(t){return{collisionGroup:t.collisionGroup,collisionMask:t.collisionMask,sensorGroup:t.sensorGroup,sensorMask:t.sensorMask,fluidGroup:t.fluidGroup,fluidMask:t.fluidMask}}function at(t){if(t==null)return null;let e=t.gravity;return{density:t.density,viscosity:t.viscosity,gravity:e!=null?s(e):null}}function rt(t){let e=ot(t.material),o=et(t.filter),n=t.fluidEnabled,a=n?at(t.fluidProperties):null,u=t.sensorEnabled;if(t.isCircle()){let p=t,r=t.localCOM;return{type:"circle",radius:p.radius,localCOM:s(r),material:e,filter:o,sensorEnabled:u,fluidEnabled:n,fluidProperties:a}}else{let r=t.localVerts,m=[],c=r.length;for(let d=0;d<c;d++)m.push(s(r.at(d)));return{type:"polygon",localVerts:m,material:e,filter:o,sensorEnabled:u,fluidEnabled:n,fluidProperties:a}}}var it={1:"STATIC",2:"DYNAMIC",3:"KINEMATIC"},st={0:"DEFAULT",1:"FIXED",2:"FIXED_GROUP"},ct={0:"DEFAULT",1:"FIXED",2:"FIXED_GROUP"},lt={0:"DEFAULT",1:"FIXED",2:"SCALED"};function ut(t,e){let o=t.zpp_inner,n=st[o.massMode]??"DEFAULT",a=ct[o.inertiaMode]??"DEFAULT",u=lt[o.gravMassMode]??"DEFAULT",p=[],r=t.shapes,m=r.length;for(let c=0;c<m;c++)p.push(rt(r.at(c)));return{id:e,type:it[o.type]??"DYNAMIC",position:s(t.position),rotation:t.rotation,velocity:s(t.velocity),angularVel:t.angularVel,kinematicVel:s(t.kinematicVel),kinAngVel:t.kinAngVel,surfaceVel:s(t.surfaceVel),force:s(t.force),torque:o.type===2?t.torque:0,massMode:n,mass:n==="FIXED"?o.cmass:null,inertiaMode:a,inertia:a==="FIXED"?o.cinertia:null,gravMassMode:u,gravMassScale:o.gravMassScale,allowMovement:t.allowMovement,allowRotation:t.allowRotation,bullet:t.isBullet,shapes:p,userData:$(o.userData)}}function M(t,e,o,n){let a=t.zpp_inner,u=o!=null?e.get(o.zpp_inner.id)??null:null,p=n!=null?e.get(n.zpp_inner.id)??null:null;return{body1Id:u,body2Id:p,active:a.active,ignore:a.ignore,stiff:a.stiff,frequency:a.frequency,damping:a.damping,maxForce:a.maxForce,maxError:a.maxError,breakUnderForce:a.breakUnderForce,breakUnderError:a.breakUnderError,removeOnBreak:a.removeOnBreak,userData:$(a.userData)}}function pt(t,e){switch(t.constructor?.name??""){case"PivotJoint":{let n=t;return{...M(t,e,n.body1,n.body2),type:"PivotJoint",anchor1:s(n.anchor1),anchor2:s(n.anchor2)}}case"DistanceJoint":{let n=t;return{...M(t,e,n.body1,n.body2),type:"DistanceJoint",anchor1:s(n.anchor1),anchor2:s(n.anchor2),jointMin:n.jointMin,jointMax:n.jointMax}}case"AngleJoint":{let n=t;return{...M(t,e,n.body1,n.body2),type:"AngleJoint",jointMin:n.jointMin,jointMax:n.jointMax,ratio:n.ratio}}case"MotorJoint":{let n=t;return{...M(t,e,n.body1,n.body2),type:"MotorJoint",rate:n.rate,ratio:n.ratio}}case"LineJoint":{let n=t;return{...M(t,e,n.body1,n.body2),type:"LineJoint",anchor1:s(n.anchor1),anchor2:s(n.anchor2),direction:s(n.direction),jointMin:n.jointMin,jointMax:n.jointMax}}case"PulleyJoint":{let n=t;return{...M(t,e,n.body1,n.body2),type:"PulleyJoint",anchor1:s(n.anchor1),anchor2:s(n.anchor2),anchor3:s(n.anchor3),anchor4:s(n.anchor4),jointMin:n.jointMin,jointMax:n.jointMax,ratio:n.ratio}}case"WeldJoint":{let n=t;return{...M(t,e,n.body1,n.body2),type:"WeldJoint",anchor1:s(n.anchor1),anchor2:s(n.anchor2),phase:n.phase}}default:return null}}function mt(t){let e=[],o=new Map,n=new Map;function a(i){let y=i.zpp_inner.id;if(n.has(y))return;let D=e.length;o.set(i,D),n.set(y,D),e.push(ut(i,D))}let u=t.bodies,p=u.length;for(let i=0;i<p;i++)a(u.at(i));let r=t.compounds,m=r.length;for(let i=0;i<m;i++){let D=r.at(i).bodies,C=D.length;for(let f=0;f<C;f++)a(D.at(f))}let c=[],d=new Map;function I(i){if(d.has(i))return;let y=pt(i,n);y!=null&&(d.set(i,c.length),c.push(y))}let x=t.constraints,Q=x.length;for(let i=0;i<Q;i++)I(x.at(i));for(let i=0;i<m;i++){let D=r.at(i).constraints,C=D.length;for(let f=0;f<C;f++)I(D.at(f))}let V=[];for(let i=0;i<m;i++){let y=r.at(i),D=[],C=y.bodies,f=C.length;for(let b=0;b<f;b++){let F=C.at(b),E=o.get(F);E!=null&&D.push(E)}let B=[],w=y.constraints,nt=w.length;for(let b=0;b<nt;b++){let F=w.at(b),E=d.get(F);E!=null&&B.push(E)}V.push({bodyIds:D,constraintIndices:B,childIndices:[]})}let Z=t.zpp_inner.bphase.is_sweep?"SWEEP_AND_PRUNE":"DYNAMIC_AABB_TREE",tt=t.gravity;return{version:1,gravity:s(tt),worldLinearDrag:t.worldLinearDrag,worldAngularDrag:t.worldAngularDrag,sortContacts:t.sortContacts,broadphase:Z,bodies:e,constraints:c,compounds:V}}function h(t){return v.get(t.x,t.y)}function l(t){return v.weak(t.x,t.y)}function Dt(t){return new L(t.elasticity,t.dynamicFriction,t.staticFriction,t.density,t.rollingFriction)}function yt(t){let e=new G;return e.collisionGroup=t.collisionGroup,e.collisionMask=t.collisionMask,e.sensorGroup=t.sensorGroup,e.sensorMask=t.sensorMask,e.fluidGroup=t.fluidGroup,e.fluidMask=t.fluidMask,e}function ft(t){let e=new k(t.density,t.viscosity);return t.gravity!=null&&(e.gravity=h(t.gravity)),e}function dt(t){let e=Dt(t.material),o=yt(t.filter),n;if(t.type==="circle"){let a=h(t.localCOM);n=new U(t.radius,a,e,o)}else{let a=t.localVerts.map(u=>h(u));n=new R(a,e,o)}return n.sensorEnabled=t.sensorEnabled,n.fluidEnabled=t.fluidEnabled,t.fluidEnabled&&t.fluidProperties!=null&&(n.fluidProperties=ft(t.fluidProperties)),n}function bt(t){let e=t.type==="STATIC"?S.STATIC:t.type==="KINEMATIC"?S.KINEMATIC:S.DYNAMIC,o=new T(e,l(t.position));o.rotation=t.rotation,t.type!=="STATIC"&&(o.velocity=h(t.velocity),o.angularVel=t.angularVel),o.kinematicVel=h(t.kinematicVel),o.kinAngVel=t.kinAngVel,o.surfaceVel=h(t.surfaceVel),t.type==="DYNAMIC"&&(o.force=h(t.force),o.torque=t.torque),t.massMode==="FIXED"&&t.mass!=null?o.mass=t.mass:t.massMode==="DEFAULT"&&(o.massMode=_.DEFAULT),t.inertiaMode==="FIXED"&&t.inertia!=null?o.inertia=t.inertia:t.inertiaMode==="DEFAULT"&&(o.inertiaMode=O.DEFAULT),t.gravMassMode==="SCALED"?(o.gravMassMode=A.SCALED,o.gravMassScale=t.gravMassScale):t.gravMassMode==="FIXED"&&(o.gravMassMode=A.FIXED),o.allowMovement=t.allowMovement,o.allowRotation=t.allowRotation,o.isBullet=t.bullet;for(let n of t.shapes)dt(n).body=o;return t.userData!=null&&Object.assign(o.userData,t.userData),o}function g(t,e){t.active=e.active,t.ignore=e.ignore,t.stiff=e.stiff,t.frequency=e.frequency,t.damping=e.damping,t.maxForce=e.maxForce,t.maxError=e.maxError,t.breakUnderForce=e.breakUnderForce,t.breakUnderError=e.breakUnderError,t.removeOnBreak=e.removeOnBreak,e.userData!=null&&Object.assign(t.userData,e.userData)}function Mt(t,e){let o=t.body1Id!=null?e[t.body1Id]??null:null,n=t.body2Id!=null?e[t.body2Id]??null:null;switch(t.type){case"PivotJoint":{let a=new Y(o,n,l(t.anchor1),l(t.anchor2));return g(a,t),a}case"DistanceJoint":{let a=new X(o,n,l(t.anchor1),l(t.anchor2),t.jointMin,t.jointMax);return g(a,t),a}case"AngleJoint":{let a=new W(o,n,t.jointMin,t.jointMax,t.ratio);return g(a,t),a}case"MotorJoint":{let a=new z(o,n,t.rate,t.ratio);return g(a,t),a}case"LineJoint":{let a=new q(o,n,l(t.anchor1),l(t.anchor2),l(t.direction),t.jointMin,t.jointMax);return g(a,t),a}case"PulleyJoint":{let a=new H(o,n,null,null,l(t.anchor1),l(t.anchor2),l(t.anchor3),l(t.anchor4),t.jointMin,t.jointMax,t.ratio);return g(a,t),a}case"WeldJoint":{let a=new K(o,n,l(t.anchor1),l(t.anchor2),t.phase);return g(a,t),a}}}function gt(t){if(t.version!==1)throw new Error(`nape-js serialization: unsupported snapshot version ${t.version} (expected ${1})`);let e=t.broadphase==="SWEEP_AND_PRUNE"?P.SWEEP_AND_PRUNE:P.DYNAMIC_AABB_TREE,o=new j(l(t.gravity),e);o.worldLinearDrag=t.worldLinearDrag,o.worldAngularDrag=t.worldAngularDrag,o.sortContacts=t.sortContacts;let n=t.bodies.map(bt),a=t.constraints.map(r=>Mt(r,n)),u=new Set,p=new Set;for(let r of t.compounds){let m=new N;for(let c of r.bodyIds)n[c].compound=m,u.add(c);for(let c of r.constraintIndices)a[c].compound=m,p.add(c);m.space=o}for(let r=0;r<n.length;r++)u.has(r)||(n[r].space=o);for(let r=0;r<a.length;r++)p.has(r)||(a[r].space=o);return o}export{J as SNAPSHOT_VERSION,gt as spaceFromJSON,mt as spaceToJSON};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@newkrok/nape-js",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.0",
|
|
4
4
|
"description": "Nape Physics Engine - Modern TypeScript wrapper for 2D physics simulation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": [
|
|
@@ -34,6 +34,16 @@
|
|
|
34
34
|
"types": "./dist/index.d.cts",
|
|
35
35
|
"default": "./dist/index.cjs"
|
|
36
36
|
}
|
|
37
|
+
},
|
|
38
|
+
"./serialization": {
|
|
39
|
+
"import": {
|
|
40
|
+
"types": "./dist/serialization/index.d.ts",
|
|
41
|
+
"default": "./dist/serialization/index.js"
|
|
42
|
+
},
|
|
43
|
+
"require": {
|
|
44
|
+
"types": "./dist/serialization/index.d.cts",
|
|
45
|
+
"default": "./dist/serialization/index.cjs"
|
|
46
|
+
}
|
|
37
47
|
}
|
|
38
48
|
},
|
|
39
49
|
"llms": "https://newkrok.github.io/nape-js/llms.txt",
|