isometric-physics 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Carson Fujita
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ getCardinalLabel: () => getCardinalLabel,
24
+ getToScreen: () => getToScreen,
25
+ isoTransform: () => isoTransform,
26
+ stepVesselPosition: () => stepVesselPosition,
27
+ vec2Add: () => vec2Add,
28
+ vec2Dot: () => vec2Dot,
29
+ vec2Length: () => vec2Length,
30
+ vec2Normalize: () => vec2Normalize,
31
+ vec2Scale: () => vec2Scale,
32
+ vec2Sub: () => vec2Sub,
33
+ vec3Add: () => vec3Add,
34
+ vec3Cross: () => vec3Cross,
35
+ vec3Dot: () => vec3Dot,
36
+ vec3Length: () => vec3Length,
37
+ vec3Normalize: () => vec3Normalize,
38
+ vec3Scale: () => vec3Scale,
39
+ vec3Sub: () => vec3Sub
40
+ });
41
+ module.exports = __toCommonJS(index_exports);
42
+
43
+ // src/isometric.ts
44
+ var ISO_ANGLE = Math.PI / 6;
45
+ var COS_30 = Math.cos(ISO_ANGLE);
46
+ var SIN_30 = Math.sin(ISO_ANGLE);
47
+ var isoTransform = (x, y, z = 0) => ({
48
+ x: (x - y) * COS_30,
49
+ y: (x + y) * SIN_30 - z
50
+ });
51
+ var getToScreen = (centerX, centerY, scale = 1) => (x, y, z) => {
52
+ const iso = isoTransform(x * scale, y * scale, z * scale);
53
+ return { x: centerX + iso.x, y: centerY + iso.y };
54
+ };
55
+ var getCardinalLabel = (heading) => {
56
+ const directions = [
57
+ { min: 337.5, max: 360, label: "N" },
58
+ { min: 0, max: 22.5, label: "N" },
59
+ { min: 22.5, max: 67.5, label: "NE" },
60
+ { min: 67.5, max: 112.5, label: "E" },
61
+ { min: 112.5, max: 157.5, label: "SE" },
62
+ { min: 157.5, max: 202.5, label: "S" },
63
+ { min: 202.5, max: 247.5, label: "SW" },
64
+ { min: 247.5, max: 292.5, label: "W" },
65
+ { min: 292.5, max: 337.5, label: "NW" }
66
+ ];
67
+ for (const dir of directions) {
68
+ if (heading >= dir.min && heading < dir.max) return dir.label;
69
+ }
70
+ return "N";
71
+ };
72
+
73
+ // src/vectors.ts
74
+ var vec2Add = (a, b) => ({ x: a.x + b.x, y: a.y + b.y });
75
+ var vec2Sub = (a, b) => ({ x: a.x - b.x, y: a.y - b.y });
76
+ var vec2Scale = (v, s) => ({ x: v.x * s, y: v.y * s });
77
+ var vec2Length = (v) => Math.sqrt(v.x ** 2 + v.y ** 2);
78
+ var vec2Normalize = (v) => {
79
+ const l = vec2Length(v) || 1;
80
+ return vec2Scale(v, 1 / l);
81
+ };
82
+ var vec2Dot = (a, b) => a.x * b.x + a.y * b.y;
83
+ var vec3Add = (a, b) => ({ x: a.x + b.x, y: a.y + b.y, z: a.z + b.z });
84
+ var vec3Sub = (a, b) => ({ x: a.x - b.x, y: a.y - b.y, z: a.z - b.z });
85
+ var vec3Scale = (v, s) => ({ x: v.x * s, y: v.y * s, z: v.z * s });
86
+ var vec3Length = (v) => Math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2);
87
+ var vec3Normalize = (v) => {
88
+ const l = vec3Length(v) || 1;
89
+ return vec3Scale(v, 1 / l);
90
+ };
91
+ var vec3Dot = (a, b) => a.x * b.x + a.y * b.y + a.z * b.z;
92
+ var vec3Cross = (a, b) => ({
93
+ x: a.y * b.z - a.z * b.y,
94
+ y: a.z * b.x - a.x * b.z,
95
+ z: a.x * b.y - a.y * b.x
96
+ });
97
+
98
+ // src/physics.ts
99
+ var stepVesselPosition = (pos, velocity, rotationDeg, deltaTime, gridSize) => {
100
+ if (velocity === 0) return pos;
101
+ const limit = gridSize / 2;
102
+ const rad = -rotationDeg * Math.PI / 180;
103
+ const dx = -velocity * Math.sin(rad) * deltaTime;
104
+ const dy = -velocity * Math.cos(rad) * deltaTime;
105
+ let newX = pos.x + dx;
106
+ let newY = pos.y + dy;
107
+ if (newX > limit) newX -= gridSize;
108
+ if (newX < -limit) newX += gridSize;
109
+ if (newY > limit) newY -= gridSize;
110
+ if (newY < -limit) newY += gridSize;
111
+ return { x: newX, y: newY };
112
+ };
113
+ // Annotate the CommonJS export names for ESM import in node:
114
+ 0 && (module.exports = {
115
+ getCardinalLabel,
116
+ getToScreen,
117
+ isoTransform,
118
+ stepVesselPosition,
119
+ vec2Add,
120
+ vec2Dot,
121
+ vec2Length,
122
+ vec2Normalize,
123
+ vec2Scale,
124
+ vec2Sub,
125
+ vec3Add,
126
+ vec3Cross,
127
+ vec3Dot,
128
+ vec3Length,
129
+ vec3Normalize,
130
+ vec3Scale,
131
+ vec3Sub
132
+ });
133
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/isometric.ts","../src/vectors.ts","../src/physics.ts"],"sourcesContent":["export * from './isometric.js';\nexport * from './vectors.js';\nexport * from './physics.js';\n","/**\n * @file isometric.ts\n * @description Isometric projection math: standard 2:1 dimetric projection (X right-down, Y left-down, Z up).\n * @version 1.0.0\n */\n\nexport type Point = { x: number; y: number };\n\nconst ISO_ANGLE = Math.PI / 6;\nconst COS_30 = Math.cos(ISO_ANGLE);\nconst SIN_30 = Math.sin(ISO_ANGLE);\n\n/**\n * Standard isometric projection (X right-down, Y left-down, Z up).\n * @version 1.0.0\n */\nexport const isoTransform = (x: number, y: number, z = 0): Point => ({\n x: (x - y) * COS_30,\n y: (x + y) * SIN_30 - z,\n});\n\n/**\n * Creates a projection function bound to a specific canvas origin and scale.\n */\nexport const getToScreen = (centerX: number, centerY: number, scale = 1) =>\n (x: number, y: number, z: number): Point => {\n const iso = isoTransform(x * scale, y * scale, z * scale);\n return { x: centerX + iso.x, y: centerY + iso.y };\n };\n\n/**\n * Returns a cardinal direction label for a heading in degrees (0–360).\n */\nexport const getCardinalLabel = (heading: number): string => {\n const directions = [\n { min: 337.5, max: 360, label: 'N' },\n { min: 0, max: 22.5, label: 'N' },\n { min: 22.5, max: 67.5, label: 'NE' },\n { min: 67.5, max: 112.5, label: 'E' },\n { min: 112.5, max: 157.5, label: 'SE' },\n { min: 157.5, max: 202.5, label: 'S' },\n { min: 202.5, max: 247.5, label: 'SW' },\n { min: 247.5, max: 292.5, label: 'W' },\n { min: 292.5, max: 337.5, label: 'NW' },\n ];\n for (const dir of directions) {\n if (heading >= dir.min && heading < dir.max) return dir.label;\n }\n return 'N';\n};\n","/**\n * @file vectors.ts\n * @description Lightweight 2D and 3D vector types and operations.\n * @version 1.0.0\n */\n\nexport type Vector2 = { x: number; y: number };\nexport type Vector3 = { x: number; y: number; z: number };\n\nexport const vec2Add = (a: Vector2, b: Vector2): Vector2 => ({ x: a.x + b.x, y: a.y + b.y });\nexport const vec2Sub = (a: Vector2, b: Vector2): Vector2 => ({ x: a.x - b.x, y: a.y - b.y });\nexport const vec2Scale = (v: Vector2, s: number): Vector2 => ({ x: v.x * s, y: v.y * s });\nexport const vec2Length = (v: Vector2): number => Math.sqrt(v.x ** 2 + v.y ** 2);\nexport const vec2Normalize = (v: Vector2): Vector2 => {\n const l = vec2Length(v) || 1;\n return vec2Scale(v, 1 / l);\n};\nexport const vec2Dot = (a: Vector2, b: Vector2): number => a.x * b.x + a.y * b.y;\n\nexport const vec3Add = (a: Vector3, b: Vector3): Vector3 => ({ x: a.x + b.x, y: a.y + b.y, z: a.z + b.z });\nexport const vec3Sub = (a: Vector3, b: Vector3): Vector3 => ({ x: a.x - b.x, y: a.y - b.y, z: a.z - b.z });\nexport const vec3Scale = (v: Vector3, s: number): Vector3 => ({ x: v.x * s, y: v.y * s, z: v.z * s });\nexport const vec3Length = (v: Vector3): number => Math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2);\nexport const vec3Normalize = (v: Vector3): Vector3 => {\n const l = vec3Length(v) || 1;\n return vec3Scale(v, 1 / l);\n};\nexport const vec3Dot = (a: Vector3, b: Vector3): number => a.x * b.x + a.y * b.y + a.z * b.z;\nexport const vec3Cross = (a: Vector3, b: Vector3): Vector3 => ({\n x: a.y * b.z - a.z * b.y,\n y: a.z * b.x - a.x * b.z,\n z: a.x * b.y - a.y * b.x,\n});\n","/**\n * @file physics.ts\n * @description Pure vessel-position integration step, detached from React. Given a current\n * position, velocity, and heading, advances the position by one timestep and wraps it around\n * a square world of the given size.\n * @version 1.0.0\n */\nimport type { Point } from './isometric.js';\n\n/**\n * Advances a vessel's position by one physics step.\n * @param pos - Current { x, y } position in world units.\n * @param velocity - Current velocity in world units per second.\n * @param rotationDeg - Current heading in degrees (0 = north, 90 = east).\n * @param deltaTime - Elapsed time since the last step, in seconds.\n * @param gridSize - Total span of the (square) world; position wraps at ±gridSize/2.\n * @returns The new { x, y } position, wrapped to stay within the world bounds.\n * @version 1.0.0\n */\nexport const stepVesselPosition = (\n pos: Point,\n velocity: number,\n rotationDeg: number,\n deltaTime: number,\n gridSize: number,\n): Point => {\n if (velocity === 0) return pos;\n\n const limit = gridSize / 2;\n const rad = (-rotationDeg * Math.PI) / 180;\n const dx = -velocity * Math.sin(rad) * deltaTime;\n const dy = -velocity * Math.cos(rad) * deltaTime;\n\n let newX = pos.x + dx;\n let newY = pos.y + dy;\n if (newX > limit) newX -= gridSize;\n if (newX < -limit) newX += gridSize;\n if (newY > limit) newY -= gridSize;\n if (newY < -limit) newY += gridSize;\n\n return { x: newX, y: newY };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQA,IAAM,YAAY,KAAK,KAAK;AAC5B,IAAM,SAAS,KAAK,IAAI,SAAS;AACjC,IAAM,SAAS,KAAK,IAAI,SAAS;AAM1B,IAAM,eAAe,CAAC,GAAW,GAAW,IAAI,OAAc;AAAA,EACjE,IAAI,IAAI,KAAK;AAAA,EACb,IAAI,IAAI,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AAG,QAAO;AAE3B,QAAM,QAAQ,WAAW;AACzB,QAAM,MAAO,CAAC,cAAc,KAAK,KAAM;AACvC,QAAM,KAAK,CAAC,WAAW,KAAK,IAAI,GAAG,IAAI;AACvC,QAAM,KAAK,CAAC,WAAW,KAAK,IAAI,GAAG,IAAI;AAEvC,MAAI,OAAO,IAAI,IAAI;AACnB,MAAI,OAAO,IAAI,IAAI;AACnB,MAAI,OAAQ,MAAO,SAAQ;AAC3B,MAAI,OAAO,CAAC,MAAO,SAAQ;AAC3B,MAAI,OAAQ,MAAO,SAAQ;AAC3B,MAAI,OAAO,CAAC,MAAO,SAAQ;AAE3B,SAAO,EAAE,GAAG,MAAM,GAAG,KAAK;AAC9B;","names":[]}
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @file isometric.ts
3
+ * @description Isometric projection math: standard 2:1 dimetric projection (X right-down, Y left-down, Z up).
4
+ * @version 1.0.0
5
+ */
6
+ type Point = {
7
+ x: number;
8
+ y: number;
9
+ };
10
+ /**
11
+ * Standard isometric projection (X right-down, Y left-down, Z up).
12
+ * @version 1.0.0
13
+ */
14
+ declare const isoTransform: (x: number, y: number, z?: number) => Point;
15
+ /**
16
+ * Creates a projection function bound to a specific canvas origin and scale.
17
+ */
18
+ declare const getToScreen: (centerX: number, centerY: number, scale?: number) => (x: number, y: number, z: number) => Point;
19
+ /**
20
+ * Returns a cardinal direction label for a heading in degrees (0–360).
21
+ */
22
+ declare const getCardinalLabel: (heading: number) => string;
23
+
24
+ /**
25
+ * @file vectors.ts
26
+ * @description Lightweight 2D and 3D vector types and operations.
27
+ * @version 1.0.0
28
+ */
29
+ type Vector2 = {
30
+ x: number;
31
+ y: number;
32
+ };
33
+ type Vector3 = {
34
+ x: number;
35
+ y: number;
36
+ z: number;
37
+ };
38
+ declare const vec2Add: (a: Vector2, b: Vector2) => Vector2;
39
+ declare const vec2Sub: (a: Vector2, b: Vector2) => Vector2;
40
+ declare const vec2Scale: (v: Vector2, s: number) => Vector2;
41
+ declare const vec2Length: (v: Vector2) => number;
42
+ declare const vec2Normalize: (v: Vector2) => Vector2;
43
+ declare const vec2Dot: (a: Vector2, b: Vector2) => number;
44
+ declare const vec3Add: (a: Vector3, b: Vector3) => Vector3;
45
+ declare const vec3Sub: (a: Vector3, b: Vector3) => Vector3;
46
+ declare const vec3Scale: (v: Vector3, s: number) => Vector3;
47
+ declare const vec3Length: (v: Vector3) => number;
48
+ declare const vec3Normalize: (v: Vector3) => Vector3;
49
+ declare const vec3Dot: (a: Vector3, b: Vector3) => number;
50
+ declare const vec3Cross: (a: Vector3, b: Vector3) => Vector3;
51
+
52
+ /**
53
+ * @file physics.ts
54
+ * @description Pure vessel-position integration step, detached from React. Given a current
55
+ * position, velocity, and heading, advances the position by one timestep and wraps it around
56
+ * a square world of the given size.
57
+ * @version 1.0.0
58
+ */
59
+
60
+ /**
61
+ * Advances a vessel's position by one physics step.
62
+ * @param pos - Current { x, y } position in world units.
63
+ * @param velocity - Current velocity in world units per second.
64
+ * @param rotationDeg - Current heading in degrees (0 = north, 90 = east).
65
+ * @param deltaTime - Elapsed time since the last step, in seconds.
66
+ * @param gridSize - Total span of the (square) world; position wraps at ±gridSize/2.
67
+ * @returns The new { x, y } position, wrapped to stay within the world bounds.
68
+ * @version 1.0.0
69
+ */
70
+ declare const stepVesselPosition: (pos: Point, velocity: number, rotationDeg: number, deltaTime: number, gridSize: number) => Point;
71
+
72
+ export { type Point, type Vector2, type Vector3, getCardinalLabel, getToScreen, isoTransform, stepVesselPosition, vec2Add, vec2Dot, vec2Length, vec2Normalize, vec2Scale, vec2Sub, vec3Add, vec3Cross, vec3Dot, vec3Length, vec3Normalize, vec3Scale, vec3Sub };
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @file isometric.ts
3
+ * @description Isometric projection math: standard 2:1 dimetric projection (X right-down, Y left-down, Z up).
4
+ * @version 1.0.0
5
+ */
6
+ type Point = {
7
+ x: number;
8
+ y: number;
9
+ };
10
+ /**
11
+ * Standard isometric projection (X right-down, Y left-down, Z up).
12
+ * @version 1.0.0
13
+ */
14
+ declare const isoTransform: (x: number, y: number, z?: number) => Point;
15
+ /**
16
+ * Creates a projection function bound to a specific canvas origin and scale.
17
+ */
18
+ declare const getToScreen: (centerX: number, centerY: number, scale?: number) => (x: number, y: number, z: number) => Point;
19
+ /**
20
+ * Returns a cardinal direction label for a heading in degrees (0–360).
21
+ */
22
+ declare const getCardinalLabel: (heading: number) => string;
23
+
24
+ /**
25
+ * @file vectors.ts
26
+ * @description Lightweight 2D and 3D vector types and operations.
27
+ * @version 1.0.0
28
+ */
29
+ type Vector2 = {
30
+ x: number;
31
+ y: number;
32
+ };
33
+ type Vector3 = {
34
+ x: number;
35
+ y: number;
36
+ z: number;
37
+ };
38
+ declare const vec2Add: (a: Vector2, b: Vector2) => Vector2;
39
+ declare const vec2Sub: (a: Vector2, b: Vector2) => Vector2;
40
+ declare const vec2Scale: (v: Vector2, s: number) => Vector2;
41
+ declare const vec2Length: (v: Vector2) => number;
42
+ declare const vec2Normalize: (v: Vector2) => Vector2;
43
+ declare const vec2Dot: (a: Vector2, b: Vector2) => number;
44
+ declare const vec3Add: (a: Vector3, b: Vector3) => Vector3;
45
+ declare const vec3Sub: (a: Vector3, b: Vector3) => Vector3;
46
+ declare const vec3Scale: (v: Vector3, s: number) => Vector3;
47
+ declare const vec3Length: (v: Vector3) => number;
48
+ declare const vec3Normalize: (v: Vector3) => Vector3;
49
+ declare const vec3Dot: (a: Vector3, b: Vector3) => number;
50
+ declare const vec3Cross: (a: Vector3, b: Vector3) => Vector3;
51
+
52
+ /**
53
+ * @file physics.ts
54
+ * @description Pure vessel-position integration step, detached from React. Given a current
55
+ * position, velocity, and heading, advances the position by one timestep and wraps it around
56
+ * a square world of the given size.
57
+ * @version 1.0.0
58
+ */
59
+
60
+ /**
61
+ * Advances a vessel's position by one physics step.
62
+ * @param pos - Current { x, y } position in world units.
63
+ * @param velocity - Current velocity in world units per second.
64
+ * @param rotationDeg - Current heading in degrees (0 = north, 90 = east).
65
+ * @param deltaTime - Elapsed time since the last step, in seconds.
66
+ * @param gridSize - Total span of the (square) world; position wraps at ±gridSize/2.
67
+ * @returns The new { x, y } position, wrapped to stay within the world bounds.
68
+ * @version 1.0.0
69
+ */
70
+ declare const stepVesselPosition: (pos: Point, velocity: number, rotationDeg: number, deltaTime: number, gridSize: number) => Point;
71
+
72
+ export { type Point, type Vector2, type Vector3, getCardinalLabel, getToScreen, isoTransform, stepVesselPosition, vec2Add, vec2Dot, vec2Length, vec2Normalize, vec2Scale, vec2Sub, vec3Add, vec3Cross, vec3Dot, vec3Length, vec3Normalize, vec3Scale, vec3Sub };
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ // src/isometric.ts
2
+ var ISO_ANGLE = Math.PI / 6;
3
+ var COS_30 = Math.cos(ISO_ANGLE);
4
+ var SIN_30 = Math.sin(ISO_ANGLE);
5
+ var isoTransform = (x, y, z = 0) => ({
6
+ x: (x - y) * COS_30,
7
+ y: (x + y) * SIN_30 - z
8
+ });
9
+ var getToScreen = (centerX, centerY, scale = 1) => (x, y, z) => {
10
+ const iso = isoTransform(x * scale, y * scale, z * scale);
11
+ return { x: centerX + iso.x, y: centerY + iso.y };
12
+ };
13
+ var getCardinalLabel = (heading) => {
14
+ const directions = [
15
+ { min: 337.5, max: 360, label: "N" },
16
+ { min: 0, max: 22.5, label: "N" },
17
+ { min: 22.5, max: 67.5, label: "NE" },
18
+ { min: 67.5, max: 112.5, label: "E" },
19
+ { min: 112.5, max: 157.5, label: "SE" },
20
+ { min: 157.5, max: 202.5, label: "S" },
21
+ { min: 202.5, max: 247.5, label: "SW" },
22
+ { min: 247.5, max: 292.5, label: "W" },
23
+ { min: 292.5, max: 337.5, label: "NW" }
24
+ ];
25
+ for (const dir of directions) {
26
+ if (heading >= dir.min && heading < dir.max) return dir.label;
27
+ }
28
+ return "N";
29
+ };
30
+
31
+ // src/vectors.ts
32
+ var vec2Add = (a, b) => ({ x: a.x + b.x, y: a.y + b.y });
33
+ var vec2Sub = (a, b) => ({ x: a.x - b.x, y: a.y - b.y });
34
+ var vec2Scale = (v, s) => ({ x: v.x * s, y: v.y * s });
35
+ var vec2Length = (v) => Math.sqrt(v.x ** 2 + v.y ** 2);
36
+ var vec2Normalize = (v) => {
37
+ const l = vec2Length(v) || 1;
38
+ return vec2Scale(v, 1 / l);
39
+ };
40
+ var vec2Dot = (a, b) => a.x * b.x + a.y * b.y;
41
+ var vec3Add = (a, b) => ({ x: a.x + b.x, y: a.y + b.y, z: a.z + b.z });
42
+ var vec3Sub = (a, b) => ({ x: a.x - b.x, y: a.y - b.y, z: a.z - b.z });
43
+ var vec3Scale = (v, s) => ({ x: v.x * s, y: v.y * s, z: v.z * s });
44
+ var vec3Length = (v) => Math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2);
45
+ var vec3Normalize = (v) => {
46
+ const l = vec3Length(v) || 1;
47
+ return vec3Scale(v, 1 / l);
48
+ };
49
+ var vec3Dot = (a, b) => a.x * b.x + a.y * b.y + a.z * b.z;
50
+ var vec3Cross = (a, b) => ({
51
+ x: a.y * b.z - a.z * b.y,
52
+ y: a.z * b.x - a.x * b.z,
53
+ z: a.x * b.y - a.y * b.x
54
+ });
55
+
56
+ // src/physics.ts
57
+ var stepVesselPosition = (pos, velocity, rotationDeg, deltaTime, gridSize) => {
58
+ if (velocity === 0) return pos;
59
+ const limit = gridSize / 2;
60
+ const rad = -rotationDeg * Math.PI / 180;
61
+ const dx = -velocity * Math.sin(rad) * deltaTime;
62
+ const dy = -velocity * Math.cos(rad) * deltaTime;
63
+ let newX = pos.x + dx;
64
+ let newY = pos.y + dy;
65
+ if (newX > limit) newX -= gridSize;
66
+ if (newX < -limit) newX += gridSize;
67
+ if (newY > limit) newY -= gridSize;
68
+ if (newY < -limit) newY += gridSize;
69
+ return { x: newX, y: newY };
70
+ };
71
+ export {
72
+ getCardinalLabel,
73
+ getToScreen,
74
+ isoTransform,
75
+ stepVesselPosition,
76
+ vec2Add,
77
+ vec2Dot,
78
+ vec2Length,
79
+ vec2Normalize,
80
+ vec2Scale,
81
+ vec2Sub,
82
+ vec3Add,
83
+ vec3Cross,
84
+ vec3Dot,
85
+ vec3Length,
86
+ vec3Normalize,
87
+ vec3Scale,
88
+ vec3Sub
89
+ };
90
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/isometric.ts","../src/vectors.ts","../src/physics.ts"],"sourcesContent":["/**\n * @file isometric.ts\n * @description Isometric projection math: standard 2:1 dimetric projection (X right-down, Y left-down, Z up).\n * @version 1.0.0\n */\n\nexport type Point = { x: number; y: number };\n\nconst ISO_ANGLE = Math.PI / 6;\nconst COS_30 = Math.cos(ISO_ANGLE);\nconst SIN_30 = Math.sin(ISO_ANGLE);\n\n/**\n * Standard isometric projection (X right-down, Y left-down, Z up).\n * @version 1.0.0\n */\nexport const isoTransform = (x: number, y: number, z = 0): Point => ({\n x: (x - y) * COS_30,\n y: (x + y) * SIN_30 - z,\n});\n\n/**\n * Creates a projection function bound to a specific canvas origin and scale.\n */\nexport const getToScreen = (centerX: number, centerY: number, scale = 1) =>\n (x: number, y: number, z: number): Point => {\n const iso = isoTransform(x * scale, y * scale, z * scale);\n return { x: centerX + iso.x, y: centerY + iso.y };\n };\n\n/**\n * Returns a cardinal direction label for a heading in degrees (0–360).\n */\nexport const getCardinalLabel = (heading: number): string => {\n const directions = [\n { min: 337.5, max: 360, label: 'N' },\n { min: 0, max: 22.5, label: 'N' },\n { min: 22.5, max: 67.5, label: 'NE' },\n { min: 67.5, max: 112.5, label: 'E' },\n { min: 112.5, max: 157.5, label: 'SE' },\n { min: 157.5, max: 202.5, label: 'S' },\n { min: 202.5, max: 247.5, label: 'SW' },\n { min: 247.5, max: 292.5, label: 'W' },\n { min: 292.5, max: 337.5, label: 'NW' },\n ];\n for (const dir of directions) {\n if (heading >= dir.min && heading < dir.max) return dir.label;\n }\n return 'N';\n};\n","/**\n * @file vectors.ts\n * @description Lightweight 2D and 3D vector types and operations.\n * @version 1.0.0\n */\n\nexport type Vector2 = { x: number; y: number };\nexport type Vector3 = { x: number; y: number; z: number };\n\nexport const vec2Add = (a: Vector2, b: Vector2): Vector2 => ({ x: a.x + b.x, y: a.y + b.y });\nexport const vec2Sub = (a: Vector2, b: Vector2): Vector2 => ({ x: a.x - b.x, y: a.y - b.y });\nexport const vec2Scale = (v: Vector2, s: number): Vector2 => ({ x: v.x * s, y: v.y * s });\nexport const vec2Length = (v: Vector2): number => Math.sqrt(v.x ** 2 + v.y ** 2);\nexport const vec2Normalize = (v: Vector2): Vector2 => {\n const l = vec2Length(v) || 1;\n return vec2Scale(v, 1 / l);\n};\nexport const vec2Dot = (a: Vector2, b: Vector2): number => a.x * b.x + a.y * b.y;\n\nexport const vec3Add = (a: Vector3, b: Vector3): Vector3 => ({ x: a.x + b.x, y: a.y + b.y, z: a.z + b.z });\nexport const vec3Sub = (a: Vector3, b: Vector3): Vector3 => ({ x: a.x - b.x, y: a.y - b.y, z: a.z - b.z });\nexport const vec3Scale = (v: Vector3, s: number): Vector3 => ({ x: v.x * s, y: v.y * s, z: v.z * s });\nexport const vec3Length = (v: Vector3): number => Math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2);\nexport const vec3Normalize = (v: Vector3): Vector3 => {\n const l = vec3Length(v) || 1;\n return vec3Scale(v, 1 / l);\n};\nexport const vec3Dot = (a: Vector3, b: Vector3): number => a.x * b.x + a.y * b.y + a.z * b.z;\nexport const vec3Cross = (a: Vector3, b: Vector3): Vector3 => ({\n x: a.y * b.z - a.z * b.y,\n y: a.z * b.x - a.x * b.z,\n z: a.x * b.y - a.y * b.x,\n});\n","/**\n * @file physics.ts\n * @description Pure vessel-position integration step, detached from React. Given a current\n * position, velocity, and heading, advances the position by one timestep and wraps it around\n * a square world of the given size.\n * @version 1.0.0\n */\nimport type { Point } from './isometric.js';\n\n/**\n * Advances a vessel's position by one physics step.\n * @param pos - Current { x, y } position in world units.\n * @param velocity - Current velocity in world units per second.\n * @param rotationDeg - Current heading in degrees (0 = north, 90 = east).\n * @param deltaTime - Elapsed time since the last step, in seconds.\n * @param gridSize - Total span of the (square) world; position wraps at ±gridSize/2.\n * @returns The new { x, y } position, wrapped to stay within the world bounds.\n * @version 1.0.0\n */\nexport const stepVesselPosition = (\n pos: Point,\n velocity: number,\n rotationDeg: number,\n deltaTime: number,\n gridSize: number,\n): Point => {\n if (velocity === 0) return pos;\n\n const limit = gridSize / 2;\n const rad = (-rotationDeg * Math.PI) / 180;\n const dx = -velocity * Math.sin(rad) * deltaTime;\n const dy = -velocity * Math.cos(rad) * deltaTime;\n\n let newX = pos.x + dx;\n let newY = pos.y + dy;\n if (newX > limit) newX -= gridSize;\n if (newX < -limit) newX += gridSize;\n if (newY > limit) newY -= gridSize;\n if (newY < -limit) newY += gridSize;\n\n return { x: newX, y: newY };\n};\n"],"mappings":";AAQA,IAAM,YAAY,KAAK,KAAK;AAC5B,IAAM,SAAS,KAAK,IAAI,SAAS;AACjC,IAAM,SAAS,KAAK,IAAI,SAAS;AAM1B,IAAM,eAAe,CAAC,GAAW,GAAW,IAAI,OAAc;AAAA,EACjE,IAAI,IAAI,KAAK;AAAA,EACb,IAAI,IAAI,KAAK,SAAS;AAC1B;AAKO,IAAM,cAAc,CAAC,SAAiB,SAAiB,QAAQ,MAClE,CAAC,GAAW,GAAW,MAAqB;AACxC,QAAM,MAAM,aAAa,IAAI,OAAO,IAAI,OAAO,IAAI,KAAK;AACxD,SAAO,EAAE,GAAG,UAAU,IAAI,GAAG,GAAG,UAAU,IAAI,EAAE;AACpD;AAKG,IAAM,mBAAmB,CAAC,YAA4B;AACzD,QAAM,aAAa;AAAA,IACf,EAAE,KAAK,OAAO,KAAK,KAAK,OAAO,IAAI;AAAA,IACnC,EAAE,KAAK,GAAO,KAAK,MAAO,OAAO,IAAI;AAAA,IACrC,EAAE,KAAK,MAAO,KAAK,MAAO,OAAO,KAAK;AAAA,IACtC,EAAE,KAAK,MAAO,KAAK,OAAO,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
+ {
2
+ "name": "isometric-physics",
3
+ "version": "1.0.0",
4
+ "description": "Zero-dependency isometric projection math, 2D/3D vector operations, and vessel position integration.",
5
+ "author": "Carson Fujita <carsonfujita@gmail.com>",
6
+ "repository": "https://github.com/TheFujirose/isometric-physics",
7
+ "license": "MIT",
8
+ "keywords": [
9
+ "isometric",
10
+ "projection",
11
+ "vectors",
12
+ "physics",
13
+ "math"
14
+ ],
15
+ "sideEffects": false,
16
+ "type": "module",
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
25
+ }
26
+ },
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "devDependencies": {
31
+ "tsup": "^8.5.1",
32
+ "typescript": "^6.0.3"
33
+ },
34
+ "scripts": {
35
+ "build": "tsup",
36
+ "dev": "tsup --watch"
37
+ }
38
+ }