Skip to content

Commit

Permalink
Convert rotation functions to right hand rule & fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Incoherent-Code committed Nov 22, 2024
1 parent 36386a7 commit 2666584
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
40 changes: 21 additions & 19 deletions libraries/math/src/vector3/coreHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { Vector2, Vector3 } from '@minecraft/server';
import { describe, expect, it } from 'vitest';
import { Vector2Utils, VECTOR3_ONE, Vector3Utils } from './coreHelpers';
import { Vector2Utils, VECTOR3_LEFT, VECTOR3_UP, Vector3Utils } from './coreHelpers';

describe('Vector3 operations', () => {
const v1: Vector3 = { x: 1, y: 2, z: 3 };
Expand Down Expand Up @@ -192,27 +192,29 @@ describe('Vector3 operations', () => {

it('calculates two vectors multiplied together', () => {
const result: Vector3 = Vector3Utils.multiply(v1, v2);
expect(result).toEqual({ x: 4, y: 10, z: 18});
expect(result).toEqual({ x: 4, y: 10, z: 18 });
});

it(`calculates a vector rotated along the x axis`, () => {
const result = Vector3Utils.rotateX(VECTOR3_ONE, Math.PI / 2);
expect(result.x).toBeCloseTo(1);
expect(result.y).toBeCloseTo(1);
expect(result.z).toBeCloseTo(-1);
});
describe('Vector3 rotation functions', () => {
it(`calculates a vector rotated along the x axis`, () => {
const result = Vector3Utils.rotateX(VECTOR3_UP, Math.PI / 2);
expect(result.x).toBeCloseTo(0);
expect(result.y).toBeCloseTo(0);
expect(result.z).toBeCloseTo(1);
});

it(`calculates a vector rotated along the y axis`, () => {
const result = Vector3Utils.rotateY(VECTOR3_ONE, Math.PI / 4);
expect(result.x).toBeCloseTo(Math.sqrt(2));
expect(result.y).toBeCloseTo(1);
expect(result.z).toBeCloseTo(0);
});
it(`calculates a vector rotated along the y axis`, () => {
const result = Vector3Utils.rotateY(VECTOR3_LEFT, Math.PI / 2);
expect(result.x).toBeCloseTo(0);
expect(result.y).toBeCloseTo(0);
expect(result.z).toBeCloseTo(-1);
});

it(`calculates a vector rotated along the z axis`, () => {
const result = Vector3Utils.rotateZ(VECTOR3_ONE, (3 * Math.PI) / 4);
expect(result.x).toBeCloseTo(0);
expect(result.y).toBeCloseTo(-Math.sqrt(2));
expect(result.z).toBeCloseTo(1);
it(`calculates a vector rotated along the z axis`, () => {
const result = Vector3Utils.rotateZ(VECTOR3_UP, Math.PI / 2);
expect(result.x).toBeCloseTo(-1);
expect(result.y).toBeCloseTo(0);
expect(result.z).toBeCloseTo(0);
});
});
});
51 changes: 26 additions & 25 deletions libraries/math/src/vector3/coreHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,63 +161,64 @@ export class Vector3Utils {
}
/**
* multiply
*
* Multiplies two vectors together
*
* Element-wise multiplication of two vectors together.
* Not to be confused with {@link Vector3Utils.dot} product or {@link Vector3Utils.cross} product
*/
static multiply(a: Vector3, b: Vector3): Vector3 {
return {
x: a.x * b.x,
y: a.y * b.y,
z: a.z * b.z
return {
x: a.x * b.x,
y: a.y * b.y,
z: a.z * b.z,
};
}

/**
* rotateX
*
* Rotates the vector around the x axis
* @param a Angle in radians
*
* Rotates the vector around the x axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
static rotateX(v: Vector3, a: number): Vector3 {
let cos = Math.cos(a);
let sin = Math.sin(a);
return {
x: v.x,
y: v.y * cos + v.z * sin,
z: v.z * cos - v.y * sin
}
y: v.y * cos - v.z * sin,
z: v.z * cos + v.y * sin,
};
}

/**
* rotateY
*
* Rotates the vector around the y axis
* @param a Angle in radians
*
* Rotates the vector around the y axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
static rotateY(v: Vector3, a: number): Vector3 {
let cos = Math.cos(a);
let sin = Math.sin(a);
return {
x: v.x * cos + v.z * sin,
x: v.x * cos - v.z * sin,
y: v.y,
z: v.z * cos - v.x * sin
}
z: v.z * cos + v.x * sin,
};
}

/**
* rotateZ
*
* Rotates the vector around the z axis
* @param a Angle in radians
*
* Rotates the vector around the z axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
static rotateZ(v: Vector3, a: number): Vector3 {
let cos = Math.cos(a);
let sin = Math.sin(a);
return {
x: v.x * cos + v.y * sin,
y: v.y * cos - v.x * sin,
z: v.z
}
x: v.x * cos - v.y * sin,
y: v.y * cos + v.x * sin,
z: v.z,
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/math/src/vector3/vectorWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('Vector3Builder', () => {
const resultB = vectorA.rotateY(angle);
expect(resultA).toEqual(resultB);
});

it('should be able to rotate over z with the same result as the coreHelpers function', () => {
const vectorA = new Vector3Builder(5, 6, 3);
const angle = Math.PI / 2;
Expand Down
26 changes: 13 additions & 13 deletions libraries/math/src/vector3/vectorWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class Vector3Builder implements Vector3 {

/**
* multiply
*
*
* Multiplies two vectors together
*/
multiply(vec: Vector3): Vector3 {
Expand All @@ -179,32 +179,32 @@ export class Vector3Builder implements Vector3 {

/**
* rotateX
*
* Rotates the vector around the x axis
* @param a Angle in radians
*
* Rotates the vector around the x axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
rotateX(a: number): Vector3 {
return this.assign(Vector3Utils.rotateX(this, a))
return this.assign(Vector3Utils.rotateX(this, a));
}

/**
* rotateY
*
* Rotates the vector around the y axis
* @param a Angle in radians
*
* Rotates the vector around the y axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
rotateY(a: number): Vector3 {
return this.assign(Vector3Utils.rotateY(this, a))
return this.assign(Vector3Utils.rotateY(this, a));
}

/**
* rotateZ
*
* Rotates the vector around the z axis
* @param a Angle in radians
*
* Rotates the vector around the z axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
rotateZ(a: number): Vector3 {
return this.assign(Vector3Utils.rotateZ(this, a))
return this.assign(Vector3Utils.rotateZ(this, a));
}
}

Expand Down

0 comments on commit 2666584

Please sign in to comment.