-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.look-in-vector-direction.spec.js
82 lines (61 loc) · 2.7 KB
/
physics.look-in-vector-direction.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { lookInVectorDirection } from "./physics.js";
const sinCos45deg = 0.7071;
describe("physics", () => {
describe("lookInVectorDirection()", () => {
it("rotates π/2 rad around the y-axis when the vector is pointing right", () => {
const vector = { x: 1.0, y: 0.0, z: 0.0 };
const object = withoutRotation();
lookInVectorDirection(object, vector);
expect(object.rotation.x).toBe(0);
expect(object.rotation.y).toBe(Math.PI / 2.0);
expect(object.rotation.z).toBe(0);
});
it("rotates π/4 rad around the y-axis when the vector is pointing right and forward", () => {
const vector = { x: sinCos45deg, y: 0.0, z: sinCos45deg };
const object = withoutRotation();
lookInVectorDirection(object, vector);
expect(object.rotation.x).toBe(0);
expect(round4(object.rotation.y)).toBe(round4(Math.PI / 4.0));
expect(object.rotation.z).toBe(0);
});
it("rotates 3π/4 rad around the y-axis when the vector is pointing right and back", () => {
const vector = { x: sinCos45deg, y: 0.0, z: -sinCos45deg };
const object = withoutRotation();
lookInVectorDirection(object, vector);
expect(object.rotation.x).toBe(0);
expect(round4(object.rotation.y)).toBe(round4((3.0 * Math.PI) / 4.0));
expect(object.rotation.z).toBe(0);
});
it("rotates 5π/4 rad around the y-axis when the vector is pointing left and back", () => {
const vector = { x: -sinCos45deg, y: 0.0, z: -sinCos45deg };
const object = withoutRotation();
lookInVectorDirection(object, vector);
expect(object.rotation.x).toBe(0);
expect(round4(object.rotation.y)).toBe(round4((5.0 * Math.PI) / 4.0));
expect(object.rotation.z).toBe(0);
});
it("rotates 7π/4 rad around the y-axis when the vector is pointing left and forward", () => {
const vector = { x: -sinCos45deg, y: 0.0, z: sinCos45deg };
const object = withoutRotation();
lookInVectorDirection(object, vector);
expect(object.rotation.x).toBe(0);
expect(object.rotation.y).toBe((7.0 * Math.PI) / 4.0);
expect(object.rotation.z).toBe(0);
});
it("doesn't update rotation when vector length is 0", () => {
const vector = { x: 0.0, y: 0.0, z: 0.0 };
const initialRotation = Math.PI / 7.0;
const object = { rotation: { x: 0.0, y: initialRotation, z: 0.0 } };
lookInVectorDirection(object, vector);
expect(object.rotation.x).toBe(0);
expect(object.rotation.y).toBe(initialRotation);
expect(object.rotation.z).toBe(0);
});
});
});
function withoutRotation() {
return { rotation: { x: 0.0, y: 0.0, z: 0.0 } };
}
function round4(n) {
return Math.round(n * 10000.0) / 10000.0;
}