Skip to content

Commit

Permalink
feat: translate w/ direction
Browse files Browse the repository at this point in the history
  • Loading branch information
ydcjeff committed Dec 14, 2023
1 parent fcc7fc0 commit 7c581ad
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/objects/space_entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,33 @@ export abstract class SpaceEntity {
/**
* Translates object relative to its direction
*/
translate(delta: Vector3): void {
throw new Error('not implemented');
translateX(delta: Vector3) {
this.#translate(delta, 'x');
}

translateZ(delta: Vector3) {
this.#translate(delta, 'z');
}

#translate(delta: Vector3, direction: 'x' | 'z') {
let theta = Math.atan(this.#direction.x / this.#direction.z);
// make angle always positive
if (theta < 0) {
theta = theta * -1;
}
if (direction === 'x') {
const direction = this.#direction.clone();
if (!direction.x) {
direction.x = 1;
}
delta.x = delta.x * Math.cos(theta) * (direction.x > 0 ? 1 : -1);
delta.z = delta.x * Math.sin(theta);
this.#position.add(Vector3.multiply(delta, direction));
}
else if (direction === 'z') {
delta.x = delta.z * Math.sin(theta);
delta.z = -delta.z * Math.cos(theta);
this.#position.add(Vector3.multiply(delta, this.#direction));
}
}
}

0 comments on commit 7c581ad

Please sign in to comment.