]> git.r.bdr.sh - rbdr/lissajous/blame - lib/systems/camera_rotator.js
Add a few comments
[rbdr/lissajous] / lib / systems / camera_rotator.js
CommitLineData
362f9116
RBR
1import { mat4, vec3 } from 'gl-matrix';
2import { System } from '@serpentity/serpentity';
3import Cameras from '../nodes/cameras';
4
32e2eed2
RBR
5/**
6 * Rotates the camera around a "sphere"
7 */
362f9116
RBR
8export default class CameraRotator extends System {
9
10 constructor() {
11
12 super();
13 }
14
15 added(engine){
16
17 this.cameras = engine.getNodes(Cameras);
18 }
19
20 removed(){
21
22 delete this.cameras;
23 }
24
25 update(dt){
26
27 for (const camera of this.cameras) {
28
29 let rotationMatrix = mat4.create();
30 mat4.rotateY(rotationMatrix, rotationMatrix, camera.angle.yaw);
31 mat4.rotateX(rotationMatrix, rotationMatrix, camera.angle.pitch);
32 mat4.rotateZ(rotationMatrix, rotationMatrix, camera.angle.roll);
33
34 let eye = vec3.fromValues(0, 0, camera.radius.radius);
35 vec3.transformMat4(eye, eye, rotationMatrix);
36
37 camera.position.x = eye[0];
38 camera.position.y = eye[1];
39 camera.position.z = eye[2];
40
41 let up = vec3.fromValues(0, 1, 0);
42 vec3.transformMat4(up, up, rotationMatrix);
43 camera.up.x = up[0];
44 camera.up.y = up[1];
45 camera.up.z = up[2];
46
6a87c253
RBR
47 camera.angle.pitch = (camera.angle.pitch + camera.velocity.x * dt / 500 + 2 * Math.PI) % (2 * Math.PI);
48 camera.angle.yaw = (camera.angle.yaw + camera.velocity.y * dt / 500 + 2 * Math.PI) % (2 * Math.PI);
49 camera.angle.roll = (camera.angle.roll + camera.velocity.z * dt / 500 + 2 * Math.PI) % (2 * Math.PI);
362f9116
RBR
50 }
51 }
52};