|
3 | 3 | * @author David Gossow - [email protected]
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -import Quaternion from './Quaternion.js'; |
| 6 | +import { type IQuaternion } from './Quaternion.js'; |
| 7 | +import { type PartialNullable } from '../types/interface-types.js'; |
| 8 | + |
| 9 | +export interface IVector3 { |
| 10 | + /** |
| 11 | + * The x value. |
| 12 | + */ |
| 13 | + x: number; |
| 14 | + /** |
| 15 | + * The y value. |
| 16 | + */ |
| 17 | + y: number; |
| 18 | + /** |
| 19 | + * The z value. |
| 20 | + */ |
| 21 | + z: number; |
| 22 | +} |
7 | 23 |
|
8 | 24 | /**
|
9 | 25 | * A 3D vector.
|
10 | 26 | */
|
11 |
| -export default class Vector3 { |
12 |
| - /** |
13 |
| - * @param {Object} [options] |
14 |
| - * @param {number} [options.x=0] - The x value. |
15 |
| - * @param {number} [options.y=0] - The y value. |
16 |
| - * @param {number} [options.z=0] - The z value. |
17 |
| - */ |
18 |
| - constructor(options) { |
19 |
| - options = options || {}; |
20 |
| - this.x = options.x || 0; |
21 |
| - this.y = options.y || 0; |
22 |
| - this.z = options.z || 0; |
| 27 | +export default class Vector3 implements IVector3 { |
| 28 | + x: number; |
| 29 | + y: number; |
| 30 | + z: number; |
| 31 | + |
| 32 | + constructor(options?: PartialNullable<IVector3> | null) { |
| 33 | + this.x = options?.x ?? 0; |
| 34 | + this.y = options?.y ?? 0; |
| 35 | + this.z = options?.z ?? 0; |
23 | 36 | }
|
| 37 | + |
24 | 38 | /**
|
25 | 39 | * Set the values of this vector to the sum of itself and the given vector.
|
26 | 40 | *
|
27 | 41 | * @param {Vector3} v - The vector to add with.
|
28 | 42 | */
|
29 |
| - add(v) { |
| 43 | + add(v: IVector3): void { |
30 | 44 | this.x += v.x;
|
31 | 45 | this.y += v.y;
|
32 | 46 | this.z += v.z;
|
33 | 47 | }
|
| 48 | + |
34 | 49 | /**
|
35 | 50 | * Set the values of this vector to the difference of itself and the given vector.
|
36 | 51 | *
|
37 | 52 | * @param {Vector3} v - The vector to subtract with.
|
38 | 53 | */
|
39 |
| - subtract(v) { |
| 54 | + subtract(v: IVector3): void { |
40 | 55 | this.x -= v.x;
|
41 | 56 | this.y -= v.y;
|
42 | 57 | this.z -= v.z;
|
43 | 58 | }
|
| 59 | + |
44 | 60 | /**
|
45 | 61 | * Multiply the given Quaternion with this vector.
|
46 | 62 | *
|
47 | 63 | * @param {Quaternion} q - The quaternion to multiply with.
|
48 | 64 | */
|
49 |
| - multiplyQuaternion(q) { |
50 |
| - var ix = q.w * this.x + q.y * this.z - q.z * this.y; |
51 |
| - var iy = q.w * this.y + q.z * this.x - q.x * this.z; |
52 |
| - var iz = q.w * this.z + q.x * this.y - q.y * this.x; |
53 |
| - var iw = -q.x * this.x - q.y * this.y - q.z * this.z; |
| 65 | + multiplyQuaternion(q: IQuaternion) { |
| 66 | + const ix = q.w * this.x + q.y * this.z - q.z * this.y; |
| 67 | + const iy = q.w * this.y + q.z * this.x - q.x * this.z; |
| 68 | + const iz = q.w * this.z + q.x * this.y - q.y * this.x; |
| 69 | + const iw = -q.x * this.x - q.y * this.y - q.z * this.z; |
54 | 70 | this.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;
|
55 | 71 | this.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;
|
56 | 72 | this.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;
|
57 | 73 | }
|
| 74 | + |
58 | 75 | /**
|
59 | 76 | * Clone a copy of this vector.
|
60 | 77 | *
|
61 | 78 | * @returns {Vector3} The cloned vector.
|
62 | 79 | */
|
63 |
| - clone() { |
| 80 | + clone(): Vector3 { |
64 | 81 | return new Vector3(this);
|
65 | 82 | }
|
66 | 83 | }
|
0 commit comments