From 76446f179d3de3b5635e8b4c40e1bd49ea99995c Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Thu, 19 Jan 2023 13:39:40 -0500 Subject: cube working --- public/gl/controller/SimpleController.js | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 public/gl/controller/SimpleController.js (limited to 'public/gl/controller') diff --git a/public/gl/controller/SimpleController.js b/public/gl/controller/SimpleController.js new file mode 100644 index 0000000..b0e2938 --- /dev/null +++ b/public/gl/controller/SimpleController.js @@ -0,0 +1,50 @@ +import { Input } from '../io/Input.js' +import { M } from '../math/Math.js' +import { Vec3 } from '../math/Vec3.js' + +export class SimpleController { + + constructor(camera) { + this.camera = camera + this.lookSpeed = 100 + this.moveSpeed = 10 + } + + update(dt) { + var press = Input.isKeyDown + var rotate = new Vec3(); + if (press('ArrowRight')) rotate.y += 1; + if (press('ArrowLeft')) rotate.y -= 1; + if (press('ArrowUp')) rotate.x -= 1; + if (press('ArrowDown')) rotate.x += 1; + if(rotate.dot(rotate) > Number.EPSILON) { + var normal = rotate.normalize() + this.camera.rotation.x += this.lookSpeed * dt * normal.x; + this.camera.rotation.y += this.lookSpeed * dt * normal.y; + this.camera.rotation.z += this.lookSpeed * dt * normal.z; + } + this.camera.rotation.x = M.clamp(this.camera.rotation.x, -90, 90) + this.camera.rotation.y = this.camera.rotation.y % 360 + + const yaw = this.camera.rotation.y * (Math.PI/180); + var forward = new Vec3(Math.sin(yaw), 0, Math.cos(yaw)) + var left = new Vec3(-forward.z, 0, forward.x) + var up = new Vec3(0, 1, 0) + + var move = new Vec3(); + if (press('w')) move.add(forward) + if (press('s')) move.sub(forward) + if (press('a')) move.add(left) + if (press('d')) move.sub(left) + if (press('q')) move.sub(up) + if (press('e')) move.add(up) + + if(move.dot(move) > Number.EPSILON) { + var normal = move.normalize() + this.camera.position.x += this.moveSpeed * dt * normal.x; + this.camera.position.y += this.moveSpeed * dt * normal.y; + this.camera.position.z += this.moveSpeed * dt * normal.z; + } + } + +} \ No newline at end of file -- cgit v1.2.3-freya