@threenative/physics 0.3.6 → 0.3.9

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.
@@ -25,7 +25,6 @@ interface IPhysicsWorldHandle extends IPhysicsHandle {
25
25
  declare function physicsHandle(raw: unknown): IPhysicsHandle;
26
26
  declare function physicsWorldHandle(raw: unknown, simulation: unknown): IPhysicsWorldHandle;
27
27
 
28
- /** One record is logical body id, xyz position, and xyzw rotation. */
29
28
  declare const PHYSICS_TRANSFORM_STRIDE = 8;
30
29
  declare const PHYSICS_COLLISION_EVENT_STRIDE = 4;
31
30
  /** Caps native query buffers while remaining exactly representable by the native ABI. */
@@ -106,6 +105,8 @@ interface IPhysicsRayQuery {
106
105
  readonly from: IPhysicsVector3;
107
106
  readonly to: IPhysicsVector3;
108
107
  readonly collisionMask: number;
108
+ /** Godot `hit_from_inside`; default false: a solid that contains `from` is skipped. */
109
+ readonly hitFromInside?: boolean;
109
110
  }
110
111
  interface IPhysicsShapeQuery {
111
112
  readonly shape: IPhysicsShapeDescriptor;
@@ -304,6 +305,15 @@ interface IPhysicsSimulation {
304
305
  /** Cold-path respawn: move the chassis, drop both velocities, and face the given yaw in radians. */
305
306
  resetVehicle?(id: number, position: IPhysicsVector3, yaw: number): void;
306
307
  drainCollisionEvents(buffer: Uint32Array): number;
308
+ /**
309
+ * Per-body backend crossings of the last `step()` and `readVisibleTransforms()`: bodies whose
310
+ * forces were cleared and bodies whose pose was read from the backend. Fixed and sleeping
311
+ * bodies cost neither. Optional: a bulk backend crosses once per step, whatever the count.
312
+ */
313
+ readonly crossings?: {
314
+ readonly forceResets: number;
315
+ readonly transformReads: number;
316
+ };
307
317
  dispose(): void;
308
318
  }
309
319
  /** Runtime metadata needed to expose backend-specific escape hatches without leaking them. */
@@ -597,6 +607,11 @@ interface IIntersectRayOptions {
597
607
  readonly from: PhysicsQueryVector3;
598
608
  readonly to: PhysicsQueryVector3;
599
609
  readonly collisionMask?: number;
610
+ /**
611
+ * Godot `PhysicsRayQueryParameters3D.hit_from_inside`, default false: a ray whose origin is
612
+ * inside a solid ignores that solid. True returns it as a distance 0 hit with a zero normal.
613
+ */
614
+ readonly hitFromInside?: boolean;
600
615
  }
601
616
  interface IIntersectShapeOptions {
602
617
  readonly shape: CollisionShape3D;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { I as IPhysicsContext, a as IPhysicsWorldHandle, b as IPhysicsBodyHandle, c as IPhysicsVector3, d as IPhysicsRotation, e as IPhysicsJointLimit, R as RigidBody3D, f as IPhysicsColliderHandle, P as PhysicsBody3D, C as CollisionShape3D, g as IPhysicsVehicleWheelOptions } from './Area3D-vbmjpxsN.js';
2
- export { A as Area3D, B as Buoyancy3D, h as BuoyancyPointPosition, i as CharacterBody3D, j as IBuoyancy3DOptions, k as IBuoyancyHullPoint, l as IBuoyancySurface, m as IBuoyancySurfaceSample, n as ICollisionShapeHandle, o as IIntersectPointOptions, p as IIntersectRayOptions, q as IIntersectShapeOptions, r as IPhysicsBodyCreateOptions, s as IPhysicsCharacterOptions, t as IPhysicsHandle, u as IPhysicsInputSnapshot, v as IPhysicsJointCreateOptions, w as IPhysicsPointQuery, x as IPhysicsQueryHit, y as IPhysicsRayHit, z as IPhysicsRayQuery, D as IPhysicsRuntimeSimulation, E as IPhysicsShapeDescriptor, F as IPhysicsShapeQuery, G as IPhysicsSimulation, H as IPhysicsVehicleCreateOptions, J as IPhysicsVehicleInput, K as IPhysicsVehicleState, L as IPointHit, M as IRayHit, N as IRigidBody3DOptions, O as IShapeHit, Q as MAX_PHYSICS_QUERY_RESULTS, S as PHYSICS_COLLISION_EVENT_STRIDE, T as PHYSICS_TRANSFORM_STRIDE, U as PHYSICS_VEHICLE_WHEEL_STRIDE, V as PhysicsDirectSpaceState3D, W as PhysicsJointKind, X as PhysicsQueryVector3, Y as PhysicsShapeKind, Z as RigidBodyType, _ as rapier } from './Area3D-vbmjpxsN.js';
1
+ import { I as IPhysicsContext, a as IPhysicsWorldHandle, b as IPhysicsBodyHandle, c as IPhysicsVector3, d as IPhysicsRotation, e as IPhysicsJointLimit, R as RigidBody3D, f as IPhysicsColliderHandle, P as PhysicsBody3D, C as CollisionShape3D, g as IPhysicsVehicleWheelOptions } from './Area3D-AgKEDg0q.js';
2
+ export { A as Area3D, B as Buoyancy3D, h as BuoyancyPointPosition, i as CharacterBody3D, j as IBuoyancy3DOptions, k as IBuoyancyHullPoint, l as IBuoyancySurface, m as IBuoyancySurfaceSample, n as ICollisionShapeHandle, o as IIntersectPointOptions, p as IIntersectRayOptions, q as IIntersectShapeOptions, r as IPhysicsBodyCreateOptions, s as IPhysicsCharacterOptions, t as IPhysicsHandle, u as IPhysicsInputSnapshot, v as IPhysicsJointCreateOptions, w as IPhysicsPointQuery, x as IPhysicsQueryHit, y as IPhysicsRayHit, z as IPhysicsRayQuery, D as IPhysicsRuntimeSimulation, E as IPhysicsShapeDescriptor, F as IPhysicsShapeQuery, G as IPhysicsSimulation, H as IPhysicsVehicleCreateOptions, J as IPhysicsVehicleInput, K as IPhysicsVehicleState, L as IPointHit, M as IRayHit, N as IRigidBody3DOptions, O as IShapeHit, Q as MAX_PHYSICS_QUERY_RESULTS, S as PHYSICS_COLLISION_EVENT_STRIDE, T as PHYSICS_TRANSFORM_STRIDE, U as PHYSICS_VEHICLE_WHEEL_STRIDE, V as PhysicsDirectSpaceState3D, W as PhysicsJointKind, X as PhysicsQueryVector3, Y as PhysicsShapeKind, Z as RigidBodyType, _ as rapier } from './Area3D-AgKEDg0q.js';
3
3
  import { Object3D } from 'three';
4
4
  import { ISoftBodyCollision } from '@threenative/core';
5
5
  import { ISnowFootprint, SnowField } from '@threenative/core/world';
package/dist/index.js CHANGED
@@ -28,6 +28,18 @@ function physicsColliderHandle(id, raw) {
28
28
  }
29
29
 
30
30
  // src/simulation.ts
31
+ function countEngineCost(name, count) {
32
+ const host = globalThis;
33
+ host.__tnEngineCost ??= {};
34
+ host.__tnEngineCost[name] = (host.__tnEngineCost[name] ?? 0) + count;
35
+ }
36
+ var ENGINE_COST_KNOB = "__THREENATIVE_ENGINE_COST__";
37
+ function stepClock() {
38
+ return globalThis.performance?.now() ?? Date.now();
39
+ }
40
+ function engineCostAsked() {
41
+ return Reflect.get(globalThis, ENGINE_COST_KNOB) === true;
42
+ }
31
43
  var PHYSICS_TRANSFORM_STRIDE = 8;
32
44
  var PHYSICS_COLLISION_EVENT_STRIDE = 4;
33
45
  var PHYSICS_SLEEP_STATE_STRIDE = 2;
@@ -182,6 +194,9 @@ var QUERY_SHAPE_KINDS = /* @__PURE__ */ new Set([
182
194
  "convexHull",
183
195
  "heightfield"
184
196
  ]);
197
+ function startsInside(hit) {
198
+ return hit.timeOfImpact === 0;
199
+ }
185
200
  function queryObject(value, label) {
186
201
  if (typeof value !== "object" || value === null)
187
202
  throw new Error(`IPhysicsSimulation ${label} must be an object.`);
@@ -245,9 +260,12 @@ function requirePhysicsRayQuery(value) {
245
260
  const to = queryVector(query.to, "ray to");
246
261
  if (from.x === to.x && from.y === to.y && from.z === to.z)
247
262
  throw new Error("IPhysicsSimulation ray must have non-zero length.");
263
+ if (query.hitFromInside !== void 0 && typeof query.hitFromInside !== "boolean")
264
+ throw new Error("IPhysicsSimulation ray hitFromInside must be a boolean.");
248
265
  return {
249
266
  collisionMask: requirePhysicsCollisionMask(query.collisionMask),
250
267
  from,
268
+ hitFromInside: query.hitFromInside === true,
251
269
  to
252
270
  };
253
271
  }
@@ -303,11 +321,10 @@ function createWebPhysicsShape(rapier3, shape, sensor = shape.sensor) {
303
321
  descriptor.setActiveEvents(rapier3.ActiveEvents.COLLISION_EVENTS);
304
322
  return descriptor;
305
323
  }
306
- function bodyDescription(rapier3, type, position, rotation, mass, continuousCollision) {
324
+ function bodyDescription(rapier3, type, position, rotation, continuousCollision) {
307
325
  const description = type === "fixed" ? rapier3.RigidBodyDesc.fixed() : type === "kinematic" || type === "character" ? rapier3.RigidBodyDesc.kinematicPositionBased() : rapier3.RigidBodyDesc.dynamic();
308
326
  description.setTranslation(position.x, position.y, position.z).setRotation({ x: rotation.x, y: rotation.y, z: rotation.z, w: rotation.w });
309
327
  if (type === "dynamic") description.setCcdEnabled(continuousCollision);
310
- if (mass !== 0) description.setAdditionalMass(mass);
311
328
  return description;
312
329
  }
313
330
  function jointDescription(rapier3, jointOptions) {
@@ -351,7 +368,7 @@ function characterState(simulationBody, byCollider) {
351
368
  }
352
369
  }
353
370
  }
354
- const grounded = controller.computedGrounded() && (walkable || contacts === 0);
371
+ const grounded = controller.computedGrounded() && (walkable || contacts === 0 || simulationBody.held === true);
355
372
  if (groundCollider !== void 0) simulationBody.groundCollider = groundCollider;
356
373
  else if (!grounded) simulationBody.groundCollider = void 0;
357
374
  state.grounded = grounded;
@@ -395,6 +412,19 @@ function writeRapierTransformRecord(entry, renderBuffer, offset) {
395
412
  renderBuffer[offset + 6] = rotation.z;
396
413
  renderBuffer[offset + 7] = rotation.w;
397
414
  }
415
+ function writeVisibleRecord(entry, renderBuffer, offset) {
416
+ const fixed = entry.type === "fixed";
417
+ if (!(fixed && entry.restingPose !== void 0) && !entry.body.isValid()) return "invalid";
418
+ const resting = fixed || entry.type === "dynamic" && entry.body.isSleeping();
419
+ if (resting && entry.restingPose !== void 0) {
420
+ renderBuffer[offset] = entry.id;
421
+ renderBuffer.set(entry.restingPose, offset + 1);
422
+ return "replayed";
423
+ }
424
+ writeRapierTransformRecord(entry, renderBuffer, offset);
425
+ entry.restingPose = resting ? renderBuffer.slice(offset + 1, offset + PHYSICS_TRANSFORM_STRIDE) : void 0;
426
+ return "read";
427
+ }
398
428
  function writeContactRecord(buffer, record, colliderId, manifold, flipped) {
399
429
  const solved = manifold.numSolverContacts();
400
430
  let impulse = 0;
@@ -448,6 +478,20 @@ function createWebPhysicsSimulation(options) {
448
478
  const requireLive = () => {
449
479
  if (disposed) throw new Error("Physics simulation is disposed.");
450
480
  };
481
+ const countStepWork = () => {
482
+ if (!engineCostAsked()) return;
483
+ let awake = 0;
484
+ let dynamic = 0;
485
+ for (const entry of bodies.values()) {
486
+ if (entry.type !== "dynamic") continue;
487
+ dynamic += 1;
488
+ if (!entry.body.isSleeping()) awake += 1;
489
+ }
490
+ countEngineCost("physicsBodies", bodies.size);
491
+ countEngineCost("physicsColliders", byCollider.size);
492
+ countEngineCost("physicsDynamicBodies", dynamic);
493
+ countEngineCost("physicsAwakeBodies", awake);
494
+ };
451
495
  const requireDynamic = (entry, id, operation) => {
452
496
  if (entry === void 0)
453
497
  throw new Error(`TN_PHYSICS_UNKNOWN_BODY: ${operation} references an unknown body ${id}.`);
@@ -492,7 +536,9 @@ function createWebPhysicsSimulation(options) {
492
536
  throw new Error(`TN_VEHICLE_UNKNOWN: ${operation} references an unknown vehicle ${id}.`);
493
537
  return record;
494
538
  };
539
+ const crossings = { forceResets: 0, transformReads: 0 };
495
540
  const simulation = {
541
+ crossings,
496
542
  version: options.version,
497
543
  rawWorld: options.world,
498
544
  rawEventQueue: options.eventQueue,
@@ -508,13 +554,13 @@ function createWebPhysicsSimulation(options) {
508
554
  const id = nextId;
509
555
  nextId += 1;
510
556
  const rawShape = createWebPhysicsShape(options.rapier, bodyOptions.shape, sensor);
557
+ if (bodyOptions.mass > 0) rawShape.setMass(bodyOptions.mass);
511
558
  const rawBody = options.world.createRigidBody(
512
559
  bodyDescription(
513
560
  options.rapier,
514
561
  bodyOptions.type,
515
562
  bodyOptions.position,
516
563
  bodyOptions.rotation,
517
- bodyOptions.mass,
518
564
  effectiveContinuousCollision(bodyOptions.type, bodyOptions.continuousCollision)
519
565
  )
520
566
  );
@@ -629,6 +675,7 @@ function createWebPhysicsSimulation(options) {
629
675
  entry.body.setTranslation(position, true);
630
676
  options.world.propagateModifiedBodyPositionsToColliders();
631
677
  dirtyBodies.add(entry);
678
+ entry.restingPose = void 0;
632
679
  },
633
680
  applyBodyImpulse: (id, impulse) => {
634
681
  requireLive();
@@ -662,6 +709,9 @@ function createWebPhysicsSimulation(options) {
662
709
  },
663
710
  step: (deltaTime, inputSnapshot) => {
664
711
  requireLive();
712
+ const counted = engineCostAsked();
713
+ const startedAt = counted ? stepClock() : 0;
714
+ if (counted) countStepWork();
665
715
  requirePhysicsStepInput(deltaTime, inputSnapshot, (id) => bodies.has(id));
666
716
  if (inputSnapshot !== void 0) {
667
717
  for (let index = 0; index < inputSnapshot.kinematicCount; index += 1) {
@@ -706,6 +756,7 @@ function createWebPhysicsSimulation(options) {
706
756
  filterPredicate
707
757
  );
708
758
  const movement = controller.computedMovement();
759
+ entry.held = desired.y < -1e-4 && Math.abs(movement.y) < 1e-4;
709
760
  const nextTranslation = kinematicScratch.nextTranslation;
710
761
  nextTranslation.x = current.x + movement.x;
711
762
  nextTranslation.y = current.y + movement.y;
@@ -734,22 +785,29 @@ function createWebPhysicsSimulation(options) {
734
785
  }
735
786
  }
736
787
  options.world.step(options.eventQueue);
788
+ crossings.forceResets = 0;
737
789
  for (const entry of bodies.values()) {
790
+ if (entry.type !== "dynamic") continue;
738
791
  entry.body.resetForces(true);
739
792
  entry.body.resetTorques(true);
793
+ crossings.forceResets += 1;
740
794
  }
795
+ countEngineCost("physicsForceResets", crossings.forceResets);
741
796
  dirtyBodies.clear();
797
+ if (counted) countEngineCost("physicsStepUs", Math.round((stepClock() - startedAt) * 1e3));
742
798
  },
743
799
  readVisibleTransforms: (renderBuffer) => {
744
800
  requireLive();
745
801
  requirePhysicsRenderBuffer(renderBuffer, bodies.size);
746
802
  let index = 0;
803
+ crossings.transformReads = 0;
747
804
  for (const entry of bodies.values()) {
748
- if (!entry.body.isValid()) continue;
749
- const offset = index * PHYSICS_TRANSFORM_STRIDE;
750
- writeRapierTransformRecord(entry, renderBuffer, offset);
805
+ const written = writeVisibleRecord(entry, renderBuffer, index * PHYSICS_TRANSFORM_STRIDE);
806
+ if (written === "invalid") continue;
807
+ if (written === "read") crossings.transformReads += 1;
751
808
  index += 1;
752
809
  }
810
+ countEngineCost("physicsTransformReads", crossings.transformReads);
753
811
  return index;
754
812
  },
755
813
  readBodySleepStates: (buffer) => {
@@ -777,23 +835,35 @@ function createWebPhysicsSimulation(options) {
777
835
  y: dy / distance,
778
836
  z: dz / distance
779
837
  });
780
- const hit = options.world.castRayAndGetNormal(
781
- ray,
782
- distance,
783
- true,
784
- options.rapier.QueryFilterFlags.EXCLUDE_SENSORS,
785
- void 0,
786
- void 0,
787
- void 0,
788
- queryPredicate(query.collisionMask)
789
- );
790
- let closestCollider = hit?.collider;
791
- let closestTime = hit?.timeOfImpact;
792
- let closestNormal = hit?.normal;
838
+ const skipped = /* @__PURE__ */ new Set();
839
+ const accepts = queryPredicate(query.collisionMask);
840
+ let closestCollider;
841
+ let closestTime;
842
+ let closestNormal;
843
+ for (; ; ) {
844
+ const hit = options.world.castRayAndGetNormal(
845
+ ray,
846
+ distance,
847
+ true,
848
+ options.rapier.QueryFilterFlags.EXCLUDE_SENSORS,
849
+ void 0,
850
+ void 0,
851
+ void 0,
852
+ (collider) => !skipped.has(collider.handle) && accepts(collider)
853
+ );
854
+ if (hit !== null && !query.hitFromInside && startsInside(hit)) {
855
+ skipped.add(hit.collider.handle);
856
+ continue;
857
+ }
858
+ closestCollider = hit?.collider;
859
+ closestTime = hit?.timeOfImpact;
860
+ closestNormal = hit?.normal;
861
+ break;
862
+ }
793
863
  for (const dirtyBody of dirtyBodies) {
794
864
  if (!queryMatches(query.collisionMask, dirtyBody.collider)) continue;
795
865
  const dirtyHit = dirtyBody.collider.castRayAndGetNormal(ray, distance, true);
796
- if (dirtyHit === null || closestTime !== void 0 && dirtyHit.timeOfImpact >= closestTime)
866
+ if (dirtyHit === null || !query.hitFromInside && startsInside(dirtyHit) || closestTime !== void 0 && dirtyHit.timeOfImpact >= closestTime)
797
867
  continue;
798
868
  closestCollider = dirtyBody.collider;
799
869
  closestTime = dirtyHit.timeOfImpact;
@@ -808,7 +878,8 @@ function createWebPhysicsSimulation(options) {
808
878
  return {
809
879
  ...queryHit(entry),
810
880
  distance: closestTime,
811
- normal: { x: closestNormal.x, y: closestNormal.y, z: closestNormal.z },
881
+ // `hitFromInside` reports the solid around the origin with a zero normal, whatever its shape.
882
+ normal: closestTime === 0 ? { x: 0, y: 0, z: 0 } : { x: closestNormal.x, y: closestNormal.y, z: closestNormal.z },
812
883
  position: { x: position.x, y: position.y, z: position.z }
813
884
  };
814
885
  },
@@ -1144,6 +1215,11 @@ function bulkTransformValue(values, index) {
1144
1215
  throw new Error("IPhysicsSimulation returned a malformed transform.");
1145
1216
  return value;
1146
1217
  }
1218
+ function setRotationIfChanged(quaternion, x, y, z, w) {
1219
+ if (Object.is(quaternion.x, x) && Object.is(quaternion.y, y) && Object.is(quaternion.z, z) && Object.is(quaternion.w, w))
1220
+ return;
1221
+ quaternion.set(x, y, z, w);
1222
+ }
1147
1223
 
1148
1224
  // src/Area3D.ts
1149
1225
  var MAX_CONTACT_LOG = 1e3;
@@ -1450,7 +1526,7 @@ var CharacterBody3D = class {
1450
1526
  }
1451
1527
  if (this.#sliding && this.grounded && this.velocity.y < 0) this.velocity.y = 0;
1452
1528
  this.object.position.set(x, y, z);
1453
- this.object.quaternion.set(qx, qy, qz, qw);
1529
+ setRotationIfChanged(this.object.quaternion, qx, qy, qz, qw);
1454
1530
  this.#desired.x = 0;
1455
1531
  this.#desired.y = 0;
1456
1532
  this.#desired.z = 0;
@@ -1464,7 +1540,8 @@ var CharacterBody3D = class {
1464
1540
  const transform = this.#simulation.readBodyTransform(this.body.id);
1465
1541
  if (transform === void 0) return;
1466
1542
  this.object.position.set(transform.position.x, transform.position.y, transform.position.z);
1467
- this.object.quaternion.set(
1543
+ setRotationIfChanged(
1544
+ this.object.quaternion,
1468
1545
  transform.rotation.x,
1469
1546
  transform.rotation.y,
1470
1547
  transform.rotation.z,
@@ -1919,6 +1996,7 @@ function rayQuery(options) {
1919
1996
  return requirePhysicsRayQuery({
1920
1997
  collisionMask: options.collisionMask === void 0 ? 65535 : options.collisionMask,
1921
1998
  from: options.from,
1999
+ hitFromInside: options.hitFromInside,
1922
2000
  to: options.to
1923
2001
  });
1924
2002
  }
@@ -2117,7 +2195,8 @@ var RigidBody3D = class {
2117
2195
  const transform = this.#simulation.readBodyTransform(this.body.id);
2118
2196
  if (transform === void 0) return;
2119
2197
  object.position.set(transform.position.x, transform.position.y, transform.position.z);
2120
- object.quaternion.set(
2198
+ setRotationIfChanged(
2199
+ object.quaternion,
2121
2200
  transform.rotation.x,
2122
2201
  transform.rotation.y,
2123
2202
  transform.rotation.z,
@@ -2138,7 +2217,7 @@ var RigidBody3D = class {
2138
2217
  const object = this.#object;
2139
2218
  if (object === void 0) return;
2140
2219
  object.position.set(x, y, z);
2141
- object.quaternion.set(qx, qy, qz, qw);
2220
+ setRotationIfChanged(object.quaternion, qx, qy, qz, qw);
2142
2221
  this.#lastPosition.x = x;
2143
2222
  this.#lastPosition.y = y;
2144
2223
  this.#lastPosition.z = z;
@@ -1,4 +1,4 @@
1
- export { A as Area3D, $ as AreaEvent, a0 as AreaHandler, B as Buoyancy3D, h as BuoyancyPointPosition, i as CharacterBody3D, C as CollisionShape3D, a1 as CollisionShapeKind, a2 as IArea3DOptions, a3 as IAreaContact, j as IBuoyancy3DOptions, k as IBuoyancyHullPoint, l as IBuoyancySurface, m as IBuoyancySurfaceSample, a4 as ICharacterBody3DOptions, o as IIntersectPointOptions, p as IIntersectRayOptions, q as IIntersectShapeOptions, r as IPhysicsBodyCreateOptions, b as IPhysicsBodyHandle, s as IPhysicsCharacterOptions, f as IPhysicsColliderHandle, I as IPhysicsContext, t as IPhysicsHandle, u as IPhysicsInputSnapshot, v as IPhysicsJointCreateOptions, e as IPhysicsJointLimit, a5 as IPhysicsOptions, w as IPhysicsPointQuery, x as IPhysicsQueryHit, y as IPhysicsRayHit, z as IPhysicsRayQuery, d as IPhysicsRotation, D as IPhysicsRuntimeSimulation, E as IPhysicsShapeDescriptor, F as IPhysicsShapeQuery, G as IPhysicsSimulation, c as IPhysicsVector3, H as IPhysicsVehicleCreateOptions, J as IPhysicsVehicleInput, K as IPhysicsVehicleState, a as IPhysicsWorldHandle, L as IPointHit, M as IRayHit, N as IRigidBody3DOptions, O as IShapeHit, Q as MAX_PHYSICS_QUERY_RESULTS, S as PHYSICS_COLLISION_EVENT_STRIDE, T as PHYSICS_TRANSFORM_STRIDE, U as PHYSICS_VEHICLE_WHEEL_STRIDE, P as PhysicsBody3D, V as PhysicsDirectSpaceState3D, W as PhysicsJointKind, a6 as PhysicsPlugin, X as PhysicsQueryVector3, Y as PhysicsShapeKind, R as RigidBody3D, Z as RigidBodyType, _ as rapier } from '../Area3D-vbmjpxsN.js';
1
+ export { A as Area3D, $ as AreaEvent, a0 as AreaHandler, B as Buoyancy3D, h as BuoyancyPointPosition, i as CharacterBody3D, C as CollisionShape3D, a1 as CollisionShapeKind, a2 as IArea3DOptions, a3 as IAreaContact, j as IBuoyancy3DOptions, k as IBuoyancyHullPoint, l as IBuoyancySurface, m as IBuoyancySurfaceSample, a4 as ICharacterBody3DOptions, o as IIntersectPointOptions, p as IIntersectRayOptions, q as IIntersectShapeOptions, r as IPhysicsBodyCreateOptions, b as IPhysicsBodyHandle, s as IPhysicsCharacterOptions, f as IPhysicsColliderHandle, I as IPhysicsContext, t as IPhysicsHandle, u as IPhysicsInputSnapshot, v as IPhysicsJointCreateOptions, e as IPhysicsJointLimit, a5 as IPhysicsOptions, w as IPhysicsPointQuery, x as IPhysicsQueryHit, y as IPhysicsRayHit, z as IPhysicsRayQuery, d as IPhysicsRotation, D as IPhysicsRuntimeSimulation, E as IPhysicsShapeDescriptor, F as IPhysicsShapeQuery, G as IPhysicsSimulation, c as IPhysicsVector3, H as IPhysicsVehicleCreateOptions, J as IPhysicsVehicleInput, K as IPhysicsVehicleState, a as IPhysicsWorldHandle, L as IPointHit, M as IRayHit, N as IRigidBody3DOptions, O as IShapeHit, Q as MAX_PHYSICS_QUERY_RESULTS, S as PHYSICS_COLLISION_EVENT_STRIDE, T as PHYSICS_TRANSFORM_STRIDE, U as PHYSICS_VEHICLE_WHEEL_STRIDE, P as PhysicsBody3D, V as PhysicsDirectSpaceState3D, W as PhysicsJointKind, a6 as PhysicsPlugin, X as PhysicsQueryVector3, Y as PhysicsShapeKind, R as RigidBody3D, Z as RigidBodyType, _ as rapier } from '../Area3D-AgKEDg0q.js';
2
2
  export { IBuildStaticCollidersOptions, IFixedJoint3DOptions, IHingeJoint3DOptions, IJoint3DOptions, IPinJoint3DOptions, ISnowPhysicsBinding, ISnowPhysicsObservation, ISnowPhysicsOptions, IStaticColliderContext, IVehicleBody3DOptions, IVehicleWheel3D, Joint3D, PhysicsJointBody, StaticColliderPredicate, VehicleBody3D, VehicleForwardAxis, attachSnowPhysics, boxFootprint, buildStaticColliders, capsuleFootprint, interactionGroups, softBodyCollision } from '../index.js';
3
3
  import 'three';
4
4
  import '@threenative/core';
@@ -234,9 +234,12 @@ function requirePhysicsRayQuery(value) {
234
234
  const to = queryVector(query.to, "ray to");
235
235
  if (from.x === to.x && from.y === to.y && from.z === to.z)
236
236
  throw new Error("IPhysicsSimulation ray must have non-zero length.");
237
+ if (query.hitFromInside !== void 0 && typeof query.hitFromInside !== "boolean")
238
+ throw new Error("IPhysicsSimulation ray hitFromInside must be a boolean.");
237
239
  return {
238
240
  collisionMask: requirePhysicsCollisionMask(query.collisionMask),
239
241
  from,
242
+ hitFromInside: query.hitFromInside === true,
240
243
  to
241
244
  };
242
245
  }
@@ -909,6 +912,11 @@ function bulkTransformValue(values, index) {
909
912
  throw new Error("IPhysicsSimulation returned a malformed transform.");
910
913
  return value;
911
914
  }
915
+ function setRotationIfChanged(quaternion, x, y, z, w) {
916
+ if (Object.is(quaternion.x, x) && Object.is(quaternion.y, y) && Object.is(quaternion.z, z) && Object.is(quaternion.w, w))
917
+ return;
918
+ quaternion.set(x, y, z, w);
919
+ }
912
920
 
913
921
  // src/Area3D.ts
914
922
  var MAX_CONTACT_LOG = 1e3;
@@ -1215,7 +1223,7 @@ var CharacterBody3D = class {
1215
1223
  }
1216
1224
  if (this.#sliding && this.grounded && this.velocity.y < 0) this.velocity.y = 0;
1217
1225
  this.object.position.set(x, y, z);
1218
- this.object.quaternion.set(qx, qy, qz, qw);
1226
+ setRotationIfChanged(this.object.quaternion, qx, qy, qz, qw);
1219
1227
  this.#desired.x = 0;
1220
1228
  this.#desired.y = 0;
1221
1229
  this.#desired.z = 0;
@@ -1229,7 +1237,8 @@ var CharacterBody3D = class {
1229
1237
  const transform = this.#simulation.readBodyTransform(this.body.id);
1230
1238
  if (transform === void 0) return;
1231
1239
  this.object.position.set(transform.position.x, transform.position.y, transform.position.z);
1232
- this.object.quaternion.set(
1240
+ setRotationIfChanged(
1241
+ this.object.quaternion,
1233
1242
  transform.rotation.x,
1234
1243
  transform.rotation.y,
1235
1244
  transform.rotation.z,
@@ -1684,6 +1693,7 @@ function rayQuery(options) {
1684
1693
  return requirePhysicsRayQuery({
1685
1694
  collisionMask: options.collisionMask === void 0 ? 65535 : options.collisionMask,
1686
1695
  from: options.from,
1696
+ hitFromInside: options.hitFromInside,
1687
1697
  to: options.to
1688
1698
  });
1689
1699
  }
@@ -1882,7 +1892,8 @@ var RigidBody3D = class {
1882
1892
  const transform = this.#simulation.readBodyTransform(this.body.id);
1883
1893
  if (transform === void 0) return;
1884
1894
  object.position.set(transform.position.x, transform.position.y, transform.position.z);
1885
- object.quaternion.set(
1895
+ setRotationIfChanged(
1896
+ object.quaternion,
1886
1897
  transform.rotation.x,
1887
1898
  transform.rotation.y,
1888
1899
  transform.rotation.z,
@@ -1903,7 +1914,7 @@ var RigidBody3D = class {
1903
1914
  const object = this.#object;
1904
1915
  if (object === void 0) return;
1905
1916
  object.position.set(x, y, z);
1906
- object.quaternion.set(qx, qy, qz, qw);
1917
+ setRotationIfChanged(object.quaternion, qx, qy, qz, qw);
1907
1918
  this.#lastPosition.x = x;
1908
1919
  this.#lastPosition.y = y;
1909
1920
  this.#lastPosition.z = z;
@@ -1,4 +1,4 @@
1
1
  import '@threenative/core';
2
2
  import 'recast-navigation';
3
- export { a7 as INavigationAgent3DOptions, a8 as INavigationContext, a9 as INavigationObstacle3DOptions, aa as INavigationRegion3DOptions, ab as NavigationAgent3D, ac as NavigationAgentEvent, ad as NavigationAgentHandler, ae as NavigationObstacle3D, af as NavigationPlugin, ag as NavigationRegion3D, ah as recast } from '../Area3D-vbmjpxsN.js';
3
+ export { a7 as INavigationAgent3DOptions, a8 as INavigationContext, a9 as INavigationObstacle3DOptions, aa as INavigationRegion3DOptions, ab as NavigationAgent3D, ac as NavigationAgentEvent, ad as NavigationAgentHandler, ae as NavigationObstacle3D, af as NavigationPlugin, ag as NavigationRegion3D, ah as recast } from '../Area3D-AgKEDg0q.js';
4
4
  import 'three';
@@ -139,14 +139,14 @@ var NavigationAgent3D = class {
139
139
  this.#path = [];
140
140
  this.#crowdAgent?.resetMoveTarget();
141
141
  } else if (start.polyRef === end.polyRef) {
142
- this.#storeComputedPath(target);
142
+ this.#storeComputedPath(target, end.point);
143
143
  if (!navigationPointMatchesTarget(end.point, target, this.targetDesiredDistance, this.height)) {
144
144
  this.#crowdAgent?.resetMoveTarget();
145
145
  } else if (this.#path.length > 0) {
146
146
  this.#requestCrowdMoveTarget(target);
147
147
  }
148
148
  } else {
149
- const reachable = this.#storeComputedPath(target);
149
+ const reachable = this.#storeComputedPath(target, end.point);
150
150
  if (!reachable) {
151
151
  this.#crowdAgent?.resetMoveTarget();
152
152
  } else if (this.#path.length > 0) {
@@ -177,14 +177,14 @@ var NavigationAgent3D = class {
177
177
  * whether its final waypoint lands within tolerance. The one place both `setTargetPosition`
178
178
  * and reachability judgments get their path from.
179
179
  */
180
- #storeComputedPath(target) {
180
+ #storeComputedPath(target, projectedTarget) {
181
181
  const result = this.navigation.query.computePath(
182
182
  toNavigationVector(this.object.position, this.#objectRecord),
183
183
  toNavigationVector(target, this.#targetRecord)
184
184
  );
185
185
  this.#path = result.success ? result.path.map((point) => new Vector3(point.x, point.y, point.z)) : [];
186
186
  const final = this.#path.at(-1);
187
- return result.success && final !== void 0 && navigationPointMatchesTarget(final, target, this.targetDesiredDistance, this.height);
187
+ return result.success && final !== void 0 && navigationPointMatchesTarget(final, target, this.targetDesiredDistance, this.height) && navigationPointMatchesTarget(projectedTarget, target, this.targetDesiredDistance, this.height);
188
188
  }
189
189
  isTargetReachable(position) {
190
190
  const target = position ?? this.#target;
@@ -208,7 +208,7 @@ var NavigationAgent3D = class {
208
208
  toNavigationVector(target, this.#targetRecord)
209
209
  );
210
210
  const final = path.path.at(-1);
211
- return path.success && final !== void 0 && navigationPointMatchesTarget(final, target, this.targetDesiredDistance, this.height);
211
+ return path.success && final !== void 0 && navigationPointMatchesTarget(final, target, this.targetDesiredDistance, this.height) && navigationPointMatchesTarget(end.point, target, this.targetDesiredDistance, this.height);
212
212
  }
213
213
  getFinalPosition() {
214
214
  return this.#path.at(-1)?.clone() ?? this.object.position.clone();
@@ -248,7 +248,12 @@ var NavigationAgent3D = class {
248
248
  break;
249
249
  this.#pathIndex += 1;
250
250
  }
251
- const reached = this.#pathIndex === this.#path.length - 1 && horizontalDistanceSquared(this.object.position, this.#target) <= this.targetDesiredDistance ** 2;
251
+ const reached = this.#pathIndex === this.#path.length - 1 && navigationPointMatchesTarget(
252
+ this.object.position,
253
+ this.#target,
254
+ this.targetDesiredDistance,
255
+ this.height
256
+ );
252
257
  if (!reached || this.#finished) return;
253
258
  this.#finished = true;
254
259
  for (const handler of this.#listeners.targetReached) handler();
@@ -571,10 +576,10 @@ var NavigationRegion3D = class {
571
576
  dispose() {
572
577
  if (this.#disposed) return;
573
578
  this.#disposed = true;
574
- for (const obstacle of [...this.navigation.obstacles]) obstacle.dispose();
575
- for (const agent of [...this.navigation.agents]) agent.dispose();
576
579
  this.navigation.regions.delete(this);
577
580
  if (this.navigation.navMesh === this.navigationMesh) {
581
+ for (const obstacle of [...this.navigation.obstacles]) obstacle.dispose();
582
+ for (const agent of [...this.navigation.agents]) agent.dispose();
578
583
  this.navigation.crowd?.destroy();
579
584
  this.navigation.crowd = void 0;
580
585
  this.navigation.query.destroy();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threenative/physics",
3
- "version": "0.3.6",
3
+ "version": "0.3.9",
4
4
  "license": "MIT",
5
5
  "description": "Thin Rapier physics bindings for ThreeNative games.",
6
6
  "keywords": [
@@ -55,7 +55,7 @@
55
55
  "recast-navigation": "0.43.1"
56
56
  },
57
57
  "peerDependencies": {
58
- "@threenative/core": ">=0.3.6 <0.4.0",
58
+ "@threenative/core": ">=0.3.9 <0.4.0",
59
59
  "three": ">=0.185.0 <0.186.0"
60
60
  },
61
61
  "devDependencies": {
@@ -65,7 +65,7 @@
65
65
  "tsup": "8.5.1",
66
66
  "typescript": "npm:@typescript/typescript6@6.0.2",
67
67
  "@typescript/native": "npm:typescript@7.0.2",
68
- "@threenative/core": "0.3.6"
68
+ "@threenative/core": "0.3.9"
69
69
  },
70
70
  "scripts": {
71
71
  "build": "tsup --config tsup.config.ts && publint",