@needle-tools/engine 3.3.0-alpha → 3.5.0-alpha

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.
Files changed (149) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/needle-engine.js +62620 -61118
  3. package/dist/needle-engine.min.js +434 -410
  4. package/dist/needle-engine.umd.cjs +435 -411
  5. package/lib/engine/api.d.ts +1 -0
  6. package/lib/engine/api.js +1 -0
  7. package/lib/engine/api.js.map +1 -1
  8. package/lib/engine/codegen/register_types.js +50 -2
  9. package/lib/engine/codegen/register_types.js.map +1 -1
  10. package/lib/engine/engine_context.d.ts +1 -1
  11. package/lib/engine/engine_context.js +21 -15
  12. package/lib/engine/engine_context.js.map +1 -1
  13. package/lib/engine/engine_context_registry.d.ts +5 -3
  14. package/lib/engine/engine_context_registry.js +10 -2
  15. package/lib/engine/engine_context_registry.js.map +1 -1
  16. package/lib/engine/engine_element.js.map +1 -1
  17. package/lib/engine/engine_element_loading.js +2 -3
  18. package/lib/engine/engine_element_loading.js.map +1 -1
  19. package/lib/engine/engine_gameobject.d.ts +1 -1
  20. package/lib/engine/engine_gameobject.js +4 -2
  21. package/lib/engine/engine_gameobject.js.map +1 -1
  22. package/lib/engine/engine_input.d.ts +2 -2
  23. package/lib/engine/engine_physics.d.ts +20 -93
  24. package/lib/engine/engine_physics.js +20 -892
  25. package/lib/engine/engine_physics.js.map +1 -1
  26. package/lib/engine/engine_physics.types.js.map +1 -1
  27. package/lib/engine/engine_physics_rapier.d.ts +103 -0
  28. package/lib/engine/engine_physics_rapier.js +1003 -0
  29. package/lib/engine/engine_physics_rapier.js.map +1 -0
  30. package/lib/engine/engine_three_utils.js +2 -2
  31. package/lib/engine/engine_three_utils.js.map +1 -1
  32. package/lib/engine/engine_types.d.ts +50 -1
  33. package/lib/engine/engine_types.js +8 -0
  34. package/lib/engine/engine_types.js.map +1 -1
  35. package/lib/engine-components/Animation.js +4 -0
  36. package/lib/engine-components/Animation.js.map +1 -1
  37. package/lib/engine-components/Collider.js +6 -6
  38. package/lib/engine-components/Collider.js.map +1 -1
  39. package/lib/engine-components/Joints.js +2 -2
  40. package/lib/engine-components/Joints.js.map +1 -1
  41. package/lib/engine-components/RigidBody.d.ts +0 -1
  42. package/lib/engine-components/RigidBody.js +24 -30
  43. package/lib/engine-components/RigidBody.js.map +1 -1
  44. package/lib/engine-components/codegen/components.d.ts +25 -1
  45. package/lib/engine-components/codegen/components.js +25 -1
  46. package/lib/engine-components/codegen/components.js.map +1 -1
  47. package/lib/engine-components/export/usdz/Extension.d.ts +4 -4
  48. package/lib/engine-components/export/usdz/ThreeUSDZExporter.d.ts +86 -0
  49. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +858 -0
  50. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js.map +1 -0
  51. package/lib/engine-components/export/usdz/USDZExporter.d.ts +6 -3
  52. package/lib/engine-components/export/usdz/USDZExporter.js +34 -11
  53. package/lib/engine-components/export/usdz/USDZExporter.js.map +1 -1
  54. package/lib/engine-components/export/usdz/extensions/Animation.d.ts +15 -15
  55. package/lib/engine-components/export/usdz/extensions/Animation.js +24 -29
  56. package/lib/engine-components/export/usdz/extensions/Animation.js.map +1 -1
  57. package/lib/engine-components/export/usdz/extensions/DocumentExtension.d.ts +5 -0
  58. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js +7 -0
  59. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js.map +1 -0
  60. package/lib/engine-components/export/usdz/extensions/USDZText.d.ts +47 -0
  61. package/lib/engine-components/export/usdz/extensions/USDZText.js +114 -0
  62. package/lib/engine-components/export/usdz/extensions/USDZText.js.map +1 -0
  63. package/lib/engine-components/export/usdz/extensions/behavior/Actions.d.ts +30 -0
  64. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js +89 -0
  65. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js.map +1 -0
  66. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.d.ts +23 -0
  67. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js +114 -0
  68. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js.map +1 -0
  69. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.d.ts +102 -0
  70. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js +458 -0
  71. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js.map +1 -0
  72. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.d.ts +111 -0
  73. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js +409 -0
  74. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js.map +1 -0
  75. package/lib/engine-components/postprocessing/PostProcessingHandler.js.map +1 -1
  76. package/lib/engine-components/ui/BaseUIComponent.d.ts +2 -0
  77. package/lib/engine-components/ui/BaseUIComponent.js +6 -0
  78. package/lib/engine-components/ui/BaseUIComponent.js.map +1 -1
  79. package/lib/engine-components/ui/Canvas.d.ts +11 -1
  80. package/lib/engine-components/ui/Canvas.js +72 -3
  81. package/lib/engine-components/ui/Canvas.js.map +1 -1
  82. package/lib/engine-components/ui/Graphic.js.map +1 -1
  83. package/lib/engine-components/ui/Image.js +4 -4
  84. package/lib/engine-components/ui/Image.js.map +1 -1
  85. package/lib/engine-components/ui/Interfaces.d.ts +11 -0
  86. package/lib/engine-components/ui/Interfaces.js +11 -0
  87. package/lib/engine-components/ui/Interfaces.js.map +1 -1
  88. package/lib/engine-components/ui/Layout.d.ts +65 -3
  89. package/lib/engine-components/ui/Layout.js +304 -3
  90. package/lib/engine-components/ui/Layout.js.map +1 -1
  91. package/lib/engine-components/ui/RectTransform.d.ts +8 -7
  92. package/lib/engine-components/ui/RectTransform.js +66 -36
  93. package/lib/engine-components/ui/RectTransform.js.map +1 -1
  94. package/lib/engine-components/utils/LookAt.d.ts +7 -1
  95. package/lib/engine-components/utils/LookAt.js +43 -6
  96. package/lib/engine-components/utils/LookAt.js.map +1 -1
  97. package/lib/engine-components/webxr/WebXRImageTracking.d.ts +4 -3
  98. package/lib/engine-components/webxr/WebXRImageTracking.js +81 -25
  99. package/lib/engine-components/webxr/WebXRImageTracking.js.map +1 -1
  100. package/lib/tsconfig.tsbuildinfo +1 -1
  101. package/package.json +1 -1
  102. package/plugins/vite/config.js +2 -1
  103. package/plugins/vite/defines.js +30 -0
  104. package/plugins/vite/dependency-watcher.js +173 -0
  105. package/plugins/vite/editor-connection.js +37 -39
  106. package/plugins/vite/index.js +5 -1
  107. package/plugins/vite/reload.js +16 -3
  108. package/src/engine/api.ts +1 -0
  109. package/src/engine/codegen/register_types.js +50 -2
  110. package/src/engine/engine_context.ts +32 -23
  111. package/src/engine/engine_context_registry.ts +13 -6
  112. package/src/engine/engine_element.ts +2 -1
  113. package/src/engine/engine_element_loading.ts +2 -3
  114. package/src/engine/engine_gameobject.ts +3 -2
  115. package/src/engine/engine_input.ts +2 -2
  116. package/src/engine/engine_physics.ts +25 -1020
  117. package/src/engine/engine_physics.types.ts +1 -3
  118. package/src/engine/engine_physics_rapier.ts +1127 -0
  119. package/src/engine/engine_three_utils.ts +2 -2
  120. package/src/engine/engine_types.ts +66 -4
  121. package/src/engine-components/Animation.ts +4 -0
  122. package/src/engine-components/Collider.ts +6 -6
  123. package/src/engine-components/Joints.ts +2 -2
  124. package/src/engine-components/RigidBody.ts +24 -31
  125. package/src/engine-components/codegen/components.ts +25 -1
  126. package/src/engine-components/export/usdz/Extension.ts +4 -5
  127. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +1312 -0
  128. package/src/engine-components/export/usdz/USDZExporter.ts +39 -17
  129. package/src/engine-components/export/usdz/extensions/Animation.ts +37 -45
  130. package/src/engine-components/export/usdz/extensions/DocumentExtension.ts +10 -0
  131. package/src/engine-components/export/usdz/extensions/USDZText.ts +142 -0
  132. package/src/engine-components/export/usdz/extensions/behavior/Actions.ts +99 -0
  133. package/src/engine-components/export/usdz/extensions/behavior/Behaviour.ts +181 -0
  134. package/src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +545 -0
  135. package/src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts +459 -0
  136. package/src/engine-components/postprocessing/PostProcessingHandler.ts +1 -1
  137. package/src/engine-components/ui/BaseUIComponent.ts +7 -1
  138. package/src/engine-components/ui/Canvas.ts +80 -5
  139. package/src/engine-components/ui/Graphic.ts +2 -0
  140. package/src/engine-components/ui/Image.ts +3 -3
  141. package/src/engine-components/ui/Interfaces.ts +30 -6
  142. package/src/engine-components/ui/Layout.ts +303 -4
  143. package/src/engine-components/ui/RectTransform.ts +67 -41
  144. package/src/engine-components/utils/LookAt.ts +60 -7
  145. package/src/engine-components/webxr/WebXRImageTracking.ts +100 -27
  146. package/lib/engine-components/export/usdz/types.d.ts +0 -34
  147. package/lib/engine-components/export/usdz/types.js +0 -2
  148. package/lib/engine-components/export/usdz/types.js.map +0 -1
  149. package/src/engine-components/export/usdz/types.ts +0 -39
@@ -0,0 +1,1127 @@
1
+ import { BufferAttribute, BufferGeometry, LineBasicMaterial, LineSegments, Matrix4, Mesh, Object3D, Quaternion, Vector3 } from 'three'
2
+ import { CircularBuffer, getParam } from "./engine_utils"
3
+ import { getWorldPosition, getWorldQuaternion, getWorldScale, setWorldPositionXYZ, setWorldQuaternionXYZW } from "./engine_three_utils"
4
+ import {
5
+ IPhysicsEngine,
6
+ IComponent,
7
+ ICollider,
8
+ IRigidbody,
9
+ Collision,
10
+ ContactPoint,
11
+ Vec3,
12
+ IGameObject,
13
+ Vec2,
14
+ IContext,
15
+ } from './engine_types';
16
+ import { foreachComponent } from './engine_gameobject';
17
+
18
+ import { ActiveCollisionTypes, ActiveEvents, CoefficientCombineRule, Ball, Collider, ColliderDesc, EventQueue, JointData, QueryFilterFlags, RigidBody, RigidBodyType, ShapeColliderTOI, World, Ray } from '@dimforge/rapier3d-compat';
19
+ import { CollisionDetectionMode, PhysicsMaterialCombine } from '../engine/engine_physics.types';
20
+ import { Gizmos } from './engine_gizmos';
21
+ import { Mathf } from './engine_math';
22
+ import { SphereOverlapResult } from './engine_types';
23
+ import { ContextEvent, ContextRegistry } from './engine_context_registry';
24
+
25
+ const debugPhysics = getParam("debugphysics");
26
+ const debugColliderPlacement = getParam("debugphysicscolliders");
27
+ const debugCollisions = getParam("debugcollisions");
28
+ const showColliders = getParam("showcolliders");
29
+
30
+
31
+ /** on physics body and references the needle component */
32
+ const $componentKey = Symbol("needle component");
33
+ /** on needle component and references physics body */
34
+ const $bodyKey = Symbol("physics body");
35
+ const $colliderRigidbody = Symbol("rigidbody");
36
+
37
+
38
+ let RAPIER: undefined | any = undefined;
39
+ declare const NEEDLE_USE_RAPIER: boolean;
40
+
41
+
42
+ if (NEEDLE_USE_RAPIER) {
43
+ ContextRegistry.registerCallback(ContextEvent.ContextCreationStart, evt => {
44
+ if (debugPhysics)
45
+ console.log("Register rapier physics backend")
46
+ evt.context.physics.engine = new RapierPhysics();
47
+ // We want the physics engine to be initialized on start so when components start to enable and modify values they don't have delays
48
+ // TODO: should the promise be returned here to make the engine creation wait?
49
+ if (NEEDLE_USE_RAPIER) {
50
+ evt.context.physics.engine.initialize(evt.context);
51
+ }
52
+ });
53
+ }
54
+
55
+
56
+ declare type PhysicsBody = {
57
+ translation(): { x: number, y: number, z: number }
58
+ rotation(): { x: number, y: number, z: number, w: number }
59
+ }
60
+
61
+ export class RapierPhysics implements IPhysicsEngine {
62
+
63
+ removeBody(obj: IComponent) {
64
+ this.validate();
65
+ const body = obj[$bodyKey];
66
+ obj[$bodyKey] = null;
67
+ if (body && this.world) {
68
+ const index = this.objects.findIndex(o => o === obj);
69
+ if (index >= 0) {
70
+ const body = this.bodies[index];
71
+ this.bodies.splice(index, 1);
72
+ this.objects.splice(index, 1);
73
+
74
+ if (body instanceof Collider) {
75
+ const collider = body as Collider;
76
+ this.world?.removeCollider(collider, true);
77
+
78
+ // remove the rigidbody if it doesnt have colliders anymore
79
+ const rb = collider.parent();
80
+ if (rb && rb.numColliders() <= 0) {
81
+ this.world?.removeRigidBody(rb);
82
+ }
83
+ }
84
+ else if (body instanceof RigidBody) {
85
+ // TODO: running this code below causes a crash in rapier
86
+ // const rb = body as RigidBody;
87
+ // console.log("colliders", rb.numColliders())
88
+ // for (let i = 0; i < rb.numColliders(); i++) {
89
+ // const col = rb.collider(i);
90
+ // this.world?.removeCollider(col, true);
91
+ // }
92
+ // console.log("colliders", rb.numColliders(), rb)
93
+ // console.log(rb.handle, rb.userData);
94
+ // if (rb.userData === undefined)
95
+ // this.world?.removeRigidBody(rb);
96
+ }
97
+
98
+ // check if we need to remove the rigidbody too
99
+ // const col = obj as ICollider;
100
+ // if (col.isCollider && col.attachedRigidbody) {
101
+ // const rb = col.attachedRigidbody[$bodyKey] as RigidBody;
102
+ // if (rb && rb.numColliders() <= 0) {
103
+ // // this.world?.removeRigidBody(rb);
104
+ // }
105
+ // }
106
+ }
107
+ }
108
+ }
109
+
110
+ updateBody(comp: ICollider | IRigidbody, translation: boolean, rotation: boolean) {
111
+ this.validate();
112
+ if (!this.enabled) return;
113
+ if (comp.destroyed || !comp.gameObject) return;
114
+ if (!translation && !rotation) return;
115
+
116
+ if ((comp as ICollider).isCollider === true) {
117
+ // const collider = comp as ICollider;
118
+ console.warn("TODO: implement updating collider position");
119
+ }
120
+ else {
121
+ const rigidbody = comp as IRigidbody;
122
+ const body = rigidbody[$bodyKey];
123
+ if (body) {
124
+ this.syncPhysicsBody(rigidbody.gameObject, body, translation, rotation);
125
+ }
126
+ }
127
+ }
128
+
129
+ updateProperties(rigidbody: IRigidbody) {
130
+ this.validate();
131
+ const physicsBody = rigidbody[$bodyKey];
132
+ if (physicsBody) {
133
+ this.internalUpdateProperties(rigidbody, physicsBody);
134
+ }
135
+ }
136
+ addForce(rigidbody: IRigidbody, force: Vec3, wakeup: boolean) {
137
+ this.validate();
138
+ const body = this.internal_getRigidbody(rigidbody);
139
+ body?.addForce(force, wakeup)
140
+ }
141
+ addImpulse(rigidbody: IRigidbody, force: Vec3, wakeup: boolean) {
142
+ this.validate();
143
+ const body = this.internal_getRigidbody(rigidbody);
144
+ body?.applyImpulse(force, wakeup)
145
+ }
146
+ getLinearVelocity(rigidbody: IRigidbody): Vec3 | null {
147
+ this.validate();
148
+ const body = this.internal_getRigidbody(rigidbody);
149
+ if (body) {
150
+ const vel = body.linvel();
151
+ return vel;
152
+ }
153
+ return null;
154
+ }
155
+ getAngularVelocity(rb: IRigidbody): Vec3 | null {
156
+ this.validate();
157
+ const body = this.internal_getRigidbody(rb);
158
+ if (body) {
159
+ const vel = body.angvel();
160
+ return vel;
161
+ }
162
+ return null;
163
+ }
164
+ resetForces(rb: IRigidbody, wakeup: boolean) {
165
+ this.validate();
166
+ const body = this.internal_getRigidbody(rb);
167
+ body?.resetForces(wakeup);
168
+ }
169
+ resetTorques(rb: IRigidbody, wakeup: boolean) {
170
+ this.validate();
171
+ const body = this.internal_getRigidbody(rb);
172
+ body?.resetTorques(wakeup);
173
+ }
174
+ applyImpulse(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
175
+ this.validate();
176
+ const body = this.internal_getRigidbody(rb);
177
+ body?.applyImpulse(vec, wakeup);
178
+ }
179
+
180
+ wakeup(rb: IRigidbody) {
181
+ this.validate();
182
+ const body = this.internal_getRigidbody(rb);
183
+ body?.wakeUp();
184
+ }
185
+ setAngularVelocity(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
186
+ this.validate();
187
+ const body = this.internal_getRigidbody(rb);
188
+ body?.setAngvel(vec, wakeup);
189
+ }
190
+ setLinearVelocity(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
191
+ this.validate();
192
+ const body = this.internal_getRigidbody(rb);
193
+ body?.setLinvel(vec, wakeup);
194
+ }
195
+
196
+ private context?: IContext;
197
+ private _initializePromise?: Promise<boolean>;
198
+ private _isInitialized: boolean = false;
199
+
200
+ async initialize(context: IContext) {
201
+ this.context = context;
202
+ if (!this._initializePromise)
203
+ this._initializePromise = this.internalInitialization();
204
+ return this._initializePromise;
205
+ }
206
+
207
+ private async internalInitialization() {
208
+ // NEEDLE_PHYSICS_INIT_START
209
+ // use .env file with VITE_NEEDLE_USE_RAPIER=false to treeshape rapier
210
+ if (import.meta.env.VITE_NEEDLE_USE_RAPIER === "false") {
211
+ return false;
212
+ }
213
+ // Can be transformed during build time to disable rapier
214
+ if (!NEEDLE_USE_RAPIER) return false;
215
+ if (this._hasCreatedWorld) {
216
+ console.error("Invalid call to create physics world: world is already created");
217
+ return true;
218
+ }
219
+ this._hasCreatedWorld = true;
220
+ if (RAPIER === undefined) {
221
+ RAPIER = await import("@dimforge/rapier3d-compat");
222
+ await RAPIER.init()
223
+ }
224
+ if (debugPhysics) console.log("Physics engine initialized, creating world...");
225
+ this.world = new World(this._gravity);
226
+ this.enabled = true;
227
+ this._isInitialized = true;
228
+ if (debugPhysics) console.log("Physics world created");
229
+ return true;
230
+ // NEEDLE_PHYSICS_INIT_END
231
+ }
232
+
233
+ /** Check is the physics engine has been initialized and the call can be made */
234
+ private validate() {
235
+ if (!this._isInitialized) {
236
+ if (debugPhysics)
237
+ console.warn("Physics engine is not initialized");
238
+ }
239
+ }
240
+
241
+
242
+ private rapierRay = new Ray({ x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 1 });
243
+ private raycastVectorsBuffer = new CircularBuffer(() => new Vector3(), 10);
244
+ /** Fast raycast against physics colliders
245
+ * @param origin ray origin in screen or worldspace
246
+ * @param direction ray direction in worldspace
247
+ * @param maxDistance max distance to raycast
248
+ * @param solid if true it will also hit the collider if origin is already inside it
249
+ */
250
+ public raycast(origin: Vec2 | Vec3, direction: Vec3 | undefined = undefined, maxDistance: number = Infinity, solid: boolean = true)
251
+ : null | { point: Vector3, collider: ICollider } {
252
+
253
+ const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
254
+ if (!ray) return null;
255
+
256
+ const hit = this.world?.castRay(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
257
+ // ignore objects in the IgnoreRaycast=2 layer
258
+ return !c[$componentKey]?.gameObject.layers.isEnabled(2);
259
+ });
260
+ if (hit) {
261
+ const point = ray.pointAt(hit.toi);
262
+ const vec = this.raycastVectorsBuffer.get();
263
+ vec.set(point.x, point.y, point.z);
264
+ return { point: vec, collider: hit.collider[$componentKey] };
265
+ }
266
+
267
+ return null;
268
+ }
269
+
270
+ public raycastAndGetNormal(origin: Vec2 | Vec3, direction: Vec3 | undefined = undefined, maxDistance: number = Infinity, solid: boolean = true)
271
+ : null | { point: Vector3, normal: Vector3, collider: ICollider } {
272
+
273
+ const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
274
+ if (!ray) return null;
275
+
276
+ const hit = this.world?.castRayAndGetNormal(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
277
+ // ignore objects in the IgnoreRaycast=2 layer
278
+ return !c[$componentKey]?.gameObject.layers.isEnabled(2);
279
+ });
280
+ if (hit) {
281
+ const point = ray.pointAt(hit.toi);
282
+ const normal = hit.normal;
283
+ const vec = this.raycastVectorsBuffer.get();
284
+ const nor = this.raycastVectorsBuffer.get();
285
+ vec.set(point.x, point.y, point.z);
286
+ nor.set(normal.x, normal.y, normal.z);
287
+ return { point: vec, normal: nor, collider: hit.collider[$componentKey] };
288
+ }
289
+ return null;
290
+ }
291
+
292
+ private getPhysicsRay(ray: Ray, origin: Vec2 | Vec3, direction: Vec3 | undefined = undefined): Ray | null {
293
+ const cam = this.context?.mainCamera;
294
+ // if we get origin in 2d space we need to project it to 3d space
295
+ if (origin["z"] === undefined) {
296
+ if (!cam) {
297
+ console.error("Can not perform raycast from 2d point - no main camera found");
298
+ return null;
299
+ }
300
+ const vec3 = this.raycastVectorsBuffer.get();
301
+ vec3.x = origin.x;
302
+ vec3.y = origin.y;
303
+ vec3.z = 0;
304
+ // if the origin is in screen space we need to convert it to raycaster space
305
+ if (vec3.x > 1 || vec3.y > 1 || vec3.y < -1 || vec3.x < -1) {
306
+ this.context?.input.convertScreenspaceToRaycastSpace(vec3);
307
+ }
308
+ vec3.unproject(cam);
309
+ origin = vec3;
310
+ }
311
+
312
+ const o = origin as Vec3;
313
+
314
+ ray.origin.x = o.x;
315
+ ray.origin.y = o.y;
316
+ ray.origin.z = o.z;
317
+ const vec = this.raycastVectorsBuffer.get();
318
+ if (direction)
319
+ vec.set(direction.x, direction.y, direction.z);
320
+ else {
321
+ if (!cam) {
322
+ console.error("Can not perform raycast - no camera found");
323
+ return null;
324
+ }
325
+ vec.set(ray.origin.x, ray.origin.y, ray.origin.z);
326
+ const camPosition = getWorldPosition(cam);
327
+ vec.sub(camPosition);
328
+ }
329
+ // we need to normalize the ray because our input is a max travel length and the direction may be not normalized
330
+ vec.normalize();
331
+ ray.dir.x = vec.x;
332
+ ray.dir.y = vec.y;
333
+ ray.dir.z = vec.z;
334
+ // Gizmos.DrawRay(ray.origin, ray.dir, 0xff0000, Infinity);
335
+ return ray;
336
+ }
337
+
338
+
339
+ private rapierSphere: Ball | null = null;
340
+ private rapierColliderArray: Array<SphereOverlapResult> = [];
341
+ private readonly rapierIdentityRotation = { x: 0, y: 0, z: 0, w: 1 };
342
+ private readonly rapierForwardVector = { x: 0, y: 0, z: 1 };
343
+ /** Precice sphere overlap detection using rapier against colliders
344
+ * @param point center of the sphere in worldspace
345
+ * @param radius radius of the sphere
346
+ * @returns array of colliders that overlap with the sphere. Note: they currently only contain the collider and the gameobject
347
+ */
348
+ public sphereOverlap(point: Vector3, radius: number): Array<SphereOverlapResult> {
349
+ this.rapierColliderArray.length = 0;
350
+ if (!this.world) return this.rapierColliderArray;
351
+ if (!this.rapierSphere)
352
+ this.rapierSphere = new Ball(radius);
353
+ this.rapierSphere.radius = radius;
354
+
355
+ this.world.intersectionsWithShape(point, this.rapierIdentityRotation, this.rapierSphere, col => {
356
+ const collider = col[$componentKey] as ICollider
357
+ // if (collider.gameObject.layers.isEnabled(2)) return true;
358
+ const intersection = new SphereOverlapResult(collider.gameObject, collider);
359
+ this.rapierColliderArray.push(intersection);
360
+ return true; // Return `false` instead if we want to stop searching for other colliders that contain this point.
361
+ }, QueryFilterFlags.EXCLUDE_SENSORS, undefined, undefined, undefined,
362
+ col => {
363
+ const collider = col[$componentKey] as ICollider
364
+ return collider.gameObject.layers.isEnabled(2) == false
365
+ }
366
+ );
367
+ return this.rapierColliderArray;
368
+
369
+
370
+ // TODO: this only returns one hit
371
+ // let filterGroups = 0xffffffff;
372
+ // filterGroups &= ~(1 << 2);
373
+ // const hit: ShapeColliderTOI | null = this.world.castShape(point,
374
+ // this.rapierIdentityRotation,
375
+ // this.rapierForwardVector,
376
+ // this.rapierSphere,
377
+ // 0,
378
+ // QueryFilterFlags.EXCLUDE_SENSORS,
379
+ // // filterGroups,
380
+ // );
381
+ // // console.log(hit);
382
+ // if (hit) {
383
+ // const collider = hit.collider[$componentKey] as ICollider
384
+ // const intersection = new SphereOverlapResult(collider.gameObject);
385
+ // this.rapierColliderArray.push(intersection);
386
+ // // const localpt = hit.witness2;
387
+ // // // const normal = hit.normal2;
388
+ // // const hitPoint = new Vector3(localpt.x, localpt.y, localpt.z);
389
+ // // // collider.gameObject.localToWorld(hitPoint);
390
+ // // // const normalPt = new Vector3(normal.x, normal.y, normal.z);
391
+ // // // const mat = new Matrix4().setPosition(point).scale(new Vector3(radius, radius, radius));
392
+ // // // hitPoint.applyMatrix4(mat);
393
+ // // console.log(hit.witness2)
394
+ // // // hitPoint.add(point);
395
+ // // const dist = hitPoint.distanceTo(point);
396
+ // }
397
+
398
+ // return this.rapierColliderArray;
399
+ }
400
+
401
+
402
+
403
+
404
+ // physics simulation
405
+
406
+ enabled: boolean = false;
407
+
408
+ private _tempPosition: Vector3 = new Vector3();
409
+ private _tempQuaternion: Quaternion = new Quaternion();
410
+ private _tempScale: Vector3 = new Vector3();
411
+ private _tempMatrix: Matrix4 = new Matrix4();
412
+
413
+ private static _didLoadPhysicsEngine: boolean = false;
414
+
415
+ private _isUpdatingPhysicsWorld: boolean = false;
416
+ get isUpdating(): boolean { return this._isUpdatingPhysicsWorld; }
417
+
418
+
419
+ private world?: World;
420
+ private _hasCreatedWorld: boolean = false;
421
+ private eventQueue?: EventQueue;
422
+ private collisionHandler?: PhysicsCollisionHandler;
423
+
424
+
425
+ private objects: IComponent[] = [];
426
+ private bodies: PhysicsBody[] = [];
427
+
428
+ private _meshCache: Map<string, Float32Array> = new Map<string, Float32Array>();
429
+
430
+ private _gravity = { x: 0.0, y: -9.81, z: 0.0 };
431
+
432
+ get gravity() {
433
+ return this.world?.gravity ?? this._gravity;
434
+ }
435
+
436
+ set gravity(value: Vec3) {
437
+ if (this.world) {
438
+ this.world.gravity = value;
439
+ }
440
+ else {
441
+ this._gravity = value;
442
+ }
443
+ }
444
+
445
+ clearCaches() {
446
+ this._meshCache.clear();
447
+ }
448
+
449
+ async addBoxCollider(collider: ICollider, center: Vector3, size: Vector3) {
450
+ if (!this._isInitialized)
451
+ await this.initialize(collider.context);
452
+ if (!collider.activeAndEnabled) return;
453
+
454
+ if (!this.enabled) {
455
+ if (debugPhysics) console.warn("Physics are disabled");
456
+ return;
457
+ }
458
+ const obj = collider.gameObject;
459
+ const scale = getWorldScale(obj, this._tempPosition).multiply(size);
460
+ scale.multiplyScalar(0.5);
461
+
462
+ // prevent negative scale
463
+ if (scale.x < 0)
464
+ scale.x = Math.abs(scale.x);
465
+ if (scale.y < 0)
466
+ scale.y = Math.abs(scale.y);
467
+ if (scale.z < 0)
468
+ scale.z = Math.abs(scale.z);
469
+
470
+ // prevent zero scale - seems normals are flipped otherwise
471
+ if (scale.x == 0) scale.x = 0.0000001;
472
+ if (scale.y == 0) scale.y = 0.0000001;
473
+ if (scale.z == 0) scale.z = 0.0000001;
474
+
475
+ const desc = ColliderDesc.cuboid(scale.x, scale.y, scale.z);
476
+ // const objectLayerMask = collider.gameObject.layers.mask;
477
+ // const mask = objectLayerMask & ~2;
478
+ // TODO: https://rapier.rs/docs/user_guides/javascript/colliders/#collision-groups-and-solver-groups
479
+ // desc.setCollisionGroups(objectLayerMask);
480
+ this.createCollider(collider, desc, center);
481
+ }
482
+
483
+ async addSphereCollider(collider: ICollider, center: Vector3, radius: number) {
484
+ if (!this._isInitialized)
485
+ await this.initialize(collider.context);
486
+ if (!collider.activeAndEnabled) return;
487
+ if (!this.enabled) {
488
+ if (debugPhysics) console.warn("Physics are disabled");
489
+ return;
490
+ }
491
+ const obj = collider.gameObject;
492
+ const scale = getWorldScale(obj, this._tempPosition).multiplyScalar(radius);
493
+ // Prevent negative scales
494
+ scale.x = Math.abs(scale.x);
495
+ const desc = ColliderDesc.ball(scale.x);
496
+ this.createCollider(collider, desc, center);
497
+ }
498
+
499
+ async addCapsuleCollider(collider: ICollider, center: Vector3, height: number, radius: number) {
500
+ if (!this._isInitialized)
501
+ await this.initialize(collider.context);
502
+ if (!collider.activeAndEnabled) return;
503
+ if (!this.enabled) {
504
+ if (debugPhysics) console.warn("Physics are disabled");
505
+ return;
506
+ }
507
+ const obj = collider.gameObject;
508
+ const scale = getWorldScale(obj, this._tempPosition);
509
+ // Prevent negative scales
510
+ scale.x = Math.abs(scale.x);
511
+ scale.y = Math.abs(scale.y);
512
+ const desc = ColliderDesc.capsule(height * .5 * scale.y - radius, radius * scale.x);
513
+ this.createCollider(collider, desc, center);
514
+ }
515
+
516
+ async addMeshCollider(collider: ICollider, mesh: Mesh, convex: boolean, scale: Vector3) {
517
+ if (!this._isInitialized)
518
+ await this.initialize(collider.context);
519
+ if (!collider.activeAndEnabled) return;
520
+ if (!this.enabled) {
521
+ if (debugPhysics) console.warn("Physics are disabled");
522
+ return;
523
+ }
524
+ const geo = mesh.geometry;
525
+ if (!geo) {
526
+ if (debugPhysics) console.warn("Missing mesh geometry", mesh.name);
527
+ return;
528
+ }
529
+
530
+ let positions = geo.getAttribute("position").array as Float32Array;
531
+ const indices = geo.index?.array as Uint32Array;
532
+
533
+ // console.log(geo.center())
534
+
535
+ // scaling seems not supported yet https://github.com/dimforge/rapier/issues/243
536
+ if (Math.abs(scale.x - 1) > 0.0001 || Math.abs(scale.y - 1) > 0.0001 || Math.abs(scale.z - 1) > 0.0001) {
537
+ const key = geo.uuid + "_" + scale.x + "_" + scale.y + "_" + scale.z + "_" + convex;
538
+ if (this._meshCache.has(key)) {
539
+ positions = this._meshCache.get(key)!;
540
+ }
541
+ else {
542
+ console.warn("Your model is using scaled mesh colliders which is not optimal for performance", mesh.name, Object.assign({}, scale), mesh);
543
+ // showBalloonWarning("Your model is using scaled mesh colliders which is not optimal for performance: " + mesh.name + ", consider using unscaled objects");
544
+ const scaledPositions = new Float32Array(positions.length);
545
+ for (let i = 0; i < positions.length; i += 3) {
546
+ scaledPositions[i] = positions[i] * scale.x;
547
+ scaledPositions[i + 1] = positions[i + 1] * scale.y;
548
+ scaledPositions[i + 2] = positions[i + 2] * scale.z;
549
+ }
550
+ positions = scaledPositions;
551
+ this._meshCache.set(key, scaledPositions);
552
+ }
553
+ }
554
+ const desc = convex ? ColliderDesc.convexMesh(positions) : ColliderDesc.trimesh(positions, indices);
555
+ if (desc) {
556
+ const col = this.createCollider(collider, desc);
557
+ col.setMassProperties(1, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0, w: 1 });
558
+ // rb?.setTranslation({ x: 0, y: 2, z: 0 });
559
+ // col.setTranslationWrtParent(new Vector3(0,2,0));
560
+
561
+ }
562
+ }
563
+
564
+ private createCollider(collider: ICollider, desc: ColliderDesc, center?: Vector3) {
565
+ if (!this.world) throw new Error("Physics world not initialized");
566
+ const matrix = this._tempMatrix;
567
+ const {
568
+ rigidBody,
569
+ useExplicitMassProperties
570
+ } = this.getRigidbody(collider, this._tempMatrix);
571
+
572
+ matrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
573
+ getWorldScale(collider.gameObject, this._tempScale);
574
+ if (center) {
575
+ center.multiply(this._tempScale);
576
+ this._tempPosition.x -= center.x;
577
+ this._tempPosition.y += center.y;
578
+ this._tempPosition.z += center.z;
579
+ }
580
+ desc.setTranslation(this._tempPosition.x, this._tempPosition.y, this._tempPosition.z);
581
+ desc.setRotation(this._tempQuaternion);
582
+ desc.setSensor(collider.isTrigger);
583
+
584
+ // TODO: we might want to update this if the material changes
585
+ const physicsMaterial = collider.sharedMaterial;
586
+ if (physicsMaterial) {
587
+ desc.setRestitution(physicsMaterial.bounciness);
588
+ switch (physicsMaterial.bounceCombine) {
589
+ case PhysicsMaterialCombine.Average:
590
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Average);
591
+ break;
592
+ case PhysicsMaterialCombine.Maximum:
593
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Max);
594
+ break;
595
+ case PhysicsMaterialCombine.Minimum:
596
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Min);
597
+ break;
598
+ case PhysicsMaterialCombine.Multiply:
599
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Multiply);
600
+ break;
601
+ }
602
+ desc.setFriction(physicsMaterial.dynamicFriction);
603
+ switch (physicsMaterial.frictionCombine) {
604
+ case PhysicsMaterialCombine.Average:
605
+ desc.setFrictionCombineRule(CoefficientCombineRule.Average);
606
+ break;
607
+ case PhysicsMaterialCombine.Maximum:
608
+ desc.setFrictionCombineRule(CoefficientCombineRule.Max);
609
+ break;
610
+ case PhysicsMaterialCombine.Minimum:
611
+ desc.setFrictionCombineRule(CoefficientCombineRule.Min);
612
+ break;
613
+ case PhysicsMaterialCombine.Multiply:
614
+ desc.setFrictionCombineRule(CoefficientCombineRule.Multiply);
615
+ break;
616
+ }
617
+ }
618
+
619
+ // if we want to use explicit mass properties, we need to set the collider density to 0
620
+ // otherwise rapier will compute the mass properties based on the collider shape and density
621
+ // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
622
+ if (useExplicitMassProperties) {
623
+ // desc.setDensity(0);
624
+ }
625
+
626
+ const col = this.world.createCollider(desc, rigidBody);
627
+ col[$componentKey] = collider;
628
+ collider[$bodyKey] = col;
629
+ col.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
630
+ // We want to receive collisitons between two triggers too
631
+ col.setActiveCollisionTypes(ActiveCollisionTypes.ALL);
632
+
633
+ // const objectLayerMask = collider.gameObject.layers.mask;
634
+ // const mask = objectLayerMask & ~2;
635
+ // col.setCollisionGroups(objectLayerMask);
636
+ this.objects.push(collider);
637
+ this.bodies.push(col);
638
+ return col;
639
+ }
640
+
641
+ private getRigidbody(collider: ICollider, _matrix: Matrix4): { rigidBody: RigidBody, useExplicitMassProperties: boolean } {
642
+
643
+ if (!this.world) throw new Error("Physics world not initialized");
644
+ let rigidBody: RigidBody | null = null;
645
+ let useExplicitMassProperties = false;
646
+
647
+ if (collider.attachedRigidbody) {
648
+
649
+ const rb = collider.attachedRigidbody;
650
+ rigidBody = rb[$bodyKey];
651
+ useExplicitMassProperties = true;
652
+ if (!rigidBody) {
653
+ const kinematic = rb.isKinematic && !debugColliderPlacement;
654
+ if (debugPhysics)
655
+ console.log("Create rigidbody", kinematic);
656
+ const rigidBodyDesc = kinematic ? RAPIER.RigidBodyDesc.kinematicPositionBased() : RAPIER.RigidBodyDesc.dynamic();
657
+ const pos = getWorldPosition(collider.attachedRigidbody.gameObject);
658
+ rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
659
+ rigidBodyDesc.setRotation(getWorldQuaternion(collider.attachedRigidbody.gameObject));
660
+ rigidBody = this.world.createRigidBody(rigidBodyDesc);
661
+ this.bodies.push(rigidBody);
662
+ this.objects.push(rb);
663
+ }
664
+ rigidBody[$componentKey] = rb;
665
+ rb[$bodyKey] = rigidBody;
666
+ this.internalUpdateProperties(rb, rigidBody);
667
+ this.getRigidbodyRelativeMatrix(collider.gameObject, rb.gameObject, _matrix);
668
+
669
+ }
670
+ else {
671
+
672
+ const rigidBodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased();
673
+ const pos = getWorldPosition(collider.gameObject);
674
+ rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
675
+ rigidBodyDesc.setRotation(getWorldQuaternion(collider.gameObject));
676
+ rigidBody = this.world.createRigidBody(rigidBodyDesc);
677
+ _matrix.identity();
678
+ rigidBody[$componentKey] = null;
679
+
680
+ }
681
+
682
+ collider[$colliderRigidbody] = rigidBody;
683
+
684
+ return { rigidBody: rigidBody, useExplicitMassProperties: useExplicitMassProperties };
685
+ }
686
+
687
+ private internal_getRigidbody(rb: IRigidbody): RigidBody | null {
688
+ return rb[$bodyKey] as RigidBody;
689
+ }
690
+
691
+ private internalUpdateProperties(rb: IRigidbody, rigidbody: RigidBody) {
692
+ // continuous collision detection
693
+ // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#continuous-collision-detection
694
+ rigidbody.enableCcd(rb.collisionDetectionMode !== CollisionDetectionMode.Discrete);
695
+ rigidbody.setLinearDamping(rb.drag);
696
+ rigidbody.setAngularDamping(rb.angularDrag);
697
+ rigidbody.setGravityScale(rb.useGravity ? rb.gravityScale : 0, true);
698
+
699
+ // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
700
+ // rigidbody.setAdditionalMass(rb.mass, true);
701
+ // for (let i = 0; i < rigidbody.numColliders(); i++) {
702
+ // const collider = rigidbody.collider(i);
703
+ // if (collider) {
704
+ // collider.setMass(rb.mass);
705
+ // // const density = rb.mass / collider.shape.computeMassProperties().mass;
706
+ // }
707
+ // }
708
+
709
+ // lock rotations
710
+ rigidbody.setEnabledRotations(!rb.lockRotationX, !rb.lockRotationY, !rb.lockRotationZ, true);
711
+ rigidbody.setEnabledTranslations(!rb.lockPositionX, !rb.lockPositionY, !rb.lockPositionZ, true);
712
+
713
+ if (rb.isKinematic) {
714
+ rigidbody.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased);
715
+ }
716
+ else {
717
+ rigidbody.setBodyType(RAPIER.RigidBodyType.Dynamic);
718
+ }
719
+ }
720
+
721
+ // private _lastStepTime: number | undefined = 0;
722
+ private lines?: LineSegments;
723
+
724
+ public step(dt?: number) {
725
+ if (!this.world) return;
726
+ if (!this.enabled) return;
727
+ this._isUpdatingPhysicsWorld = true;
728
+ if (!this.eventQueue) {
729
+ this.eventQueue = new EventQueue(false);
730
+ }
731
+ if (dt) {
732
+ // if we make to sudden changes to the timestep the physics can get unstable
733
+ // https://rapier.rs/docs/user_guides/javascript/integration_parameters/#dt
734
+ this.world.timestep = Mathf.lerp(this.world.timestep, dt, 0.8);
735
+ }
736
+ this.world.step(this.eventQueue);
737
+ this._isUpdatingPhysicsWorld = false;
738
+ this.updateDebugRendering(this.world);
739
+ }
740
+
741
+ private updateDebugRendering(world: World) {
742
+ if (debugPhysics || debugColliderPlacement || showColliders) {
743
+ if (!this.lines) {
744
+ const material = new LineBasicMaterial({
745
+ color: 0x227700,
746
+ // vertexColors: THREE.VertexColors
747
+ });
748
+ const geometry = new BufferGeometry();
749
+ this.lines = new LineSegments(geometry, material);
750
+ this.context?.scene.add(this.lines);
751
+ }
752
+ const buffers = world.debugRender();
753
+ this.lines.geometry.setAttribute('position', new BufferAttribute(buffers.vertices, 3));
754
+ this.lines.geometry.setAttribute('color', new BufferAttribute(buffers.colors, 4));
755
+ }
756
+ }
757
+
758
+ public postStep() {
759
+ if (!this.world) return;
760
+ if (!this.enabled) return;
761
+ this._isUpdatingPhysicsWorld = true;
762
+ this.syncObjects();
763
+ this._isUpdatingPhysicsWorld = false;
764
+
765
+ if (this.eventQueue && !this.collisionHandler) {
766
+ this.collisionHandler = new PhysicsCollisionHandler(this.world, this.eventQueue);
767
+ }
768
+ if (this.collisionHandler) {
769
+ this.collisionHandler.handleCollisionEvents();
770
+ this.collisionHandler.update();
771
+ }
772
+ }
773
+
774
+ /** sync rendered objects with physics world (except for colliders without rigidbody) */
775
+ private syncObjects() {
776
+ if (debugColliderPlacement) return;
777
+ for (let i = 0; i < this.bodies.length; i++) {
778
+ const obj = this.objects[i];
779
+ const body = this.bodies[i] as Collider;
780
+
781
+ // if the collider is not attached to a rigidbody
782
+ // it means that its kinematic so we need to update its position
783
+ const col = (obj as ICollider);
784
+ if (col?.isCollider === true && !col.attachedRigidbody) {
785
+ const rigidbody = body.parent();
786
+ if (rigidbody)
787
+ this.syncPhysicsBody(obj.gameObject, rigidbody, true, true);
788
+ continue;
789
+ }
790
+
791
+
792
+ // sync
793
+ const pos = body.translation();
794
+ const rot = body.rotation();
795
+ // make sure to keep the collider offset
796
+ const center = obj["center"] as Vector3;
797
+ if (center && center.isVector3) {
798
+ this._tempQuaternion.set(rot.x, rot.y, rot.z, rot.w);
799
+ const offset = this._tempPosition.copy(center).applyQuaternion(this._tempQuaternion);
800
+ const scale = getWorldScale(obj.gameObject);
801
+ offset.multiply(scale);
802
+ pos.x -= offset.x;
803
+ pos.y -= offset.y;
804
+ pos.z -= offset.z;
805
+ }
806
+ setWorldPositionXYZ(obj.gameObject, pos.x, pos.y, pos.z);
807
+ setWorldQuaternionXYZW(obj.gameObject, rot.x, rot.y, rot.z, rot.w);
808
+ }
809
+ }
810
+
811
+ private syncPhysicsBody(obj: Object3D, body: RigidBody, translation: boolean, rotation: boolean) {
812
+
813
+ // const bodyType = body.bodyType();
814
+ // const previous = physicsBody.translation();
815
+ // const vel = physicsBody.linvel();
816
+
817
+ const worldPosition = getWorldPosition(obj, this._tempPosition);
818
+ const worldQuaternion = getWorldQuaternion(obj, this._tempQuaternion);
819
+ const type = body.bodyType();
820
+ switch (type) {
821
+ case RigidBodyType.Fixed:
822
+ case RigidBodyType.KinematicPositionBased:
823
+ case RigidBodyType.KinematicVelocityBased:
824
+ if (translation)
825
+ body.setNextKinematicTranslation(worldPosition);
826
+ if (rotation)
827
+ body.setNextKinematicRotation(worldQuaternion);
828
+ break;
829
+ default:
830
+ if (translation)
831
+ body.setTranslation(worldPosition, false);
832
+ if (rotation)
833
+ body.setRotation(worldQuaternion, false);
834
+ break;
835
+
836
+ }
837
+ body.wakeUp();
838
+ // physicsBody.setBodyType(RAPIER.RigidBodyType.Fixed);
839
+ // physicsBody.setLinvel(vel, false);
840
+
841
+ // update velocity
842
+ // const pos = physicsBody.translation();
843
+ // pos.x -= previous.x;
844
+ // pos.y -= previous.y;
845
+ // pos.z -= previous.z;
846
+ // // threhold
847
+ // const t = 1;
848
+ // const canUpdateVelocity = Math.abs(pos.x) < t && Math.abs(pos.y) < t && Math.abs(pos.z) < t;
849
+ // if (canUpdateVelocity) {
850
+ // const damping = 1 + this.context.time.deltaTime;
851
+ // vel.x *= damping;
852
+ // vel.y *= damping;
853
+ // vel.z *= damping;
854
+ // vel.x += pos.x;
855
+ // vel.y += pos.y;
856
+ // vel.z += pos.z;
857
+ // console.log(vel);
858
+ // physicsBody.setLinvel(vel, true);
859
+ // }
860
+ // else if(debugPhysics) console.warn("Movement exceeded threshold, not updating velocity", pos);
861
+
862
+ // body.setBodyType(bodyType);
863
+ }
864
+
865
+ private static _matricesBuffer: Matrix4[] = [];
866
+ private getRigidbodyRelativeMatrix(comp: Object3D, rigidbody: Object3D, mat: Matrix4, matrices?: Matrix4[]): Matrix4 {
867
+ // collect all matrices to the rigidbody and then build the rigidbody relative matrix
868
+ if (matrices === undefined) {
869
+ matrices = RapierPhysics._matricesBuffer;
870
+ matrices.length = 0;
871
+ }
872
+ if (comp === rigidbody) {
873
+ const scale = getWorldScale(comp, this._tempPosition);
874
+ mat.makeScale(scale.x, scale.y, scale.z);
875
+ for (let i = matrices.length - 1; i >= 0; i--) {
876
+ mat.multiply(matrices[i]);
877
+ }
878
+ return mat;
879
+ }
880
+ matrices.push(comp.matrix);
881
+ if (comp.parent) {
882
+ this.getRigidbodyRelativeMatrix(comp.parent, rigidbody, mat, matrices);
883
+ }
884
+ return mat;
885
+ }
886
+
887
+ private static centerConnectionPos = { x: 0, y: 0, z: 0 };
888
+ private static centerConnectionRot = { x: 0, y: 0, z: 0, w: 1 };
889
+
890
+
891
+
892
+ addFixedJoint(body1: IRigidbody, body2: IRigidbody) {
893
+ if (!this.world) {
894
+ console.error("Physics world not initialized");
895
+ return;
896
+ }
897
+ const b1 = body1[$bodyKey] as RigidBody;
898
+ const b2 = body2[$bodyKey] as RigidBody;
899
+
900
+ this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
901
+ this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
902
+
903
+ const params = JointData.fixed(
904
+ RapierPhysics.centerConnectionPos, RapierPhysics.centerConnectionRot,
905
+ this._tempPosition, this._tempQuaternion,
906
+ );
907
+ const joint = this.world.createImpulseJoint(params, b1, b2, true);
908
+ if (debugPhysics)
909
+ console.log("ADD FIXED JOINT", joint)
910
+ }
911
+
912
+
913
+ /** The joint prevents any relative movement between two rigid-bodies, except for relative rotations along one axis. This is typically used to simulate wheels, fans, etc. They are characterized by one local anchor as well as one local axis on each rigid-body. */
914
+ addHingeJoint(body1: IRigidbody, body2: IRigidbody, anchor: { x: number, y: number, z: number }, axis: { x: number, y: number, z: number }) {
915
+ if (!this.world) {
916
+ console.error("Physics world not initialized");
917
+ return;
918
+ }
919
+ const b1 = body1[$bodyKey] as RigidBody;
920
+ const b2 = body2[$bodyKey] as RigidBody;
921
+
922
+ this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
923
+ this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
924
+
925
+ let params = RAPIER.JointData.revolute(anchor, this._tempPosition, axis);
926
+ let joint = this.world.createImpulseJoint(params, b1, b2, true);
927
+ if (debugPhysics)
928
+ console.log("ADD HINGE JOINT", joint)
929
+ }
930
+
931
+
932
+ private calculateJointRelativeMatrices(body1: IGameObject, body2: IGameObject, mat: Matrix4) {
933
+ body1.updateWorldMatrix(true, false);
934
+ body2.updateWorldMatrix(true, false);
935
+ const world1 = body1.matrixWorld;
936
+ const world2 = body2.matrixWorld;
937
+ // set scale to 1
938
+ world1.elements[0] = 1;
939
+ world1.elements[5] = 1;
940
+ world1.elements[10] = 1;
941
+ world2.elements[0] = 1;
942
+ world2.elements[5] = 1;
943
+ world2.elements[10] = 1;
944
+ mat.copy(world2).premultiply(world1.invert()).invert();
945
+ }
946
+ }
947
+
948
+
949
+
950
+ /** responsible of processing collision events for the component system */
951
+ class PhysicsCollisionHandler {
952
+
953
+ readonly world: World;
954
+ readonly eventQueue: EventQueue;
955
+
956
+ constructor(world: World, eventQueue: EventQueue) {
957
+ this.world = world;
958
+ this.eventQueue = eventQueue;
959
+ }
960
+
961
+ private activeCollisions: Array<{ collider: ICollider, component: IComponent, collision: Collision }> = [];
962
+ private activeCollisionsStay: Array<{ collider: ICollider, component: IComponent, collision: Collision }> = [];
963
+ private activeTriggers: Array<{ collider: ICollider, component: IComponent, otherCollider: ICollider }> = [];
964
+
965
+ handleCollisionEvents() {
966
+ if (!this.eventQueue) return;
967
+ if (!this.world) return;
968
+ this.eventQueue.drainCollisionEvents((handle1, handle2, started) => {
969
+ const col1 = this.world!.getCollider(handle1);
970
+ const col2 = this.world!.getCollider(handle2);
971
+ const colliderComponent1 = col1[$componentKey];
972
+ const colliderComponent2 = col2[$componentKey];
973
+ if (debugCollisions)
974
+ console.log("EVT", colliderComponent1.name, colliderComponent2.name, started, col1, col2);
975
+ if (colliderComponent1 && colliderComponent2) {
976
+ if (started) {
977
+ this.onCollisionStarted(colliderComponent1, col1, colliderComponent2, col2);
978
+ this.onCollisionStarted(colliderComponent2, col2, colliderComponent1, col1);
979
+ }
980
+ else {
981
+ this.onCollisionEnded(colliderComponent1, colliderComponent2);
982
+ this.onCollisionEnded(colliderComponent2, colliderComponent1);
983
+ }
984
+ }
985
+ });
986
+ }
987
+
988
+ update() {
989
+ this.onHandleCollisionStay();
990
+ }
991
+
992
+ private onCollisionStarted(self: ICollider, selfBody: Collider, other: ICollider, otherBody: Collider) {
993
+ let collision: Collision | null = null;
994
+
995
+ // if one is a trigger we dont get collisions but want to raise the trigger events
996
+ if (self.isTrigger || other.isTrigger) {
997
+ foreachComponent(self.gameObject, (c: IComponent) => {
998
+ if (c.onTriggerEnter && !c.destroyed) {
999
+ c.onTriggerEnter(other);
1000
+ }
1001
+ this.activeTriggers.push({ collider: self, component: c, otherCollider: other });
1002
+ });
1003
+ }
1004
+ else {
1005
+ const object = self.gameObject;
1006
+ // TODO: we dont respect the flip value here!
1007
+ this.world.contactPair(selfBody, otherBody, (manifold, _flipped) => {
1008
+ foreachComponent(object, (c: IComponent) => {
1009
+ if (c.destroyed) return;
1010
+ const hasDeclaredEventMethod = c.onCollisionEnter || c.onCollisionStay || c.onCollisionExit;
1011
+ if (hasDeclaredEventMethod || debugCollisions) {
1012
+ if (!collision) {
1013
+ const contacts: Array<ContactPoint> = [];
1014
+ const normal = manifold.normal();
1015
+ for (let i = 0; i < manifold.numSolverContacts(); i++) {
1016
+ // solver points are in world space
1017
+ // https://rapier.rs/docs/user_guides/javascript/advanced_collision_detection_js#the-contact-graph
1018
+ const pt = manifold.solverContactPoint(i);
1019
+ const impulse = manifold.contactImpulse(i);
1020
+ if (pt) {
1021
+ const dist = manifold.contactDist(i);
1022
+ const friction = manifold.solverContactFriction(i);
1023
+ const contact = new ContactPoint(pt, dist, normal, impulse, friction);
1024
+ contacts.push(contact);
1025
+ if (debugCollisions) {
1026
+ Gizmos.DrawDirection(pt, normal, 0xff0000, 3, true);
1027
+ }
1028
+ }
1029
+ }
1030
+ collision = new Collision(object, other, contacts);
1031
+ }
1032
+
1033
+ // we only need to keep track if any event exists
1034
+ if (hasDeclaredEventMethod) {
1035
+ const info = { collider: self, component: c, collision };
1036
+
1037
+ this.activeCollisions.push(info);
1038
+ if (c.onCollisionStay) {
1039
+ this.activeCollisionsStay.push(info);
1040
+ }
1041
+
1042
+ c.onCollisionEnter?.call(c, collision);
1043
+ }
1044
+
1045
+ }
1046
+ });
1047
+ });
1048
+ }
1049
+ }
1050
+
1051
+ private onHandleCollisionStay() {
1052
+ for (const active of this.activeCollisionsStay) {
1053
+ const c = active.component;
1054
+ if (c.destroyed) continue;
1055
+ if (c.activeAndEnabled && c.onCollisionStay) {
1056
+ const arg = active.collision;
1057
+ c.onCollisionStay(arg);
1058
+ }
1059
+ }
1060
+ for (const active of this.activeTriggers) {
1061
+ const c = active.component;
1062
+ if (c.destroyed) continue;
1063
+ if (c.activeAndEnabled && c.onTriggerStay) {
1064
+ const arg = active.otherCollider;
1065
+ c.onTriggerStay(arg);
1066
+ }
1067
+ }
1068
+ }
1069
+
1070
+ private onCollisionEnded(self: ICollider, other: ICollider) {
1071
+ if (self.destroyed || other.destroyed) return;
1072
+ for (let i = 0; i < this.activeCollisions.length; i++) {
1073
+ const active = this.activeCollisions[i];
1074
+ const collider = active.collider;
1075
+ if (collider.destroyed) {
1076
+ this.activeCollisions.splice(i, 1);
1077
+ i--;
1078
+ continue;
1079
+ }
1080
+ if (collider === self && active.collision.collider === other) {
1081
+ const c = active.component;
1082
+ this.activeCollisions.splice(i, 1);
1083
+ i--;
1084
+ if (c.activeAndEnabled && c.onCollisionExit) {
1085
+ const collision = active.collision;
1086
+ c.onCollisionExit(collision);
1087
+ }
1088
+ }
1089
+ }
1090
+ for (let i = 0; i < this.activeCollisionsStay.length; i++) {
1091
+ const active = this.activeCollisionsStay[i];
1092
+ const collider = active.collider;
1093
+ if (collider.destroyed) {
1094
+ this.activeCollisionsStay.splice(i, 1);
1095
+ i--;
1096
+ continue;
1097
+ }
1098
+ if (collider === self && active.collision.collider === other) {
1099
+ const c = active.component;
1100
+ this.activeCollisionsStay.splice(i, 1);
1101
+ i--;
1102
+ if (c.activeAndEnabled && c.onCollisionExit) {
1103
+ const collision = active.collision;
1104
+ c.onCollisionExit(collision);
1105
+ }
1106
+ }
1107
+ }
1108
+ for (let i = 0; i < this.activeTriggers.length; i++) {
1109
+ const active = this.activeTriggers[i];
1110
+ const collider = active.collider;
1111
+ if (collider.destroyed) {
1112
+ this.activeTriggers.splice(i, 1);
1113
+ i--;
1114
+ continue;
1115
+ }
1116
+ if (collider === self && active.otherCollider === other) {
1117
+ const c = active.component;
1118
+ this.activeTriggers.splice(i, 1);
1119
+ i--;
1120
+ if (c.activeAndEnabled && c.onTriggerExit) {
1121
+ const collision = active.otherCollider;
1122
+ c.onTriggerExit(collision);
1123
+ }
1124
+ }
1125
+ }
1126
+ }
1127
+ }