Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Maths from '../core/Maths'
- import Camera from './Camera'
- import {vec2, vec3, mat4, glMatrix} from 'gl-matrix'
- export default class FlyingCamera extends Camera {
- constructor(scene) {
- super(scene);
- this.position = vec3.create();
- this.rotation = vec3.create();
- this.velocity = vec3.create();
- this.friction = vec3.fromValues(.9, .9, .9);
- this.positionSpeed = 0.1;
- this.rotationSpeed = 0.1;
- this.wheelPrecision = 0.5;
- scene.engine.input.on('mousewheel', delta => {
- this.positionSpeed += delta * this.wheelPrecision;
- if (this.positionSpeed < 0)
- this.positionSpeed = 0;
- });
- scene.engine.input.on('mousemove', (position, delta) => {
- this.pointer(delta);
- })
- }
- update() {
- this.move();
- this.view();
- }
- move() {
- let direction = vec3.create();
- let moveSpeed = this.positionSpeed * this.scene.engine.clock.deltaTime;
- let rotSpeed = this.rotationSpeed * this.scene.engine.clock.deltaTime * 10;
- if (this.keys.left
- || this.keys.right
- || this.keys.shift
- || this.keys.space
- || this.keys.up
- || this.keys.down)
- this.emit('playermoved');
- if (this.keys.left) direction[0] += moveSpeed;
- if (this.keys.right) direction[0] -= moveSpeed;
- if (this.keys.shift) direction[1] += moveSpeed;
- if (this.keys.space) direction[1] -= moveSpeed;
- if (this.keys.up) direction[2] += moveSpeed;
- if (this.keys.down) direction[2] -= moveSpeed;
- if (this.keys.arrow_up) this.rotation[0] -= rotSpeed;
- if (this.keys.arrow_down) this.rotation[0] += rotSpeed;
- if (this.keys.arrow_left) this.rotation[1] -= rotSpeed;
- if (this.keys.arrow_right) this.rotation[1] += rotSpeed;
- this.limitRotation();
- mat4.identity(this.viewMatrix);
- mat4.rotateX(this.viewMatrix, this.viewMatrix, this.rotation[0]);
- mat4.rotateY(this.viewMatrix, this.viewMatrix, this.rotation[1]);
- mat4.invert(this.viewMatrix, this.viewMatrix);
- Maths.mat4MulVec3(this.viewMatrix, direction);
- vec3.add(this.velocity, this.velocity, direction);
- vec3.mul(this.velocity, this.velocity, this.friction);
- vec3.add(this.position, this.position, this.velocity);
- }
- view() {
- mat4.identity(this.viewMatrix);
- mat4.rotateX(this.viewMatrix, this.viewMatrix, this.rotation[0]);
- mat4.rotateY(this.viewMatrix, this.viewMatrix, this.rotation[1]);
- mat4.translate(this.viewMatrix, this.viewMatrix, this.position);
- }
- limitRotation() {
- if (this.rotation[1] < 0) this.rotation[1] += Math.PI * 2;
- if (this.rotation[1] >= Math.PI * 2) this.rotation[1] -= Math.PI * 2;
- if (this.rotation[0] < -Math.PI / 2) this.rotation[0] = -Math.PI / 2;
- if (this.rotation[0] > Math.PI / 2) this.rotation[0] = Math.PI / 2;
- }
- pointer(delta) {
- vec2.add(this.mouse, this.mouse, delta);
- this.rotation[1] += glMatrix.toRadian(this.mouse[0]) * this.rotationSpeed;
- this.rotation[0] += glMatrix.toRadian(this.mouse[1]) * this.rotationSpeed;
- this.limitRotation();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement