import { mat4, vec3 } from 'gl-matrix'; import { System } from '@serpentity/serpentity'; import Cameras from '../nodes/cameras'; /** * Rotates the camera around a "sphere" */ export default class CameraRotator extends System { constructor() { super(); } added(engine){ this.cameras = engine.getNodes(Cameras); } removed(){ delete this.cameras; } update(dt){ for (const camera of this.cameras) { let rotationMatrix = mat4.create(); mat4.rotateY(rotationMatrix, rotationMatrix, camera.angle.yaw); mat4.rotateX(rotationMatrix, rotationMatrix, camera.angle.pitch); mat4.rotateZ(rotationMatrix, rotationMatrix, camera.angle.roll); let eye = vec3.fromValues(0, 0, camera.radius.radius); vec3.transformMat4(eye, eye, rotationMatrix); camera.position.x = eye[0]; camera.position.y = eye[1]; camera.position.z = eye[2]; let up = vec3.fromValues(0, 1, 0); vec3.transformMat4(up, up, rotationMatrix); camera.up.x = up[0]; camera.up.y = up[1]; camera.up.z = up[2]; camera.angle.pitch = (camera.angle.pitch + camera.velocity.x * dt / 500 + 2 * Math.PI) % (2 * Math.PI); camera.angle.yaw = (camera.angle.yaw + camera.velocity.y * dt / 500 + 2 * Math.PI) % (2 * Math.PI); camera.angle.roll = (camera.angle.roll + camera.velocity.z * dt / 500 + 2 * Math.PI) % (2 * Math.PI); } } };