@defold-typescript/types 0.4.2 → 0.5.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.
@@ -3,59 +3,973 @@ import type { Matrix4, Quaternion, Vector, Vector3, Vector4 } from "../src/core-
3
3
 
4
4
  declare global {
5
5
  namespace vmath {
6
+ /**
7
+ * Clamp input value to be in range of [min, max]. In case if input value has vector3|vector4 type
8
+ * return new vector3|vector4 with clamped value at every vector's element.
9
+ * Min/max arguments can be vector3|vector4. In that case clamp excuted per every vector's element
10
+ *
11
+ * @param value - Input value or vector of values
12
+ * @param min - Min value(s) border
13
+ * @param max - Max value(s) border
14
+ * @returns Clamped value or vector
15
+ * @example
16
+ * ```lua
17
+ * local value1 = 56
18
+ * print(vmath.clamp(value1, 89, 134)) -> 89
19
+ * local v2 = vmath.vector3(190, 190, -10)
20
+ * print(vmath.clamp(v2, -50, 150)) -> vmath.vector3(150, 150, -10)
21
+ * local v3 = vmath.vector4(30, -30, 45, 1)
22
+ * print(vmath.clamp(v3, 0, 20)) -> vmath.vector4(20, 0, 20, 1)
23
+ *
24
+ * local min_v = vmath.vector4(0, -10, -10, 1)
25
+ * print(vmath.clamp(v3, min_v, 20)) -> vmath.vector4(20, -10, 20, 1)
26
+ * ```
27
+ */
6
28
  function clamp(value: number | Vector3 | Vector4, min: number | Vector3 | Vector4, max: number | Vector3 | Vector4): number | Vector3 | Vector4;
29
+ /**
30
+ * Calculates the conjugate of a quaternion. The result is a
31
+ * quaternion with the same magnitudes but with the sign of
32
+ * the imaginary (vector) parts changed:
33
+ * `q* = [w, -v]`
34
+ *
35
+ * @param q1 - quaternion of which to calculate the conjugate
36
+ * @returns the conjugate
37
+ * @example
38
+ * ```lua
39
+ * local quat = vmath.quat(1, 2, 3, 4)
40
+ * print(vmath.conj(quat)) --> vmath.quat(-1, -2, -3, 4)
41
+ * ```
42
+ */
7
43
  function conj(q1: Quaternion): Quaternion;
44
+ /**
45
+ * Given two linearly independent vectors P and Q, the cross product,
46
+ * P × Q, is a vector that is perpendicular to both P and Q and
47
+ * therefore normal to the plane containing them.
48
+ * If the two vectors have the same direction (or have the exact
49
+ * opposite direction from one another, i.e. are not linearly independent)
50
+ * or if either one has zero length, then their cross product is zero.
51
+ *
52
+ * @param v1 - first vector
53
+ * @param v2 - second vector
54
+ * @returns a new vector representing the cross product
55
+ * @example
56
+ * ```lua
57
+ * local vec1 = vmath.vector3(1, 0, 0)
58
+ * local vec2 = vmath.vector3(0, 1, 0)
59
+ * print(vmath.cross(vec1, vec2)) --> vmath.vector3(0, 0, 1)
60
+ * local vec3 = vmath.vector3(-1, 0, 0)
61
+ * print(vmath.cross(vec1, vec3)) --> vmath.vector3(0, -0, 0)
62
+ * ```
63
+ */
8
64
  function cross(v1: Vector3, v2: Vector3): Vector3;
65
+ /**
66
+ * The returned value is a scalar defined as:
67
+ * `P ⋅ Q = |P| |Q| cos θ`
68
+ * where θ is the angle between the vectors P and Q.
69
+ * - If the dot product is positive then the angle between the vectors is below 90 degrees.
70
+ * - If the dot product is zero the vectors are perpendicular (at right-angles to each other).
71
+ * - If the dot product is negative then the angle between the vectors is more than 90 degrees.
72
+ *
73
+ * @param v1 - first vector
74
+ * @param v2 - second vector
75
+ * @returns dot product
76
+ * @example
77
+ * ```lua
78
+ * if vmath.dot(vector1, vector2) == 0 then
79
+ * -- The two vectors are perpendicular (at right-angles to each other)
80
+ * ...
81
+ * end
82
+ * ```
83
+ */
9
84
  function dot(v1: Vector3 | Vector4, v2: Vector3 | Vector4): number;
85
+ /**
86
+ * Converts euler angles (x, y, z) in degrees into a quaternion
87
+ * The error is guaranteed to be less than 0.001.
88
+ * If the first argument is vector3, its values are used as x, y, z angles.
89
+ *
90
+ * @param x - rotation around x-axis in degrees or vector3 with euler angles in degrees
91
+ * @param y - rotation around y-axis in degrees
92
+ * @param z - rotation around z-axis in degrees
93
+ * @returns quaternion describing an equivalent rotation (231 (YZX) rotation sequence)
94
+ * @example
95
+ * ```lua
96
+ * local q = vmath.euler_to_quat(0, 45, 90)
97
+ * print(q) --> vmath.quat(0.27059805393219, 0.27059805393219, 0.65328145027161, 0.65328145027161)
98
+ *
99
+ * local v = vmath.vector3(0, 0, 90)
100
+ * print(vmath.euler_to_quat(v)) --> vmath.quat(0, 0, 0.70710676908493, 0.70710676908493)
101
+ * ```
102
+ */
10
103
  function euler_to_quat(x: number | Vector3, y: number, z: number): Quaternion;
104
+ /**
105
+ * The resulting matrix is the inverse of the supplied matrix.
106
+ * For ortho-normal matrices, e.g. regular object transformation,
107
+ * use `vmath.ortho_inv()` instead.
108
+ * The specialized inverse for ortho-normalized matrices is much faster
109
+ * than the general inverse.
110
+ *
111
+ * @param m1 - matrix to invert
112
+ * @returns inverse of the supplied matrix
113
+ * @example
114
+ * ```lua
115
+ * local mat1 = vmath.matrix4_rotation_z(3.141592653)
116
+ * local mat2 = vmath.inv(mat1)
117
+ * -- M * inv(M) = identity matrix
118
+ * print(mat1 * mat2) --> vmath.matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
119
+ * ```
120
+ */
11
121
  function inv(m1: Matrix4): Matrix4;
122
+ /**
123
+ * Returns the length of the supplied vector or quaternion.
124
+ * If you are comparing the lengths of vectors or quaternions, you should compare
125
+ * the length squared instead as it is slightly more efficient to calculate
126
+ * (it eliminates a square root calculation).
127
+ *
128
+ * @param v - value of which to calculate the length
129
+ * @returns length
130
+ * @example
131
+ * ```lua
132
+ * if vmath.length(self.velocity) < max_velocity then
133
+ * -- The speed (velocity vector) is below max.
134
+ *
135
+ * -- TODO: max_velocity can be expressed as squared
136
+ * -- so we can compare with length_sqr() instead.
137
+ * ...
138
+ * end
139
+ * ```
140
+ */
12
141
  function length(v: Vector3 | Vector4 | Quaternion): number;
142
+ /**
143
+ * Returns the squared length of the supplied vector or quaternion.
144
+ *
145
+ * @param v - value of which to calculate the squared length
146
+ * @returns squared length
147
+ * @example
148
+ * ```lua
149
+ * if vmath.length_sqr(vector1) < vmath.length_sqr(vector2) then
150
+ * -- Vector 1 has less magnitude than vector 2
151
+ * ...
152
+ * end
153
+ * ```
154
+ */
13
155
  function length_sqr(v: Vector3 | Vector4 | Quaternion): number;
156
+ /**
157
+ * Linearly interpolate between two vectors. The function
158
+ * treats the vectors as positions and interpolates between
159
+ * the positions in a straight line. Lerp is useful to describe
160
+ * transitions from one place to another over time.
161
+ * The function does not clamp t between 0 and 1.
162
+ *
163
+ * @param t - interpolation parameter, 0-1
164
+ * @param v1 - vector to lerp from
165
+ * @param v2 - vector to lerp to
166
+ * @returns the lerped vector
167
+ * @example
168
+ * ```lua
169
+ * function init(self)
170
+ * self.t = 0
171
+ * end
172
+ *
173
+ * function update(self, dt)
174
+ * self.t = self.t + dt
175
+ * if self.t <= 1 then
176
+ * local startpos = vmath.vector3(0, 600, 0)
177
+ * local endpos = vmath.vector3(600, 0, 0)
178
+ * local pos = vmath.lerp(self.t, startpos, endpos)
179
+ * go.set_position(pos, "go")
180
+ * end
181
+ * end
182
+ * ```
183
+ */
14
184
  function lerp(t: number, v1: Vector3 | Vector4, v2: Vector3 | Vector4): Vector3 | Vector4;
185
+ /**
186
+ * Linearly interpolate between two quaternions. Linear
187
+ * interpolation of rotations are only useful for small
188
+ * rotations. For interpolations of arbitrary rotations,
189
+ * vmath.slerp yields much better results.
190
+ * The function does not clamp t between 0 and 1.
191
+ *
192
+ * @param t - interpolation parameter, 0-1
193
+ * @param q1 - quaternion to lerp from
194
+ * @param q2 - quaternion to lerp to
195
+ * @returns the lerped quaternion
196
+ * @example
197
+ * ```lua
198
+ * function init(self)
199
+ * self.t = 0
200
+ * end
201
+ *
202
+ * function update(self, dt)
203
+ * self.t = self.t + dt
204
+ * if self.t <= 1 then
205
+ * local startrot = vmath.quat_rotation_z(0)
206
+ * local endrot = vmath.quat_rotation_z(3.141592653)
207
+ * local rot = vmath.lerp(self.t, startrot, endrot)
208
+ * go.set_rotation(rot, "go")
209
+ * end
210
+ * end
211
+ * ```
212
+ */
15
213
  function lerp(t: number, q1: Quaternion, q2: Quaternion): Quaternion;
214
+ /**
215
+ * Linearly interpolate between two values. Lerp is useful
216
+ * to describe transitions from one value to another over time.
217
+ * The function does not clamp t between 0 and 1.
218
+ *
219
+ * @param t - interpolation parameter, 0-1
220
+ * @param n1 - number to lerp from
221
+ * @param n2 - number to lerp to
222
+ * @returns the lerped number
223
+ * @example
224
+ * ```lua
225
+ * function init(self)
226
+ * self.t = 0
227
+ * end
228
+ *
229
+ * function update(self, dt)
230
+ * self.t = self.t + dt
231
+ * if self.t <= 1 then
232
+ * local startx = 0
233
+ * local endx = 600
234
+ * local x = vmath.lerp(self.t, startx, endx)
235
+ * go.set_position(vmath.vector3(x, 100, 0), "go")
236
+ * end
237
+ * end
238
+ * ```
239
+ */
16
240
  function lerp(t: number, n1: number, n2: number): number;
241
+ /**
242
+ * The resulting identity matrix describes a transform with
243
+ * no translation or rotation.
244
+ *
245
+ * @returns identity matrix
246
+ * @example
247
+ * ```lua
248
+ * local mat = vmath.matrix4()
249
+ * print(mat) --> vmath.matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
250
+ * -- get column 0:
251
+ * print(mat.c0) --> vmath.vector4(1, 0, 0, 0)
252
+ * -- get the value in row 3 and column 2:
253
+ * print(mat.m32) --> 0
254
+ * ```
255
+ */
17
256
  function matrix4(): Matrix4;
257
+ /**
258
+ * Creates a new matrix with all components set to the
259
+ * corresponding values from the supplied matrix. I.e.
260
+ * the function creates a copy of the given matrix.
261
+ *
262
+ * @param m1 - existing matrix
263
+ * @returns matrix which is a copy of the specified matrix
264
+ * @example
265
+ * ```lua
266
+ * local mat1 = vmath.matrix4_rotation_x(3.141592653)
267
+ * local mat2 = vmath.matrix4(mat1)
268
+ * if mat1 == mat2 then
269
+ * -- yes, they are equal
270
+ * print(mat2) --> vmath.matrix4(1, 0, 0, 0, 0, -1, 8.7422776573476e-08, 0, 0, -8.7422776573476e-08, -1, 0, 0, 0, 0, 1)
271
+ * end
272
+ * ```
273
+ */
18
274
  function matrix4(m1: Matrix4): Matrix4;
275
+ /**
276
+ * The resulting matrix describes a rotation around the axis by the specified angle.
277
+ *
278
+ * @param v - axis
279
+ * @param angle - angle in radians
280
+ * @returns matrix represented by axis and angle
281
+ * @example
282
+ * ```lua
283
+ * local vec = vmath.vector4(1, 1, 0, 0)
284
+ * local axis = vmath.vector3(0, 0, 1) -- z-axis
285
+ * local mat = vmath.matrix4_axis_angle(axis, 3.141592653)
286
+ * print(mat * vec) --> vmath.vector4(-0.99999994039536, -1.0000001192093, 0, 0)
287
+ * ```
288
+ */
19
289
  function matrix4_axis_angle(v: Vector3, angle: number): Matrix4;
290
+ /**
291
+ * Creates a new matrix constructed from separate
292
+ * translation vector, roation quaternion and scale vector
293
+ *
294
+ * @param translation - translation
295
+ * @param rotation - rotation
296
+ * @param scale - scale
297
+ * @returns new matrix4
298
+ * @example
299
+ * ```lua
300
+ * local translation = vmath.vector3(103, -95, 14)
301
+ * local quat = vmath.quat(1, 2, 3, 4)
302
+ * local scale = vmath.vector3(1, 0.5, 0.5)
303
+ * local result = vmath.matrix4_compose(translation, quat, scale)
304
+ * print(result) --> vmath.matrix4(-25, -10, 11, 103, 28, -9.5, 2, -95, -10, 10, -4.5, 14, 0, 0, 0, 1)
305
+ * ```
306
+ */
20
307
  function matrix4_compose(translation: Vector3 | Vector4, rotation: Quaternion, scale: Vector3): Matrix4;
308
+ /**
309
+ * Constructs a frustum matrix from the given values. The left, right,
310
+ * top and bottom coordinates of the view cone are expressed as distances
311
+ * from the center of the near clipping plane. The near and far coordinates
312
+ * are expressed as distances from the tip of the view frustum cone.
313
+ *
314
+ * @param left - coordinate for left clipping plane
315
+ * @param right - coordinate for right clipping plane
316
+ * @param bottom - coordinate for bottom clipping plane
317
+ * @param top - coordinate for top clipping plane
318
+ * @param near - coordinate for near clipping plane
319
+ * @param far - coordinate for far clipping plane
320
+ * @returns matrix representing the frustum
321
+ * @example
322
+ * ```lua
323
+ * -- Construct a projection frustum with a vertical and horizontal
324
+ * -- FOV of 45 degrees. Useful for rendering a square view.
325
+ * local proj = vmath.matrix4_frustum(-1, 1, -1, 1, 1, 1000)
326
+ * render.set_projection(proj)
327
+ * ```
328
+ */
21
329
  function matrix4_frustum(left: number, right: number, bottom: number, top: number, near: number, far: number): Matrix4;
330
+ /**
331
+ * The resulting matrix is created from the supplied look-at parameters.
332
+ * This is useful for constructing a view matrix for a camera or
333
+ * rendering in general.
334
+ *
335
+ * @param eye - eye position
336
+ * @param look_at - look-at position
337
+ * @param up - up vector
338
+ * @returns look-at matrix
339
+ * @example
340
+ * ```lua
341
+ * -- Set up a perspective camera at z 100 with 45 degrees (pi/2) FOV
342
+ * -- Aspect ratio 4:3
343
+ * local eye = vmath.vector3(0, 0, 100)
344
+ * local look_at = vmath.vector3(0, 0, 0)
345
+ * local up = vmath.vector3(0, 1, 0)
346
+ * local view = vmath.matrix4_look_at(eye, look_at, up)
347
+ * render.set_view(view)
348
+ * local proj = vmath.matrix4_perspective(3.141592/2, 4/3, 1, 1000)
349
+ * render.set_projection(proj)
350
+ * ```
351
+ */
22
352
  function matrix4_look_at(eye: Vector3, look_at: Vector3, up: Vector3): Matrix4;
353
+ /**
354
+ * Creates an orthographic projection matrix.
355
+ * This is useful to construct a projection matrix for a camera or rendering in general.
356
+ *
357
+ * @param left - coordinate for left clipping plane
358
+ * @param right - coordinate for right clipping plane
359
+ * @param bottom - coordinate for bottom clipping plane
360
+ * @param top - coordinate for top clipping plane
361
+ * @param near - coordinate for near clipping plane
362
+ * @param far - coordinate for far clipping plane
363
+ * @returns orthographic projection matrix
364
+ * @example
365
+ * ```lua
366
+ * -- Set up an orthographic projection based on the width and height
367
+ * -- of the game window.
368
+ * local w = render.get_width()
369
+ * local h = render.get_height()
370
+ * local proj = vmath.matrix4_orthographic(- w / 2, w / 2, -h / 2, h / 2, -1000, 1000)
371
+ * render.set_projection(proj)
372
+ * ```
373
+ */
23
374
  function matrix4_orthographic(left: number, right: number, bottom: number, top: number, near: number, far: number): Matrix4;
375
+ /**
376
+ * Creates a perspective projection matrix.
377
+ * This is useful to construct a projection matrix for a camera or rendering in general.
378
+ *
379
+ * @param fov - angle of the full vertical field of view in radians
380
+ * @param aspect - aspect ratio
381
+ * @param near - coordinate for near clipping plane
382
+ * @param far - coordinate for far clipping plane
383
+ * @returns perspective projection matrix
384
+ * @example
385
+ * ```lua
386
+ * -- Set up a perspective camera at z 100 with 45 degrees (pi/2) FOV
387
+ * -- Aspect ratio 4:3
388
+ * local eye = vmath.vector3(0, 0, 100)
389
+ * local look_at = vmath.vector3(0, 0, 0)
390
+ * local up = vmath.vector3(0, 1, 0)
391
+ * local view = vmath.matrix4_look_at(eye, look_at, up)
392
+ * render.set_view(view)
393
+ * local proj = vmath.matrix4_perspective(3.141592/2, 4/3, 1, 1000)
394
+ * render.set_projection(proj)
395
+ * ```
396
+ */
24
397
  function matrix4_perspective(fov: number, aspect: number, near: number, far: number): Matrix4;
398
+ /**
399
+ * The resulting matrix describes the same rotation as the quaternion, but does not have any translation (also like the quaternion).
400
+ *
401
+ * @param q - quaternion to create matrix from
402
+ * @returns matrix represented by quaternion
403
+ * @example
404
+ * ```lua
405
+ * local vec = vmath.vector4(1, 1, 0, 0)
406
+ * local quat = vmath.quat_rotation_z(3.141592653)
407
+ * local mat = vmath.matrix4_quat(quat)
408
+ * print(mat * vec) --> vmath.matrix4_frustum(-1, 1, -1, 1, 1, 1000)
409
+ * ```
410
+ */
25
411
  function matrix4_quat(q: Quaternion): Matrix4;
412
+ /**
413
+ * The resulting matrix describes a rotation around the x-axis
414
+ * by the specified angle.
415
+ *
416
+ * @param angle - angle in radians around x-axis
417
+ * @returns matrix from rotation around x-axis
418
+ * @example
419
+ * ```lua
420
+ * local vec = vmath.vector4(1, 1, 0, 0)
421
+ * local mat = vmath.matrix4_rotation_x(3.141592653)
422
+ * print(mat * vec) --> vmath.vector4(1, -1, -8.7422776573476e-08, 0)
423
+ * ```
424
+ */
26
425
  function matrix4_rotation_x(angle: number): Matrix4;
426
+ /**
427
+ * The resulting matrix describes a rotation around the y-axis
428
+ * by the specified angle.
429
+ *
430
+ * @param angle - angle in radians around y-axis
431
+ * @returns matrix from rotation around y-axis
432
+ * @example
433
+ * ```lua
434
+ * local vec = vmath.vector4(1, 1, 0, 0)
435
+ * local mat = vmath.matrix4_rotation_y(3.141592653)
436
+ * print(mat * vec) --> vmath.vector4(-1, 1, 8.7422776573476e-08, 0)
437
+ * ```
438
+ */
27
439
  function matrix4_rotation_y(angle: number): Matrix4;
440
+ /**
441
+ * The resulting matrix describes a rotation around the z-axis
442
+ * by the specified angle.
443
+ *
444
+ * @param angle - angle in radians around z-axis
445
+ * @returns matrix from rotation around z-axis
446
+ * @example
447
+ * ```lua
448
+ * local vec = vmath.vector4(1, 1, 0, 0)
449
+ * local mat = vmath.matrix4_rotation_z(3.141592653)
450
+ * print(mat * vec) --> vmath.vector4(-0.99999994039536, -1.0000001192093, 0, 0)
451
+ * ```
452
+ */
28
453
  function matrix4_rotation_z(angle: number): Matrix4;
454
+ /**
455
+ * Creates a new matrix constructed from scale vector
456
+ *
457
+ * @param scale - scale
458
+ * @returns new matrix4
459
+ * @example
460
+ * ```lua
461
+ * local scale = vmath.vector3(1, 0.5, 0.5)
462
+ * local result = vmath.matrix4_scale(scale)
463
+ * print(result) --> vmath.matrix4(1, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 1)
464
+ * ```
465
+ */
29
466
  function matrix4_scale(scale: Vector3): Matrix4;
467
+ /**
468
+ * creates a new matrix4 from uniform scale
469
+ *
470
+ * @param scale - scale
471
+ * @returns new matrix4
472
+ * @example
473
+ * ```lua
474
+ * local result = vmath.matrix4_scale(0.5)
475
+ * print(result) --> vmath.matrix4(0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 1)
476
+ * ```
477
+ */
30
478
  function matrix4_scale(scale: number): Matrix4;
479
+ /**
480
+ * Creates a new matrix4 from three scale components
481
+ *
482
+ * @param scale_x - scale along X axis
483
+ * @param scale_y - sclae along Y axis
484
+ * @param scale_z - scale along Z asis
485
+ * @returns new matrix4
486
+ * @example
487
+ * ```lua
488
+ * local result = vmath.matrix4_scale(1, 0.5, 0.5)
489
+ * print(result) --> vmath.matrix4(1, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 1)
490
+ * ```
491
+ */
31
492
  function matrix4_scale(scale_x: number, scale_y: number, scale_z: number): Matrix4;
493
+ /**
494
+ * The resulting matrix describes a translation of a point
495
+ * in euclidean space.
496
+ *
497
+ * @param position - position vector to create matrix from
498
+ * @returns matrix from the supplied position vector
499
+ * @example
500
+ * ```lua
501
+ * -- Set camera view from custom view and translation matrices
502
+ * local mat_trans = vmath.matrix4_translation(vmath.vector3(0, 10, 100))
503
+ * local mat_view = vmath.matrix4_rotation_y(-3.141592/4)
504
+ * render.set_view(mat_view * mat_trans)
505
+ * ```
506
+ */
32
507
  function matrix4_translation(position: Vector3 | Vector4): Matrix4;
508
+ /**
509
+ * Performs an element wise multiplication between two vectors of the same type
510
+ * The returned value is a vector defined as (e.g. for a vector3):
511
+ * `v = vmath.mul_per_elem(a, b) = vmath.vector3(a.x * b.x, a.y * b.y, a.z * b.z)`
512
+ *
513
+ * @param v1 - first vector
514
+ * @param v2 - second vector
515
+ * @returns multiplied vector
516
+ * @example
517
+ * ```lua
518
+ * local blend_color = vmath.mul_per_elem(color1, color2)
519
+ * ```
520
+ */
33
521
  function mul_per_elem(v1: Vector3 | Vector4, v2: Vector3 | Vector4): Vector3 | Vector4;
522
+ /**
523
+ * Normalizes a vector, i.e. returns a new vector with the same
524
+ * direction as the input vector, but with length 1.
525
+ * The length of the vector must be above 0, otherwise a
526
+ * division-by-zero will occur.
527
+ *
528
+ * @param v1 - vector to normalize
529
+ * @returns new normalized vector
530
+ * @example
531
+ * ```lua
532
+ * local vec = vmath.vector3(1, 2, 3)
533
+ * local norm_vec = vmath.normalize(vec)
534
+ * print(norm_vec) --> vmath.vector3(0.26726123690605, 0.5345224738121, 0.80178368091583)
535
+ * print(vmath.length(norm_vec)) --> 0.99999994039536
536
+ * ```
537
+ */
34
538
  function normalize(v1: Vector3 | Vector4 | Quaternion): Vector3 | Vector4 | Quaternion;
539
+ /**
540
+ * The resulting matrix is the inverse of the supplied matrix.
541
+ * The supplied matrix has to be an ortho-normal matrix, e.g.
542
+ * describe a regular object transformation.
543
+ * For matrices that are not ortho-normal
544
+ * use the general inverse `vmath.inv()` instead.
545
+ *
546
+ * @param m1 - ortho-normalized matrix to invert
547
+ * @returns inverse of the supplied matrix
548
+ * @example
549
+ * ```lua
550
+ * local mat1 = vmath.matrix4_rotation_z(3.141592653)
551
+ * local mat2 = vmath.ortho_inv(mat1)
552
+ * -- M * inv(M) = identity matrix
553
+ * print(mat1 * mat2) --> vmath.matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
554
+ * ```
555
+ */
35
556
  function ortho_inv(m1: Matrix4): Matrix4;
557
+ /**
558
+ * Calculates the extent the projection of the first vector onto the second.
559
+ * The returned value is a scalar p defined as:
560
+ * `p = |P| cos &#x03B8; / |Q|`
561
+ * where &#x03B8; is the angle between the vectors P and Q.
562
+ *
563
+ * @param v1 - vector to be projected on the second
564
+ * @param v2 - vector onto which the first will be projected, must not have zero length
565
+ * @returns the projected extent of the first vector onto the second
566
+ * @example
567
+ * ```lua
568
+ * local v1 = vmath.vector3(1, 1, 0)
569
+ * local v2 = vmath.vector3(2, 0, 0)
570
+ * print(vmath.project(v1, v2)) --> 0.5
571
+ * ```
572
+ */
36
573
  function project(v1: Vector3, v2: Vector3): number;
574
+ /**
575
+ * Creates a new identity quaternion. The identity
576
+ * quaternion is equal to:
577
+ * `vmath.quat(0, 0, 0, 1)`
578
+ *
579
+ * @returns new identity quaternion
580
+ * @example
581
+ * ```lua
582
+ * local quat = vmath.quat()
583
+ * print(quat) --> vmath.quat(0, 0, 0, 1)
584
+ * print(quat.w) --> 1
585
+ * ```
586
+ */
37
587
  function quat(): Quaternion;
588
+ /**
589
+ * Creates a new quaternion with all components set to the
590
+ * corresponding values from the supplied quaternion. I.e.
591
+ * This function creates a copy of the given quaternion.
592
+ *
593
+ * @param q1 - existing quaternion
594
+ * @returns new quaternion
595
+ * @example
596
+ * ```lua
597
+ * local quat1 = vmath.quat(1, 2, 3, 4)
598
+ * local quat2 = vmath.quat(quat1)
599
+ * if quat1 == quat2 then
600
+ * -- yes, they are equal
601
+ * print(quat2) --> vmath.quat(1, 2, 3, 4)
602
+ * end
603
+ * ```
604
+ */
38
605
  function quat(q1: Quaternion): Quaternion;
606
+ /**
607
+ * Creates a new quaternion with the components set
608
+ * according to the supplied parameter values.
609
+ *
610
+ * @param x - x coordinate
611
+ * @param y - y coordinate
612
+ * @param z - z coordinate
613
+ * @param w - w coordinate
614
+ * @returns new quaternion
615
+ * @example
616
+ * ```lua
617
+ * local quat = vmath.quat(1, 2, 3, 4)
618
+ * print(quat) --> vmath.quat(1, 2, 3, 4)
619
+ * ```
620
+ */
39
621
  function quat(x: number, y: number, z: number, w: number): Quaternion;
622
+ /**
623
+ * The resulting quaternion describes a rotation of `angle`
624
+ * radians around the axis described by the unit vector `v`.
625
+ *
626
+ * @param v - axis
627
+ * @param angle - angle
628
+ * @returns quaternion representing the axis-angle rotation
629
+ * @example
630
+ * ```lua
631
+ * local axis = vmath.vector3(1, 0, 0)
632
+ * local rot = vmath.quat_axis_angle(axis, 3.141592653)
633
+ * local vec = vmath.vector3(1, 1, 0)
634
+ * print(vmath.rotate(rot, vec)) --> vmath.vector3(1, -1, -8.7422776573476e-08)
635
+ * ```
636
+ */
40
637
  function quat_axis_angle(v: Vector3, angle: number): Quaternion;
638
+ /**
639
+ * The resulting quaternion describes the rotation from the
640
+ * identity quaternion (no rotation) to the coordinate system
641
+ * as described by the given x, y and z base unit vectors.
642
+ *
643
+ * @param x - x base vector
644
+ * @param y - y base vector
645
+ * @param z - z base vector
646
+ * @returns quaternion representing the rotation of the specified base vectors
647
+ * @example
648
+ * ```lua
649
+ * -- Axis rotated 90 degrees around z.
650
+ * local rot_x = vmath.vector3(0, -1, 0)
651
+ * local rot_y = vmath.vector3(1, 0, 0)
652
+ * local z = vmath.vector3(0, 0, 1)
653
+ * local rot1 = vmath.quat_basis(rot_x, rot_y, z)
654
+ * local rot2 = vmath.quat_from_to(vmath.vector3(0, 1, 0), vmath.vector3(1, 0, 0))
655
+ * if rot1 == rot2 then
656
+ * -- These quaternions are equal!
657
+ * print(rot2) --> vmath.quat(0, 0, -0.70710676908493, 0.70710676908493)
658
+ * end
659
+ * ```
660
+ */
41
661
  function quat_basis(x: Vector3, y: Vector3, z: Vector3): Quaternion;
662
+ /**
663
+ * The resulting quaternion describes the rotation that,
664
+ * if applied to the first vector, would rotate the first
665
+ * vector to the second. The two vectors must be unit
666
+ * vectors (of length 1).
667
+ * The result is undefined if the two vectors point in opposite directions
668
+ *
669
+ * @param v1 - first unit vector, before rotation
670
+ * @param v2 - second unit vector, after rotation
671
+ * @returns quaternion representing the rotation from first to second vector
672
+ * @example
673
+ * ```lua
674
+ * local v1 = vmath.vector3(1, 0, 0)
675
+ * local v2 = vmath.vector3(0, 1, 0)
676
+ * local rot = vmath.quat_from_to(v1, v2)
677
+ * print(vmath.rotate(rot, v1)) --> vmath.vector3(0, 0.99999994039536, 0)
678
+ * ```
679
+ */
42
680
  function quat_from_to(v1: Vector3, v2: Vector3): Quaternion;
681
+ /**
682
+ * Creates a new quaternion with the components set
683
+ * according to the supplied parameter values.
684
+ *
685
+ * @param matrix - source matrix4
686
+ * @returns new quaternion
687
+ */
43
688
  function quat_matrix4(matrix: Matrix4): Quaternion;
689
+ /**
690
+ * The resulting quaternion describes a rotation of `angle`
691
+ * radians around the x-axis.
692
+ *
693
+ * @param angle - angle in radians around x-axis
694
+ * @returns quaternion representing the rotation around the x-axis
695
+ * @example
696
+ * ```lua
697
+ * local rot = vmath.quat_rotation_x(3.141592653)
698
+ * local vec = vmath.vector3(1, 1, 0)
699
+ * print(vmath.rotate(rot, vec)) --> vmath.vector3(1, -1, -8.7422776573476e-08)
700
+ * ```
701
+ */
44
702
  function quat_rotation_x(angle: number): Quaternion;
703
+ /**
704
+ * The resulting quaternion describes a rotation of `angle`
705
+ * radians around the y-axis.
706
+ *
707
+ * @param angle - angle in radians around y-axis
708
+ * @returns quaternion representing the rotation around the y-axis
709
+ * @example
710
+ * ```lua
711
+ * local rot = vmath.quat_rotation_y(3.141592653)
712
+ * local vec = vmath.vector3(1, 1, 0)
713
+ * print(vmath.rotate(rot, vec)) --> vmath.vector3(-1, 1, 8.7422776573476e-08)
714
+ * ```
715
+ */
45
716
  function quat_rotation_y(angle: number): Quaternion;
717
+ /**
718
+ * The resulting quaternion describes a rotation of `angle`
719
+ * radians around the z-axis.
720
+ *
721
+ * @param angle - angle in radians around z-axis
722
+ * @returns quaternion representing the rotation around the z-axis
723
+ * @example
724
+ * ```lua
725
+ * local rot = vmath.quat_rotation_z(3.141592653)
726
+ * local vec = vmath.vector3(1, 1, 0)
727
+ * print(vmath.rotate(rot, vec)) --> vmath.vector3(-0.99999988079071, -1, 0)
728
+ * ```
729
+ */
46
730
  function quat_rotation_z(angle: number): Quaternion;
731
+ /**
732
+ * Converts a quaternion into euler angles (r0, r1, r2), based on YZX rotation order.
733
+ * To handle gimbal lock (singularity at r1 ~ +/- 90 degrees), the cut off is at r0 = +/- 88.85 degrees, which snaps to +/- 90.
734
+ * The provided quaternion is expected to be normalized.
735
+ * The error is guaranteed to be less than +/- 0.02 degrees
736
+ *
737
+ * @param q - source quaternion
738
+ * @example
739
+ * ```lua
740
+ * local q = vmath.quat_rotation_z(math.rad(90))
741
+ * print(vmath.quat_to_euler(q)) --> 0 0 90
742
+ *
743
+ * local q2 = vmath.quat_rotation_y(math.rad(45)) * vmath.quat_rotation_z(math.rad(90))
744
+ * local v = vmath.vector3(vmath.quat_to_euler(q2))
745
+ * print(v) --> vmath.vector3(0, 45, 90)
746
+ * ```
747
+ */
47
748
  function quat_to_euler(q: Quaternion): LuaMultiReturn<[number, number, number]>;
749
+ /**
750
+ * Returns a new vector from the supplied vector that is
751
+ * rotated by the rotation described by the supplied
752
+ * quaternion.
753
+ *
754
+ * @param q - quaternion
755
+ * @param v1 - vector to rotate
756
+ * @returns the rotated vector
757
+ * @example
758
+ * ```lua
759
+ * local vec = vmath.vector3(1, 1, 0)
760
+ * local rot = vmath.quat_rotation_z(3.141592563)
761
+ * print(vmath.rotate(rot, vec)) --> vmath.vector3(-1.0000002384186, -0.99999988079071, 0)
762
+ * ```
763
+ */
48
764
  function rotate(q: Quaternion, v1: Vector3): Vector3;
765
+ /**
766
+ * Spherically interpolates between two vectors. The difference to
767
+ * lerp is that slerp treats the vectors as directions instead of
768
+ * positions in space.
769
+ * The direction of the returned vector is interpolated by the angle
770
+ * and the magnitude is interpolated between the magnitudes of the
771
+ * from and to vectors.
772
+ * Slerp is computationally more expensive than lerp.
773
+ * The function does not clamp t between 0 and 1.
774
+ *
775
+ * @param t - interpolation parameter, 0-1
776
+ * @param v1 - vector to slerp from
777
+ * @param v2 - vector to slerp to
778
+ * @returns the slerped vector
779
+ * @example
780
+ * ```lua
781
+ * function init(self)
782
+ * self.t = 0
783
+ * end
784
+ *
785
+ * function update(self, dt)
786
+ * self.t = self.t + dt
787
+ * if self.t <= 1 then
788
+ * local startpos = vmath.vector3(0, 600, 0)
789
+ * local endpos = vmath.vector3(600, 0, 0)
790
+ * local pos = vmath.slerp(self.t, startpos, endpos)
791
+ * go.set_position(pos, "go")
792
+ * end
793
+ * end
794
+ * ```
795
+ */
49
796
  function slerp(t: number, v1: Vector3 | Vector4, v2: Vector3 | Vector4): Vector3 | Vector4;
797
+ /**
798
+ * Slerp travels the torque-minimal path maintaining constant
799
+ * velocity, which means it travels along the straightest path along
800
+ * the rounded surface of a sphere. Slerp is useful for interpolation
801
+ * of rotations.
802
+ * Slerp travels the torque-minimal path, which means it travels
803
+ * along the straightest path the rounded surface of a sphere.
804
+ * The function does not clamp t between 0 and 1.
805
+ *
806
+ * @param t - interpolation parameter, 0-1
807
+ * @param q1 - quaternion to slerp from
808
+ * @param q2 - quaternion to slerp to
809
+ * @returns the slerped quaternion
810
+ * @example
811
+ * ```lua
812
+ * function init(self)
813
+ * self.t = 0
814
+ * end
815
+ *
816
+ * function update(self, dt)
817
+ * self.t = self.t + dt
818
+ * if self.t <= 1 then
819
+ * local startrot = vmath.quat_rotation_z(0)
820
+ * local endrot = vmath.quat_rotation_z(3.141592653)
821
+ * local rot = vmath.slerp(self.t, startrot, endrot)
822
+ * go.set_rotation(rot, "go")
823
+ * end
824
+ * end
825
+ * ```
826
+ */
50
827
  function slerp(t: number, q1: Quaternion, q2: Quaternion): Quaternion;
828
+ /**
829
+ * Creates a vector of arbitrary size. The vector is initialized
830
+ * with numeric values from a table.
831
+ * The table values are converted to floating point
832
+ * values. If a value cannot be converted, a 0 is stored in that
833
+ * value position in the vector.
834
+ *
835
+ * @param t - table of numbers
836
+ * @returns new vector
837
+ * @example
838
+ * ```lua
839
+ * How to create a vector with custom data to be used for animation easing:
840
+ * local values = { 0, 0.5, 0 }
841
+ * local vec = vmath.vector(values)
842
+ * print(vec) --> vmath.vector (size: 3)
843
+ * print(vec[2]) --> 0.5
844
+ * ```
845
+ */
51
846
  function vector(t: number[]): Vector;
847
+ /**
848
+ * Creates a new zero vector with all components set to 0.
849
+ *
850
+ * @returns new zero vector
851
+ * @example
852
+ * ```lua
853
+ * local vec = vmath.vector3()
854
+ * pprint(vec) --> vmath.vector3(0, 0, 0)
855
+ * print(vec.x) --> 0
856
+ * ```
857
+ */
52
858
  function vector3(): Vector3;
859
+ /**
860
+ * Creates a new vector with all components set to the
861
+ * supplied scalar value.
862
+ *
863
+ * @param n - scalar value to splat
864
+ * @returns new vector
865
+ * @example
866
+ * ```lua
867
+ * local vec = vmath.vector3(1.0)
868
+ * print(vec) --> vmath.vector3(1, 1, 1)
869
+ * print(vec.x) --> 1
870
+ * ```
871
+ */
53
872
  function vector3(n: number): Vector3;
873
+ /**
874
+ * Creates a new vector with all components set to the
875
+ * corresponding values from the supplied vector. I.e.
876
+ * This function creates a copy of the given vector.
877
+ *
878
+ * @param v1 - existing vector
879
+ * @returns new vector
880
+ * @example
881
+ * ```lua
882
+ * local vec1 = vmath.vector3(1.0)
883
+ * local vec2 = vmath.vector3(vec1)
884
+ * if vec1 == vec2 then
885
+ * -- yes, they are equal
886
+ * print(vec2) --> vmath.vector3(1, 1, 1)
887
+ * end
888
+ * ```
889
+ */
54
890
  function vector3(v1: Vector3): Vector3;
891
+ /**
892
+ * Creates a new vector with the components set to the
893
+ * supplied values.
894
+ *
895
+ * @param x - x coordinate
896
+ * @param y - y coordinate
897
+ * @param z - z coordinate
898
+ * @returns new vector
899
+ * @example
900
+ * ```lua
901
+ * local vec = vmath.vector3(1.0, 2.0, 3.0)
902
+ * print(vec) --> vmath.vector3(1, 2, 3)
903
+ * print(-vec) --> vmath.vector3(-1, -2, -3)
904
+ * print(vec * 2) --> vmath.vector3(2, 4, 6)
905
+ * print(vec + vmath.vector3(2.0)) --> vmath.vector3(3, 4, 5)
906
+ * print(vec - vmath.vector3(2.0)) --> vmath.vector3(-1, 0, 1)
907
+ * ```
908
+ */
55
909
  function vector3(x: number, y: number, z: number): Vector3;
910
+ /**
911
+ * Creates a new zero vector with all components set to 0.
912
+ *
913
+ * @returns new zero vector
914
+ * @example
915
+ * ```lua
916
+ * local vec = vmath.vector4()
917
+ * print(vec) --> vmath.vector4(0, 0, 0, 0)
918
+ * print(vec.w) --> 0
919
+ * ```
920
+ */
56
921
  function vector4(): Vector4;
922
+ /**
923
+ * Creates a new vector with all components set to the
924
+ * supplied scalar value.
925
+ *
926
+ * @param n - scalar value to splat
927
+ * @returns new vector
928
+ * @example
929
+ * ```lua
930
+ * local vec = vmath.vector4(1.0)
931
+ * print(vec) --> vmath.vector4(1, 1, 1, 1)
932
+ * print(vec.w) --> 1
933
+ * ```
934
+ */
57
935
  function vector4(n: number): Vector4;
936
+ /**
937
+ * Creates a new vector with all components set to the
938
+ * corresponding values from the supplied vector. I.e.
939
+ * This function creates a copy of the given vector.
940
+ *
941
+ * @param v1 - existing vector
942
+ * @returns new vector
943
+ * @example
944
+ * ```lua
945
+ * local vect1 = vmath.vector4(1.0)
946
+ * local vect2 = vmath.vector4(vec1)
947
+ * if vec1 == vec2 then
948
+ * -- yes, they are equal
949
+ * print(vec2) --> vmath.vector4(1, 1, 1, 1)
950
+ * end
951
+ * ```
952
+ */
58
953
  function vector4(v1: Vector4): Vector4;
954
+ /**
955
+ * Creates a new vector with the components set to the
956
+ * supplied values.
957
+ *
958
+ * @param x - x coordinate
959
+ * @param y - y coordinate
960
+ * @param z - z coordinate
961
+ * @param w - w coordinate
962
+ * @returns new vector
963
+ * @example
964
+ * ```lua
965
+ * local vec = vmath.vector4(1.0, 2.0, 3.0, 4.0)
966
+ * print(vec) --> vmath.vector4(1, 2, 3, 4)
967
+ * print(-vec) --> vmath.vector4(-1, -2, -3, -4)
968
+ * print(vec * 2) --> vmath.vector4(2, 4, 6, 8)
969
+ * print(vec + vmath.vector4(2.0)) --> vmath.vector4(3, 4, 5, 6)
970
+ * print(vec - vmath.vector4(2.0)) --> vmath.vector4(-1, 0, 1, 2)
971
+ * ```
972
+ */
59
973
  function vector4(x: number, y: number, z: number, w: number): Vector4;
60
974
  }
61
975
  }