diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-01-19 13:39:40 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-01-19 13:39:40 -0500 |
commit | 76446f179d3de3b5635e8b4c40e1bd49ea99995c (patch) | |
tree | 9d79082ecde0866d27062f9b2d940f85caefbb21 /public/gl/controller | |
parent | merge (diff) | |
download | webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.tar.gz webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.tar.bz2 webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.zip |
cube working
Diffstat (limited to 'public/gl/controller')
-rw-r--r-- | public/gl/controller/SimpleController.js | 50 |
1 files changed, 50 insertions, 0 deletions
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 |