@dcl/ecs 7.21.1-23203004012.commit-7c64ac2 → 7.21.1-23252789519.commit-9f806e1

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 (37) hide show
  1. package/dist/components/manual/Transform.d.ts +13 -0
  2. package/dist/components/manual/Transform.js +8 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +1 -0
  5. package/dist/runtime/helpers/index.d.ts +1 -0
  6. package/dist/runtime/helpers/index.js +1 -0
  7. package/dist/runtime/helpers/tree.d.ts +6 -0
  8. package/dist/runtime/helpers/tree.js +2 -2
  9. package/dist/runtime/helpers/vectors.d.ts +1 -0
  10. package/dist/runtime/helpers/vectors.js +36 -0
  11. package/dist/runtime/initialization/index.d.ts +7 -0
  12. package/dist/runtime/initialization/index.js +6 -0
  13. package/dist/systems/physics-force.d.ts +1 -0
  14. package/dist/systems/physics-force.js +140 -0
  15. package/dist/systems/physics-impulse.d.ts +12 -0
  16. package/dist/systems/physics-impulse.js +85 -0
  17. package/dist/systems/physics.d.ts +77 -0
  18. package/dist/systems/physics.js +18 -0
  19. package/dist-cjs/components/manual/Transform.d.ts +13 -0
  20. package/dist-cjs/components/manual/Transform.js +31 -0
  21. package/dist-cjs/index.d.ts +1 -0
  22. package/dist-cjs/index.js +1 -0
  23. package/dist-cjs/runtime/helpers/index.d.ts +1 -0
  24. package/dist-cjs/runtime/helpers/index.js +1 -0
  25. package/dist-cjs/runtime/helpers/tree.d.ts +6 -0
  26. package/dist-cjs/runtime/helpers/tree.js +3 -2
  27. package/dist-cjs/runtime/helpers/vectors.d.ts +1 -0
  28. package/dist-cjs/runtime/helpers/vectors.js +39 -0
  29. package/dist-cjs/runtime/initialization/index.d.ts +7 -0
  30. package/dist-cjs/runtime/initialization/index.js +7 -1
  31. package/dist-cjs/systems/physics-force.d.ts +1 -0
  32. package/dist-cjs/systems/physics-force.js +167 -0
  33. package/dist-cjs/systems/physics-impulse.d.ts +12 -0
  34. package/dist-cjs/systems/physics-impulse.js +112 -0
  35. package/dist-cjs/systems/physics.d.ts +77 -0
  36. package/dist-cjs/systems/physics.js +23 -0
  37. package/package.json +3 -2
@@ -1,5 +1,6 @@
1
1
  import { LastWriteWinElementSetComponentDefinition, IEngine } from '../../engine';
2
2
  import { Entity } from '../../engine/entity';
3
+ import type { Vector3Type } from '../../schemas/custom/Vector3';
3
4
  /**
4
5
  * @public
5
6
  */
@@ -10,6 +11,18 @@ export type TransformComponent = LastWriteWinElementSetComponentDefinition<Trans
10
11
  export interface TransformComponentExtended extends TransformComponent {
11
12
  create(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
12
13
  createOrReplace(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
14
+ /**
15
+ * Transforms a direction vector from an entity's local coordinate space
16
+ * to world space, accounting for the full parent hierarchy.
17
+ *
18
+ * This applies only rotation (not translation or scale) — suitable for
19
+ * direction vectors like force/impulse directions.
20
+ *
21
+ * @param entity - The source entity whose local space defines the direction
22
+ * @param localDirection - Direction vector in the entity's local coordinates
23
+ * @returns The direction vector in world coordinates
24
+ */
25
+ localToWorldDirection(entity: Entity, localDirection: Vector3Type): Vector3Type;
13
26
  }
14
27
  /**
15
28
  * @public
@@ -1,3 +1,7 @@
1
+ // Use import * to safely handle circular dependency (tree.ts → components → Transform.ts → tree.ts).
2
+ // With import *, the namespace object's properties are resolved at access time (live bindings in ESM,
3
+ // getters via __importStar in CJS), so by the time methods are called all exports are available.
4
+ import * as treeHelpers from '../../runtime/helpers/tree';
1
5
  /**
2
6
  * @internal
3
7
  */
@@ -101,6 +105,10 @@ export function defineTransformComponent(engine) {
101
105
  },
102
106
  createOrReplace(entity, val) {
103
107
  return transformDef.createOrReplace(entity, TransformSchema.extend(val));
108
+ },
109
+ localToWorldDirection(entity, localDirection) {
110
+ const worldRotation = treeHelpers.getWorldRotation(engine, entity);
111
+ return treeHelpers.rotateVectorByQuaternion(localDirection, worldRotation);
104
112
  }
105
113
  };
106
114
  }
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export * from './systems/assetLoad';
12
12
  export * from './systems/async-task';
13
13
  export * from './systems/tween';
14
14
  export * from './systems/triggerArea';
15
+ export * from './systems/physics';
15
16
  export * from './engine/entity';
16
17
  export * from './components/types';
17
18
  import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended, AudioStreamComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent, VirtualCameraComponentDefinitionExtended, InputModifierComponentDefinitionExtended, LightSourceComponentDefinitionExtended, TriggerAreaComponentDefinitionExtended } from './components/types';
package/dist/index.js CHANGED
@@ -13,6 +13,7 @@ export * from './systems/assetLoad';
13
13
  export * from './systems/async-task';
14
14
  export * from './systems/tween';
15
15
  export * from './systems/triggerArea';
16
+ export * from './systems/physics';
16
17
  export * from './engine/entity';
17
18
  export * from './components/types';
18
19
  // @internal
@@ -1,3 +1,4 @@
1
1
  export * from './coordinates';
2
2
  export * from './tree';
3
+ export * from './vectors';
3
4
  export { createTimers, Timers, TimerId, TimerCallback } from './timers';
@@ -1,3 +1,4 @@
1
1
  export * from './coordinates';
2
2
  export * from './tree';
3
+ export * from './vectors';
3
4
  export { createTimers } from './timers';
@@ -2,6 +2,12 @@ import { Entity } from '../../engine/entity';
2
2
  import { ComponentDefinition, IEngine } from '../../engine';
3
3
  import { Vector3Type } from '../../schemas/custom/Vector3';
4
4
  import { QuaternionType } from '../../schemas/custom/Quaternion';
5
+ /**
6
+ * @public
7
+ * Rotate a vector by a quaternion
8
+ * Uses the formula: v' = q * v * q^(-1), optimized version
9
+ */
10
+ export declare function rotateVectorByQuaternion(v: Vector3Type, q: QuaternionType): Vector3Type;
5
11
  /**
6
12
  * Get an iterator of entities that follow a tree structure for a component
7
13
  * @public
@@ -35,11 +35,11 @@ function multiplyQuaternions(q1, q2) {
35
35
  };
36
36
  }
37
37
  /**
38
- * @internal
38
+ * @public
39
39
  * Rotate a vector by a quaternion
40
40
  * Uses the formula: v' = q * v * q^(-1), optimized version
41
41
  */
42
- function rotateVectorByQuaternion(v, q) {
42
+ export function rotateVectorByQuaternion(v, q) {
43
43
  // Extract quaternion components
44
44
  const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
45
45
  // Calculate cross product terms (q.xyz × v) * 2
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Lightweight Vector3 math utilities for internal use.
3
+ * Mirrors the subset of @dcl/ecs-math Vector3 API used by the physics helpers.
4
+ *
5
+ * TEMPORARY WORKAROUND: @dcl/ecs-math ships ESM-only, which breaks the dist-cjs build.
6
+ * The proper fix is to add a CJS build to @dcl/ecs-math upstream, then replace this
7
+ * file with `import { Vector3 } from '@dcl/ecs-math'`.
8
+ *
9
+ * @internal
10
+ */
11
+ export const Vector3 = {
12
+ add(a, b) {
13
+ return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
14
+ },
15
+ subtract(a, b) {
16
+ return { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
17
+ },
18
+ scale(v, s) {
19
+ return { x: v.x * s, y: v.y * s, z: v.z * s };
20
+ },
21
+ length(v) {
22
+ return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
23
+ },
24
+ normalize(v) {
25
+ const len = Vector3.length(v);
26
+ if (len === 0)
27
+ return { x: 0, y: 0, z: 0 };
28
+ return { x: v.x / len, y: v.y / len, z: v.z / len };
29
+ },
30
+ equals(a, b) {
31
+ return a.x === b.x && a.y === b.y && a.z === b.z;
32
+ },
33
+ equalsToFloats(v, x, y, z) {
34
+ return v.x === x && v.y === y && v.z === z;
35
+ }
36
+ };
@@ -12,6 +12,7 @@ import { AssetLoadLoadingStateSystem } from '../../systems/assetLoad';
12
12
  import { TweenSystem } from '../../systems/tween';
13
13
  import { TriggerAreaEventsSystem } from '../../systems/triggerArea';
14
14
  import { createTimers, Timers } from '../helpers/timers';
15
+ import { PhysicsSystem } from '../../systems/physics';
15
16
  /**
16
17
  * @public
17
18
  * The engine is the part of the scene that sits in the middle and manages all of the other parts.
@@ -77,6 +78,12 @@ export { TriggerAreaEventsSystem };
77
78
  */
78
79
  export declare const timers: Timers;
79
80
  export { Timers, createTimers };
81
+ /**
82
+ * @public
83
+ * Physics helpers for applying impulses and forces to the player.
84
+ */
85
+ export declare const Physics: PhysicsSystem;
86
+ export { PhysicsSystem };
80
87
  /**
81
88
  * @public
82
89
  * Runs an async function
@@ -14,6 +14,7 @@ import { pointerEventColliderChecker } from '../../systems/pointer-event-collide
14
14
  import { createTriggerAreaEventsSystem } from '../../systems/triggerArea';
15
15
  import { createTimers } from '../helpers/timers';
16
16
  import { setGlobalPolyfill } from '../globals';
17
+ import { createPhysicsSystem } from '../../systems/physics';
17
18
  /**
18
19
  * @public
19
20
  * The engine is the part of the scene that sits in the middle and manages all of the other parts.
@@ -77,6 +78,11 @@ setGlobalPolyfill('setTimeout', timers.setTimeout);
77
78
  setGlobalPolyfill('clearTimeout', timers.clearTimeout);
78
79
  setGlobalPolyfill('setInterval', timers.setInterval);
79
80
  setGlobalPolyfill('clearInterval', timers.clearInterval);
81
+ /**
82
+ * @public
83
+ * Physics helpers for applying impulses and forces to the player.
84
+ */
85
+ export const Physics = /* @__PURE__ */ createPhysicsSystem(engine);
80
86
  /**
81
87
  * Adds pointer event collider system only in DEV env
82
88
  */
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,140 @@
1
+ import * as components from '../components';
2
+ import { Vector3, createTimers } from '../runtime/helpers';
3
+ import { KnockbackFalloff } from './physics-impulse';
4
+ /** @internal */
5
+ export function createPhysicsForceHelper(engine) {
6
+ const PhysicsCombinedForce = components.PhysicsCombinedForce(engine);
7
+ const Transform = components.Transform(engine);
8
+ const timers = createTimers(engine);
9
+ const durationTimers = new Map();
10
+ // Key = source entity producing the force (not the target); all forces target PlayerEntity.
11
+ const forceSources = new Map();
12
+ const repulsionSources = new Map();
13
+ let lastWrittenForceVector = null;
14
+ function recalcForce() {
15
+ if (forceSources.size === 0) {
16
+ if (PhysicsCombinedForce.has(engine.PlayerEntity)) {
17
+ PhysicsCombinedForce.deleteFrom(engine.PlayerEntity);
18
+ }
19
+ lastWrittenForceVector = null;
20
+ return;
21
+ }
22
+ const current = PhysicsCombinedForce.getOrNull(engine.PlayerEntity);
23
+ if (current && lastWrittenForceVector && current.vector) {
24
+ if (!Vector3.equals(current.vector, lastWrittenForceVector)) {
25
+ console.error('PBPhysicsCombinedForce was modified externally.', 'Expected:', lastWrittenForceVector, 'Found:', current.vector, '-- overwriting with local state.');
26
+ }
27
+ }
28
+ let sum = { x: 0, y: 0, z: 0 };
29
+ for (const v of forceSources.values()) {
30
+ sum = Vector3.add(sum, v);
31
+ }
32
+ PhysicsCombinedForce.createOrReplace(engine.PlayerEntity, { vector: sum });
33
+ lastWrittenForceVector = sum;
34
+ }
35
+ function applyForceToPlayer(source, vector, magnitude) {
36
+ let finalVector;
37
+ if (Vector3.equalsToFloats(vector, 0, 0, 0))
38
+ return;
39
+ if (typeof magnitude === 'number') {
40
+ finalVector = Vector3.scale(Vector3.normalize(vector), magnitude);
41
+ }
42
+ else {
43
+ finalVector = vector;
44
+ }
45
+ forceSources.set(source, finalVector);
46
+ recalcForce();
47
+ }
48
+ function removeForceFromPlayer(source) {
49
+ repulsionSources.delete(source);
50
+ const timerId = durationTimers.get(source);
51
+ if (timerId !== undefined) {
52
+ timers.clearTimeout(timerId);
53
+ durationTimers.delete(source);
54
+ }
55
+ if (!forceSources.has(source))
56
+ return;
57
+ forceSources.delete(source);
58
+ recalcForce();
59
+ }
60
+ function scheduleForceDuration(source, seconds) {
61
+ const existing = durationTimers.get(source);
62
+ if (existing !== undefined) {
63
+ timers.clearTimeout(existing);
64
+ }
65
+ const timerId = timers.setTimeout(() => {
66
+ durationTimers.delete(source);
67
+ removeForceFromPlayer(source);
68
+ }, seconds * 1000);
69
+ durationTimers.set(source, timerId);
70
+ }
71
+ function applyForceToPlayerForDuration(source, duration, vector, magnitude) {
72
+ applyForceToPlayer(source, vector, magnitude);
73
+ scheduleForceDuration(source, duration);
74
+ }
75
+ function computeRepulsionVector(fromPosition, magnitude, radius, falloff) {
76
+ const diff = Vector3.subtract(Transform.get(engine.PlayerEntity).position, fromPosition);
77
+ if (Vector3.equalsToFloats(diff, 0, 0, 0))
78
+ return { x: 0, y: magnitude, z: 0 };
79
+ // Fast path: default params — no need to compute distance
80
+ if (radius === Infinity && falloff === KnockbackFalloff.CONSTANT) {
81
+ return Vector3.scale(Vector3.normalize(diff), magnitude);
82
+ }
83
+ const distance = Vector3.length(diff);
84
+ if (distance > radius)
85
+ return null;
86
+ let effectiveMagnitude;
87
+ switch (falloff) {
88
+ case KnockbackFalloff.LINEAR:
89
+ effectiveMagnitude = magnitude * (1 - distance / radius);
90
+ break;
91
+ case KnockbackFalloff.INVERSE_SQUARE:
92
+ effectiveMagnitude = magnitude / (distance * distance + 1);
93
+ break;
94
+ case KnockbackFalloff.CONSTANT:
95
+ default:
96
+ effectiveMagnitude = magnitude;
97
+ break;
98
+ }
99
+ if (effectiveMagnitude === 0)
100
+ return null;
101
+ // normalize(diff) * effectiveMagnitude in one step
102
+ return Vector3.scale(diff, effectiveMagnitude / distance);
103
+ }
104
+ function applyRepulsionForceToPlayer(source, fromPosition, magnitude, radius = Infinity, falloff = KnockbackFalloff.CONSTANT) {
105
+ repulsionSources.set(source, { fromPosition, magnitude, radius, falloff });
106
+ const vector = computeRepulsionVector(fromPosition, magnitude, radius, falloff);
107
+ if (vector) {
108
+ forceSources.set(source, vector);
109
+ }
110
+ else {
111
+ forceSources.delete(source);
112
+ }
113
+ recalcForce();
114
+ }
115
+ // Background system: recalculate repulsion vectors and clean up stale forces every tick.
116
+ // Stale forces can appear when CRDT sync from another client writes PhysicsCombinedForce
117
+ // externally (entity-1 ambiguity: each client interprets entity 1 as its own player).
118
+ engine.addSystem(() => {
119
+ // Repulsion forces need per-tick direction recalculation as the player moves
120
+ if (repulsionSources.size > 0) {
121
+ for (const [source, { fromPosition, magnitude, radius, falloff }] of repulsionSources) {
122
+ const vector = computeRepulsionVector(fromPosition, magnitude, radius, falloff);
123
+ if (vector) {
124
+ forceSources.set(source, vector);
125
+ }
126
+ else {
127
+ forceSources.delete(source);
128
+ }
129
+ }
130
+ recalcForce();
131
+ return;
132
+ }
133
+ // No local sources — clean up any externally-created component (e.g. from CRDT sync)
134
+ if (forceSources.size === 0 && PhysicsCombinedForce.has(engine.PlayerEntity)) {
135
+ PhysicsCombinedForce.deleteFrom(engine.PlayerEntity);
136
+ lastWrittenForceVector = null;
137
+ }
138
+ });
139
+ return { applyForceToPlayer, removeForceFromPlayer, applyForceToPlayerForDuration, applyRepulsionForceToPlayer };
140
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @public
3
+ * Falloff mode for knockback force over distance.
4
+ */
5
+ export declare enum KnockbackFalloff {
6
+ /** Same force at any distance within radius */
7
+ CONSTANT = 0,
8
+ /** Smooth linear decrease to 0 at radius edge: F = magnitude * (1 - distance / radius) */
9
+ LINEAR = 1,
10
+ /** Sharp drop-off, physically realistic: F = magnitude / (distance^2 + 1) */
11
+ INVERSE_SQUARE = 2
12
+ }
@@ -0,0 +1,85 @@
1
+ import * as components from '../components';
2
+ import { Vector3 } from '../runtime/helpers';
3
+ /**
4
+ * @public
5
+ * Falloff mode for knockback force over distance.
6
+ */
7
+ export var KnockbackFalloff;
8
+ (function (KnockbackFalloff) {
9
+ /** Same force at any distance within radius */
10
+ KnockbackFalloff[KnockbackFalloff["CONSTANT"] = 0] = "CONSTANT";
11
+ /** Smooth linear decrease to 0 at radius edge: F = magnitude * (1 - distance / radius) */
12
+ KnockbackFalloff[KnockbackFalloff["LINEAR"] = 1] = "LINEAR";
13
+ /** Sharp drop-off, physically realistic: F = magnitude / (distance^2 + 1) */
14
+ KnockbackFalloff[KnockbackFalloff["INVERSE_SQUARE"] = 2] = "INVERSE_SQUARE";
15
+ })(KnockbackFalloff || (KnockbackFalloff = {}));
16
+ /** @internal */
17
+ export function createPhysicsImpulseHelper(engine) {
18
+ const PhysicsCombinedImpulse = components.PhysicsCombinedImpulse(engine);
19
+ const Transform = components.Transform(engine);
20
+ const EngineInfo = components.EngineInfo(engine);
21
+ let impulseEventId = 0;
22
+ let lastWrittenEventId = 0;
23
+ let lastWrittenTick = -1;
24
+ function applyImpulseToPlayer(vector, magnitude) {
25
+ let finalVector;
26
+ if (typeof magnitude === 'number') {
27
+ if (Vector3.equalsToFloats(vector, 0, 0, 0))
28
+ return;
29
+ finalVector = Vector3.scale(Vector3.normalize(vector), magnitude);
30
+ }
31
+ else {
32
+ if (Vector3.equalsToFloats(vector, 0, 0, 0))
33
+ return;
34
+ finalVector = vector;
35
+ }
36
+ const currentTick = EngineInfo.getOrNull(engine.RootEntity)?.tickNumber ?? 0;
37
+ const existing = PhysicsCombinedImpulse.getOrNull(engine.PlayerEntity);
38
+ if (existing && existing.eventId !== lastWrittenEventId && lastWrittenEventId !== 0) {
39
+ throw new Error('PBPhysicsCombinedImpulse was modified outside Physics helper. ' +
40
+ 'Do not mix direct component access with Physics.applyImpulseToPlayer().');
41
+ }
42
+ if (lastWrittenTick === currentTick && existing) {
43
+ finalVector = Vector3.add(existing.vector ?? { x: 0, y: 0, z: 0 }, finalVector);
44
+ }
45
+ else {
46
+ lastWrittenEventId = ++impulseEventId;
47
+ }
48
+ lastWrittenTick = currentTick;
49
+ PhysicsCombinedImpulse.createOrReplace(engine.PlayerEntity, {
50
+ vector: finalVector,
51
+ eventId: lastWrittenEventId
52
+ });
53
+ }
54
+ function applyKnockbackToPlayer(fromPosition, magnitude, radius = Infinity, falloff = KnockbackFalloff.CONSTANT) {
55
+ const diff = Vector3.subtract(Transform.get(engine.PlayerEntity).position, fromPosition);
56
+ if (Vector3.equalsToFloats(diff, 0, 0, 0)) {
57
+ applyImpulseToPlayer({ x: 0, y: magnitude, z: 0 });
58
+ return;
59
+ }
60
+ // Fast path: default params — no need to compute distance
61
+ if (radius === Infinity && falloff === KnockbackFalloff.CONSTANT) {
62
+ applyImpulseToPlayer(Vector3.scale(Vector3.normalize(diff), magnitude));
63
+ return;
64
+ }
65
+ const distance = Vector3.length(diff);
66
+ if (distance > radius)
67
+ return;
68
+ let effectiveMagnitude;
69
+ switch (falloff) {
70
+ case KnockbackFalloff.LINEAR:
71
+ effectiveMagnitude = magnitude * (1 - distance / radius);
72
+ break;
73
+ case KnockbackFalloff.INVERSE_SQUARE:
74
+ effectiveMagnitude = magnitude / (distance * distance + 1);
75
+ break;
76
+ case KnockbackFalloff.CONSTANT:
77
+ default:
78
+ effectiveMagnitude = magnitude;
79
+ break;
80
+ }
81
+ // normalize(diff) * effectiveMagnitude in one step
82
+ applyImpulseToPlayer(Vector3.scale(diff, effectiveMagnitude / distance));
83
+ }
84
+ return { applyImpulseToPlayer, applyKnockbackToPlayer };
85
+ }
@@ -0,0 +1,77 @@
1
+ import { Entity } from '../engine';
2
+ import { Vector3Type } from '../schemas';
3
+ import { KnockbackFalloff } from './physics-impulse';
4
+ export { KnockbackFalloff } from './physics-impulse';
5
+ /**
6
+ * @public
7
+ */
8
+ export interface PhysicsSystem {
9
+ /**
10
+ * Apply a one-shot impulse to the player entity.
11
+ * Multiple calls within the same frame are accumulated (summed).
12
+ *
13
+ * @param vector - a single `vector` whose length encodes the strength.
14
+ * or use overload for `direction` with a separate `magnitude` — the direction will be normalized before scaling.
15
+ */
16
+ applyImpulseToPlayer(vector: Vector3Type): void;
17
+ applyImpulseToPlayer(direction: Vector3Type, magnitude: number): void;
18
+ /**
19
+ * Apply a continuous force to the player from a given source entity.
20
+ * Multiple sources are accumulated: the registry sums all active forces
21
+ * and writes a single PBPhysicsCombinedForce component.
22
+ * Calling again with the same source replaces its previous force.
23
+ *
24
+ * @param source - Entity key identifying this force source
25
+ * @param vector - single `vector` whose length encodes the strength
26
+ * or use overload for `direction` with a separate `magnitude` — the direction will be normalized before scaling.
27
+ */
28
+ applyForceToPlayer(source: Entity, vector: Vector3Type): void;
29
+ applyForceToPlayer(source: Entity, direction: Vector3Type, magnitude: number): void;
30
+ /**
31
+ * Remove a continuous force from the player. Remaining sources are
32
+ * re-summed; if none remain the force is cleared. No-op if the source
33
+ * is not registered.
34
+ *
35
+ * @param source - Entity key identifying the force source to remove
36
+ */
37
+ removeForceFromPlayer(source: Entity): void;
38
+ /**
39
+ * Push the player away from a point. Computes direction from
40
+ * `fromPosition` to the player, applies falloff, and delegates
41
+ * to applyImpulseToPlayer.
42
+ *
43
+ * If the player is exactly at `fromPosition`, pushes upward.
44
+ *
45
+ * @param fromPosition - world-space origin of the knockback (explosion center, enemy position, etc.)
46
+ * @param magnitude - base impulse strength
47
+ * @param radius - max distance of effect (default: Infinity)
48
+ * @param falloff - how force decreases with distance (default: CONSTANT)
49
+ */
50
+ applyKnockbackToPlayer(fromPosition: Vector3Type, magnitude: number, radius?: number, falloff?: KnockbackFalloff): void;
51
+ /**
52
+ * Apply a continuous force to the player for a limited duration.
53
+ * After `duration` seconds the force is automatically removed.
54
+ * Calling again with the same source resets the timer.
55
+ *
56
+ * @param source - Entity key identifying this force source
57
+ * @param duration - how long the force lasts, in seconds
58
+ * @param vector - single `vector` whose length encodes the strength
59
+ * or use overload for `direction` with a separate `magnitude` — the direction will be normalized before scaling.
60
+ */
61
+ applyForceToPlayerForDuration(source: Entity, duration: number, vector: Vector3Type): void;
62
+ applyForceToPlayerForDuration(source: Entity, duration: number, direction: Vector3Type, magnitude: number): void;
63
+ /**
64
+ * Apply a continuous repulsion force that pushes the player away from a point,
65
+ * recalculating direction every tick as the player moves.
66
+ * Remove with `removeForceFromPlayer(source)`.
67
+ *
68
+ * Negative magnitude = attraction (pulls toward the source).
69
+ *
70
+ * @param source - Entity key identifying this force source
71
+ * @param fromPosition - world-space origin of repulsion
72
+ * @param magnitude - base force strength
73
+ * @param radius - max distance of effect (default: Infinity)
74
+ * @param falloff - how force decreases with distance (default: CONSTANT)
75
+ */
76
+ applyRepulsionForceToPlayer(source: Entity, fromPosition: Vector3Type, magnitude: number, radius?: number, falloff?: KnockbackFalloff): void;
77
+ }
@@ -0,0 +1,18 @@
1
+ import { createPhysicsImpulseHelper } from './physics-impulse';
2
+ import { createPhysicsForceHelper } from './physics-force';
3
+ export { KnockbackFalloff } from './physics-impulse';
4
+ /**
5
+ * @internal
6
+ */
7
+ export function createPhysicsSystem(engine) {
8
+ const impulse = createPhysicsImpulseHelper(engine);
9
+ const force = createPhysicsForceHelper(engine);
10
+ return {
11
+ applyImpulseToPlayer: impulse.applyImpulseToPlayer,
12
+ applyForceToPlayer: force.applyForceToPlayer,
13
+ removeForceFromPlayer: force.removeForceFromPlayer,
14
+ applyKnockbackToPlayer: impulse.applyKnockbackToPlayer,
15
+ applyForceToPlayerForDuration: force.applyForceToPlayerForDuration,
16
+ applyRepulsionForceToPlayer: force.applyRepulsionForceToPlayer
17
+ };
18
+ }
@@ -1,5 +1,6 @@
1
1
  import { LastWriteWinElementSetComponentDefinition, IEngine } from '../../engine';
2
2
  import { Entity } from '../../engine/entity';
3
+ import type { Vector3Type } from '../../schemas/custom/Vector3';
3
4
  /**
4
5
  * @public
5
6
  */
@@ -10,6 +11,18 @@ export type TransformComponent = LastWriteWinElementSetComponentDefinition<Trans
10
11
  export interface TransformComponentExtended extends TransformComponent {
11
12
  create(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
12
13
  createOrReplace(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
14
+ /**
15
+ * Transforms a direction vector from an entity's local coordinate space
16
+ * to world space, accounting for the full parent hierarchy.
17
+ *
18
+ * This applies only rotation (not translation or scale) — suitable for
19
+ * direction vectors like force/impulse directions.
20
+ *
21
+ * @param entity - The source entity whose local space defines the direction
22
+ * @param localDirection - Direction vector in the entity's local coordinates
23
+ * @returns The direction vector in world coordinates
24
+ */
25
+ localToWorldDirection(entity: Entity, localDirection: Vector3Type): Vector3Type;
13
26
  }
14
27
  /**
15
28
  * @public
@@ -1,6 +1,33 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.defineTransformComponent = exports.TransformSchema = exports.TRANSFORM_LENGTH = exports.COMPONENT_ID = void 0;
27
+ // Use import * to safely handle circular dependency (tree.ts → components → Transform.ts → tree.ts).
28
+ // With import *, the namespace object's properties are resolved at access time (live bindings in ESM,
29
+ // getters via __importStar in CJS), so by the time methods are called all exports are available.
30
+ const treeHelpers = __importStar(require("../../runtime/helpers/tree"));
4
31
  /**
5
32
  * @internal
6
33
  */
@@ -104,6 +131,10 @@ function defineTransformComponent(engine) {
104
131
  },
105
132
  createOrReplace(entity, val) {
106
133
  return transformDef.createOrReplace(entity, exports.TransformSchema.extend(val));
134
+ },
135
+ localToWorldDirection(entity, localDirection) {
136
+ const worldRotation = treeHelpers.getWorldRotation(engine, entity);
137
+ return treeHelpers.rotateVectorByQuaternion(localDirection, worldRotation);
107
138
  }
108
139
  };
109
140
  }
@@ -12,6 +12,7 @@ export * from './systems/assetLoad';
12
12
  export * from './systems/async-task';
13
13
  export * from './systems/tween';
14
14
  export * from './systems/triggerArea';
15
+ export * from './systems/physics';
15
16
  export * from './engine/entity';
16
17
  export * from './components/types';
17
18
  import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended, AudioStreamComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent, VirtualCameraComponentDefinitionExtended, InputModifierComponentDefinitionExtended, LightSourceComponentDefinitionExtended, TriggerAreaComponentDefinitionExtended } from './components/types';
package/dist-cjs/index.js CHANGED
@@ -43,6 +43,7 @@ __exportStar(require("./systems/assetLoad"), exports);
43
43
  __exportStar(require("./systems/async-task"), exports);
44
44
  __exportStar(require("./systems/tween"), exports);
45
45
  __exportStar(require("./systems/triggerArea"), exports);
46
+ __exportStar(require("./systems/physics"), exports);
46
47
  __exportStar(require("./engine/entity"), exports);
47
48
  __exportStar(require("./components/types"), exports);
48
49
  // @internal
@@ -1,3 +1,4 @@
1
1
  export * from './coordinates';
2
2
  export * from './tree';
3
+ export * from './vectors';
3
4
  export { createTimers, Timers, TimerId, TimerCallback } from './timers';
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.createTimers = void 0;
18
18
  __exportStar(require("./coordinates"), exports);
19
19
  __exportStar(require("./tree"), exports);
20
+ __exportStar(require("./vectors"), exports);
20
21
  var timers_1 = require("./timers");
21
22
  Object.defineProperty(exports, "createTimers", { enumerable: true, get: function () { return timers_1.createTimers; } });
@@ -2,6 +2,12 @@ import { Entity } from '../../engine/entity';
2
2
  import { ComponentDefinition, IEngine } from '../../engine';
3
3
  import { Vector3Type } from '../../schemas/custom/Vector3';
4
4
  import { QuaternionType } from '../../schemas/custom/Quaternion';
5
+ /**
6
+ * @public
7
+ * Rotate a vector by a quaternion
8
+ * Uses the formula: v' = q * v * q^(-1), optimized version
9
+ */
10
+ export declare function rotateVectorByQuaternion(v: Vector3Type, q: QuaternionType): Vector3Type;
5
11
  /**
6
12
  * Get an iterator of entities that follow a tree structure for a component
7
13
  * @public
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getWorldRotation = exports.getWorldPosition = exports.getEntitiesWithParent = exports.removeEntityWithChildren = exports.getComponentEntityTree = void 0;
26
+ exports.getWorldRotation = exports.getWorldPosition = exports.getEntitiesWithParent = exports.removeEntityWithChildren = exports.getComponentEntityTree = exports.rotateVectorByQuaternion = void 0;
27
27
  const components = __importStar(require("../../components"));
28
28
  /**
29
29
  * @internal
@@ -61,7 +61,7 @@ function multiplyQuaternions(q1, q2) {
61
61
  };
62
62
  }
63
63
  /**
64
- * @internal
64
+ * @public
65
65
  * Rotate a vector by a quaternion
66
66
  * Uses the formula: v' = q * v * q^(-1), optimized version
67
67
  */
@@ -80,6 +80,7 @@ function rotateVectorByQuaternion(v, q) {
80
80
  z: iz * qw + iw * -qz + ix * -qy - iy * -qx
81
81
  };
82
82
  }
83
+ exports.rotateVectorByQuaternion = rotateVectorByQuaternion;
83
84
  /** @internal Identity transform values */
84
85
  const IDENTITY_POSITION = { x: 0, y: 0, z: 0 };
85
86
  const IDENTITY_ROTATION = { x: 0, y: 0, z: 0, w: 1 };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Vector3 = void 0;
4
+ /**
5
+ * Lightweight Vector3 math utilities for internal use.
6
+ * Mirrors the subset of @dcl/ecs-math Vector3 API used by the physics helpers.
7
+ *
8
+ * TEMPORARY WORKAROUND: @dcl/ecs-math ships ESM-only, which breaks the dist-cjs build.
9
+ * The proper fix is to add a CJS build to @dcl/ecs-math upstream, then replace this
10
+ * file with `import { Vector3 } from '@dcl/ecs-math'`.
11
+ *
12
+ * @internal
13
+ */
14
+ exports.Vector3 = {
15
+ add(a, b) {
16
+ return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
17
+ },
18
+ subtract(a, b) {
19
+ return { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
20
+ },
21
+ scale(v, s) {
22
+ return { x: v.x * s, y: v.y * s, z: v.z * s };
23
+ },
24
+ length(v) {
25
+ return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
26
+ },
27
+ normalize(v) {
28
+ const len = exports.Vector3.length(v);
29
+ if (len === 0)
30
+ return { x: 0, y: 0, z: 0 };
31
+ return { x: v.x / len, y: v.y / len, z: v.z / len };
32
+ },
33
+ equals(a, b) {
34
+ return a.x === b.x && a.y === b.y && a.z === b.z;
35
+ },
36
+ equalsToFloats(v, x, y, z) {
37
+ return v.x === x && v.y === y && v.z === z;
38
+ }
39
+ };
@@ -12,6 +12,7 @@ import { AssetLoadLoadingStateSystem } from '../../systems/assetLoad';
12
12
  import { TweenSystem } from '../../systems/tween';
13
13
  import { TriggerAreaEventsSystem } from '../../systems/triggerArea';
14
14
  import { createTimers, Timers } from '../helpers/timers';
15
+ import { PhysicsSystem } from '../../systems/physics';
15
16
  /**
16
17
  * @public
17
18
  * The engine is the part of the scene that sits in the middle and manages all of the other parts.
@@ -77,6 +78,12 @@ export { TriggerAreaEventsSystem };
77
78
  */
78
79
  export declare const timers: Timers;
79
80
  export { Timers, createTimers };
81
+ /**
82
+ * @public
83
+ * Physics helpers for applying impulses and forces to the player.
84
+ */
85
+ export declare const Physics: PhysicsSystem;
86
+ export { PhysicsSystem };
80
87
  /**
81
88
  * @public
82
89
  * Runs an async function
@@ -4,7 +4,7 @@
4
4
  * init and it'll be changing.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.executeTask = exports.createTimers = exports.timers = exports.triggerAreaEventsSystem = exports.tweenSystem = exports.assetLoadLoadingStateSystem = exports.videoEventsSystem = exports.raycastSystem = exports.pointerEventsSystem = exports.inputSystem = exports.engine = void 0;
7
+ exports.executeTask = exports.Physics = exports.createTimers = exports.timers = exports.triggerAreaEventsSystem = exports.tweenSystem = exports.assetLoadLoadingStateSystem = exports.videoEventsSystem = exports.raycastSystem = exports.pointerEventsSystem = exports.inputSystem = exports.engine = void 0;
8
8
  const engine_1 = require("../../engine");
9
9
  const async_task_1 = require("../../systems/async-task");
10
10
  const events_1 = require("../../systems/events");
@@ -18,6 +18,7 @@ const triggerArea_1 = require("../../systems/triggerArea");
18
18
  const timers_1 = require("../helpers/timers");
19
19
  Object.defineProperty(exports, "createTimers", { enumerable: true, get: function () { return timers_1.createTimers; } });
20
20
  const globals_1 = require("../globals");
21
+ const physics_1 = require("../../systems/physics");
21
22
  /**
22
23
  * @public
23
24
  * The engine is the part of the scene that sits in the middle and manages all of the other parts.
@@ -80,6 +81,11 @@ exports.timers = (0, timers_1.createTimers)(exports.engine);
80
81
  (0, globals_1.setGlobalPolyfill)('clearTimeout', exports.timers.clearTimeout);
81
82
  (0, globals_1.setGlobalPolyfill)('setInterval', exports.timers.setInterval);
82
83
  (0, globals_1.setGlobalPolyfill)('clearInterval', exports.timers.clearInterval);
84
+ /**
85
+ * @public
86
+ * Physics helpers for applying impulses and forces to the player.
87
+ */
88
+ exports.Physics = (0, physics_1.createPhysicsSystem)(exports.engine);
83
89
  /**
84
90
  * Adds pointer event collider system only in DEV env
85
91
  */
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.createPhysicsForceHelper = void 0;
27
+ const components = __importStar(require("../components"));
28
+ const helpers_1 = require("../runtime/helpers");
29
+ const physics_impulse_1 = require("./physics-impulse");
30
+ /** @internal */
31
+ function createPhysicsForceHelper(engine) {
32
+ const PhysicsCombinedForce = components.PhysicsCombinedForce(engine);
33
+ const Transform = components.Transform(engine);
34
+ const timers = (0, helpers_1.createTimers)(engine);
35
+ const durationTimers = new Map();
36
+ // Key = source entity producing the force (not the target); all forces target PlayerEntity.
37
+ const forceSources = new Map();
38
+ const repulsionSources = new Map();
39
+ let lastWrittenForceVector = null;
40
+ function recalcForce() {
41
+ if (forceSources.size === 0) {
42
+ if (PhysicsCombinedForce.has(engine.PlayerEntity)) {
43
+ PhysicsCombinedForce.deleteFrom(engine.PlayerEntity);
44
+ }
45
+ lastWrittenForceVector = null;
46
+ return;
47
+ }
48
+ const current = PhysicsCombinedForce.getOrNull(engine.PlayerEntity);
49
+ if (current && lastWrittenForceVector && current.vector) {
50
+ if (!helpers_1.Vector3.equals(current.vector, lastWrittenForceVector)) {
51
+ console.error('PBPhysicsCombinedForce was modified externally.', 'Expected:', lastWrittenForceVector, 'Found:', current.vector, '-- overwriting with local state.');
52
+ }
53
+ }
54
+ let sum = { x: 0, y: 0, z: 0 };
55
+ for (const v of forceSources.values()) {
56
+ sum = helpers_1.Vector3.add(sum, v);
57
+ }
58
+ PhysicsCombinedForce.createOrReplace(engine.PlayerEntity, { vector: sum });
59
+ lastWrittenForceVector = sum;
60
+ }
61
+ function applyForceToPlayer(source, vector, magnitude) {
62
+ let finalVector;
63
+ if (helpers_1.Vector3.equalsToFloats(vector, 0, 0, 0))
64
+ return;
65
+ if (typeof magnitude === 'number') {
66
+ finalVector = helpers_1.Vector3.scale(helpers_1.Vector3.normalize(vector), magnitude);
67
+ }
68
+ else {
69
+ finalVector = vector;
70
+ }
71
+ forceSources.set(source, finalVector);
72
+ recalcForce();
73
+ }
74
+ function removeForceFromPlayer(source) {
75
+ repulsionSources.delete(source);
76
+ const timerId = durationTimers.get(source);
77
+ if (timerId !== undefined) {
78
+ timers.clearTimeout(timerId);
79
+ durationTimers.delete(source);
80
+ }
81
+ if (!forceSources.has(source))
82
+ return;
83
+ forceSources.delete(source);
84
+ recalcForce();
85
+ }
86
+ function scheduleForceDuration(source, seconds) {
87
+ const existing = durationTimers.get(source);
88
+ if (existing !== undefined) {
89
+ timers.clearTimeout(existing);
90
+ }
91
+ const timerId = timers.setTimeout(() => {
92
+ durationTimers.delete(source);
93
+ removeForceFromPlayer(source);
94
+ }, seconds * 1000);
95
+ durationTimers.set(source, timerId);
96
+ }
97
+ function applyForceToPlayerForDuration(source, duration, vector, magnitude) {
98
+ applyForceToPlayer(source, vector, magnitude);
99
+ scheduleForceDuration(source, duration);
100
+ }
101
+ function computeRepulsionVector(fromPosition, magnitude, radius, falloff) {
102
+ const diff = helpers_1.Vector3.subtract(Transform.get(engine.PlayerEntity).position, fromPosition);
103
+ if (helpers_1.Vector3.equalsToFloats(diff, 0, 0, 0))
104
+ return { x: 0, y: magnitude, z: 0 };
105
+ // Fast path: default params — no need to compute distance
106
+ if (radius === Infinity && falloff === physics_impulse_1.KnockbackFalloff.CONSTANT) {
107
+ return helpers_1.Vector3.scale(helpers_1.Vector3.normalize(diff), magnitude);
108
+ }
109
+ const distance = helpers_1.Vector3.length(diff);
110
+ if (distance > radius)
111
+ return null;
112
+ let effectiveMagnitude;
113
+ switch (falloff) {
114
+ case physics_impulse_1.KnockbackFalloff.LINEAR:
115
+ effectiveMagnitude = magnitude * (1 - distance / radius);
116
+ break;
117
+ case physics_impulse_1.KnockbackFalloff.INVERSE_SQUARE:
118
+ effectiveMagnitude = magnitude / (distance * distance + 1);
119
+ break;
120
+ case physics_impulse_1.KnockbackFalloff.CONSTANT:
121
+ default:
122
+ effectiveMagnitude = magnitude;
123
+ break;
124
+ }
125
+ if (effectiveMagnitude === 0)
126
+ return null;
127
+ // normalize(diff) * effectiveMagnitude in one step
128
+ return helpers_1.Vector3.scale(diff, effectiveMagnitude / distance);
129
+ }
130
+ function applyRepulsionForceToPlayer(source, fromPosition, magnitude, radius = Infinity, falloff = physics_impulse_1.KnockbackFalloff.CONSTANT) {
131
+ repulsionSources.set(source, { fromPosition, magnitude, radius, falloff });
132
+ const vector = computeRepulsionVector(fromPosition, magnitude, radius, falloff);
133
+ if (vector) {
134
+ forceSources.set(source, vector);
135
+ }
136
+ else {
137
+ forceSources.delete(source);
138
+ }
139
+ recalcForce();
140
+ }
141
+ // Background system: recalculate repulsion vectors and clean up stale forces every tick.
142
+ // Stale forces can appear when CRDT sync from another client writes PhysicsCombinedForce
143
+ // externally (entity-1 ambiguity: each client interprets entity 1 as its own player).
144
+ engine.addSystem(() => {
145
+ // Repulsion forces need per-tick direction recalculation as the player moves
146
+ if (repulsionSources.size > 0) {
147
+ for (const [source, { fromPosition, magnitude, radius, falloff }] of repulsionSources) {
148
+ const vector = computeRepulsionVector(fromPosition, magnitude, radius, falloff);
149
+ if (vector) {
150
+ forceSources.set(source, vector);
151
+ }
152
+ else {
153
+ forceSources.delete(source);
154
+ }
155
+ }
156
+ recalcForce();
157
+ return;
158
+ }
159
+ // No local sources — clean up any externally-created component (e.g. from CRDT sync)
160
+ if (forceSources.size === 0 && PhysicsCombinedForce.has(engine.PlayerEntity)) {
161
+ PhysicsCombinedForce.deleteFrom(engine.PlayerEntity);
162
+ lastWrittenForceVector = null;
163
+ }
164
+ });
165
+ return { applyForceToPlayer, removeForceFromPlayer, applyForceToPlayerForDuration, applyRepulsionForceToPlayer };
166
+ }
167
+ exports.createPhysicsForceHelper = createPhysicsForceHelper;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @public
3
+ * Falloff mode for knockback force over distance.
4
+ */
5
+ export declare enum KnockbackFalloff {
6
+ /** Same force at any distance within radius */
7
+ CONSTANT = 0,
8
+ /** Smooth linear decrease to 0 at radius edge: F = magnitude * (1 - distance / radius) */
9
+ LINEAR = 1,
10
+ /** Sharp drop-off, physically realistic: F = magnitude / (distance^2 + 1) */
11
+ INVERSE_SQUARE = 2
12
+ }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.createPhysicsImpulseHelper = exports.KnockbackFalloff = void 0;
27
+ const components = __importStar(require("../components"));
28
+ const helpers_1 = require("../runtime/helpers");
29
+ /**
30
+ * @public
31
+ * Falloff mode for knockback force over distance.
32
+ */
33
+ var KnockbackFalloff;
34
+ (function (KnockbackFalloff) {
35
+ /** Same force at any distance within radius */
36
+ KnockbackFalloff[KnockbackFalloff["CONSTANT"] = 0] = "CONSTANT";
37
+ /** Smooth linear decrease to 0 at radius edge: F = magnitude * (1 - distance / radius) */
38
+ KnockbackFalloff[KnockbackFalloff["LINEAR"] = 1] = "LINEAR";
39
+ /** Sharp drop-off, physically realistic: F = magnitude / (distance^2 + 1) */
40
+ KnockbackFalloff[KnockbackFalloff["INVERSE_SQUARE"] = 2] = "INVERSE_SQUARE";
41
+ })(KnockbackFalloff = exports.KnockbackFalloff || (exports.KnockbackFalloff = {}));
42
+ /** @internal */
43
+ function createPhysicsImpulseHelper(engine) {
44
+ const PhysicsCombinedImpulse = components.PhysicsCombinedImpulse(engine);
45
+ const Transform = components.Transform(engine);
46
+ const EngineInfo = components.EngineInfo(engine);
47
+ let impulseEventId = 0;
48
+ let lastWrittenEventId = 0;
49
+ let lastWrittenTick = -1;
50
+ function applyImpulseToPlayer(vector, magnitude) {
51
+ let finalVector;
52
+ if (typeof magnitude === 'number') {
53
+ if (helpers_1.Vector3.equalsToFloats(vector, 0, 0, 0))
54
+ return;
55
+ finalVector = helpers_1.Vector3.scale(helpers_1.Vector3.normalize(vector), magnitude);
56
+ }
57
+ else {
58
+ if (helpers_1.Vector3.equalsToFloats(vector, 0, 0, 0))
59
+ return;
60
+ finalVector = vector;
61
+ }
62
+ const currentTick = EngineInfo.getOrNull(engine.RootEntity)?.tickNumber ?? 0;
63
+ const existing = PhysicsCombinedImpulse.getOrNull(engine.PlayerEntity);
64
+ if (existing && existing.eventId !== lastWrittenEventId && lastWrittenEventId !== 0) {
65
+ throw new Error('PBPhysicsCombinedImpulse was modified outside Physics helper. ' +
66
+ 'Do not mix direct component access with Physics.applyImpulseToPlayer().');
67
+ }
68
+ if (lastWrittenTick === currentTick && existing) {
69
+ finalVector = helpers_1.Vector3.add(existing.vector ?? { x: 0, y: 0, z: 0 }, finalVector);
70
+ }
71
+ else {
72
+ lastWrittenEventId = ++impulseEventId;
73
+ }
74
+ lastWrittenTick = currentTick;
75
+ PhysicsCombinedImpulse.createOrReplace(engine.PlayerEntity, {
76
+ vector: finalVector,
77
+ eventId: lastWrittenEventId
78
+ });
79
+ }
80
+ function applyKnockbackToPlayer(fromPosition, magnitude, radius = Infinity, falloff = KnockbackFalloff.CONSTANT) {
81
+ const diff = helpers_1.Vector3.subtract(Transform.get(engine.PlayerEntity).position, fromPosition);
82
+ if (helpers_1.Vector3.equalsToFloats(diff, 0, 0, 0)) {
83
+ applyImpulseToPlayer({ x: 0, y: magnitude, z: 0 });
84
+ return;
85
+ }
86
+ // Fast path: default params — no need to compute distance
87
+ if (radius === Infinity && falloff === KnockbackFalloff.CONSTANT) {
88
+ applyImpulseToPlayer(helpers_1.Vector3.scale(helpers_1.Vector3.normalize(diff), magnitude));
89
+ return;
90
+ }
91
+ const distance = helpers_1.Vector3.length(diff);
92
+ if (distance > radius)
93
+ return;
94
+ let effectiveMagnitude;
95
+ switch (falloff) {
96
+ case KnockbackFalloff.LINEAR:
97
+ effectiveMagnitude = magnitude * (1 - distance / radius);
98
+ break;
99
+ case KnockbackFalloff.INVERSE_SQUARE:
100
+ effectiveMagnitude = magnitude / (distance * distance + 1);
101
+ break;
102
+ case KnockbackFalloff.CONSTANT:
103
+ default:
104
+ effectiveMagnitude = magnitude;
105
+ break;
106
+ }
107
+ // normalize(diff) * effectiveMagnitude in one step
108
+ applyImpulseToPlayer(helpers_1.Vector3.scale(diff, effectiveMagnitude / distance));
109
+ }
110
+ return { applyImpulseToPlayer, applyKnockbackToPlayer };
111
+ }
112
+ exports.createPhysicsImpulseHelper = createPhysicsImpulseHelper;
@@ -0,0 +1,77 @@
1
+ import { Entity } from '../engine';
2
+ import { Vector3Type } from '../schemas';
3
+ import { KnockbackFalloff } from './physics-impulse';
4
+ export { KnockbackFalloff } from './physics-impulse';
5
+ /**
6
+ * @public
7
+ */
8
+ export interface PhysicsSystem {
9
+ /**
10
+ * Apply a one-shot impulse to the player entity.
11
+ * Multiple calls within the same frame are accumulated (summed).
12
+ *
13
+ * @param vector - a single `vector` whose length encodes the strength.
14
+ * or use overload for `direction` with a separate `magnitude` — the direction will be normalized before scaling.
15
+ */
16
+ applyImpulseToPlayer(vector: Vector3Type): void;
17
+ applyImpulseToPlayer(direction: Vector3Type, magnitude: number): void;
18
+ /**
19
+ * Apply a continuous force to the player from a given source entity.
20
+ * Multiple sources are accumulated: the registry sums all active forces
21
+ * and writes a single PBPhysicsCombinedForce component.
22
+ * Calling again with the same source replaces its previous force.
23
+ *
24
+ * @param source - Entity key identifying this force source
25
+ * @param vector - single `vector` whose length encodes the strength
26
+ * or use overload for `direction` with a separate `magnitude` — the direction will be normalized before scaling.
27
+ */
28
+ applyForceToPlayer(source: Entity, vector: Vector3Type): void;
29
+ applyForceToPlayer(source: Entity, direction: Vector3Type, magnitude: number): void;
30
+ /**
31
+ * Remove a continuous force from the player. Remaining sources are
32
+ * re-summed; if none remain the force is cleared. No-op if the source
33
+ * is not registered.
34
+ *
35
+ * @param source - Entity key identifying the force source to remove
36
+ */
37
+ removeForceFromPlayer(source: Entity): void;
38
+ /**
39
+ * Push the player away from a point. Computes direction from
40
+ * `fromPosition` to the player, applies falloff, and delegates
41
+ * to applyImpulseToPlayer.
42
+ *
43
+ * If the player is exactly at `fromPosition`, pushes upward.
44
+ *
45
+ * @param fromPosition - world-space origin of the knockback (explosion center, enemy position, etc.)
46
+ * @param magnitude - base impulse strength
47
+ * @param radius - max distance of effect (default: Infinity)
48
+ * @param falloff - how force decreases with distance (default: CONSTANT)
49
+ */
50
+ applyKnockbackToPlayer(fromPosition: Vector3Type, magnitude: number, radius?: number, falloff?: KnockbackFalloff): void;
51
+ /**
52
+ * Apply a continuous force to the player for a limited duration.
53
+ * After `duration` seconds the force is automatically removed.
54
+ * Calling again with the same source resets the timer.
55
+ *
56
+ * @param source - Entity key identifying this force source
57
+ * @param duration - how long the force lasts, in seconds
58
+ * @param vector - single `vector` whose length encodes the strength
59
+ * or use overload for `direction` with a separate `magnitude` — the direction will be normalized before scaling.
60
+ */
61
+ applyForceToPlayerForDuration(source: Entity, duration: number, vector: Vector3Type): void;
62
+ applyForceToPlayerForDuration(source: Entity, duration: number, direction: Vector3Type, magnitude: number): void;
63
+ /**
64
+ * Apply a continuous repulsion force that pushes the player away from a point,
65
+ * recalculating direction every tick as the player moves.
66
+ * Remove with `removeForceFromPlayer(source)`.
67
+ *
68
+ * Negative magnitude = attraction (pulls toward the source).
69
+ *
70
+ * @param source - Entity key identifying this force source
71
+ * @param fromPosition - world-space origin of repulsion
72
+ * @param magnitude - base force strength
73
+ * @param radius - max distance of effect (default: Infinity)
74
+ * @param falloff - how force decreases with distance (default: CONSTANT)
75
+ */
76
+ applyRepulsionForceToPlayer(source: Entity, fromPosition: Vector3Type, magnitude: number, radius?: number, falloff?: KnockbackFalloff): void;
77
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPhysicsSystem = exports.KnockbackFalloff = void 0;
4
+ const physics_impulse_1 = require("./physics-impulse");
5
+ const physics_force_1 = require("./physics-force");
6
+ var physics_impulse_2 = require("./physics-impulse");
7
+ Object.defineProperty(exports, "KnockbackFalloff", { enumerable: true, get: function () { return physics_impulse_2.KnockbackFalloff; } });
8
+ /**
9
+ * @internal
10
+ */
11
+ function createPhysicsSystem(engine) {
12
+ const impulse = (0, physics_impulse_1.createPhysicsImpulseHelper)(engine);
13
+ const force = (0, physics_force_1.createPhysicsForceHelper)(engine);
14
+ return {
15
+ applyImpulseToPlayer: impulse.applyImpulseToPlayer,
16
+ applyForceToPlayer: force.applyForceToPlayer,
17
+ removeForceFromPlayer: force.removeForceFromPlayer,
18
+ applyKnockbackToPlayer: impulse.applyKnockbackToPlayer,
19
+ applyForceToPlayerForDuration: force.applyForceToPlayerForDuration,
20
+ applyRepulsionForceToPlayer: force.applyRepulsionForceToPlayer
21
+ };
22
+ }
23
+ exports.createPhysicsSystem = createPhysicsSystem;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dcl/ecs",
3
3
  "description": "Decentraland ECS",
4
- "version": "7.21.1-23203004012.commit-7c64ac2",
4
+ "version": "7.21.1-23252789519.commit-9f806e1",
5
5
  "author": "DCL",
6
6
  "bugs": "https://github.com/decentraland/ecs/issues",
7
7
  "files": [
@@ -31,7 +31,8 @@
31
31
  "displayName": "ECS",
32
32
  "tsconfig": "./tsconfig.json"
33
33
  },
34
+ "dependencies": {},
34
35
  "types": "./dist/index.d.ts",
35
36
  "typings": "./dist/index.d.ts",
36
- "commit": "7c64ac29ee3f34e79019b4435854537a261721ab"
37
+ "commit": "9f806e11cc45f8449a651c31e78123680a8488d8"
37
38
  }