Input.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "atomic component";
  2. var component = function(self) {
  3. var yaw = 0;
  4. var pitch = 0;
  5. var node = self.node;
  6. // Movement speed as world units per second
  7. var MOVE_SPEED = 20.0;
  8. // Mouse sensitivity as degrees per pixel
  9. var MOUSE_SENSITIVITY = 0.1;
  10. self.start = function() {
  11. Atomic.audio.listener = node.getComponent("SoundListener");
  12. }
  13. self.update = function(timeStep) {
  14. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  15. var mouseMove = Atomic.input.mouseMove;
  16. yaw += MOUSE_SENSITIVITY * mouseMove[0];
  17. pitch += MOUSE_SENSITIVITY * mouseMove[1];
  18. if (pitch < -90)
  19. pitch = 90;
  20. if (pitch > 90)
  21. pitch = 90;
  22. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  23. node.rotation = QuatFromEuler(pitch, yaw, 0.0);
  24. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  25. if (Atomic.input.getKeyDown(Atomic.KEY_W))
  26. node.translate([0, 0, MOVE_SPEED * timeStep]);
  27. if (Atomic.input.getKeyDown(Atomic.KEY_S))
  28. node.translate([0, 0, -MOVE_SPEED * timeStep]);
  29. if (Atomic.input.getKeyDown(Atomic.KEY_D))
  30. node.translate([MOVE_SPEED * timeStep, 0, 0]);
  31. if (Atomic.input.getKeyDown(Atomic.KEY_A))
  32. node.translate([-MOVE_SPEED * timeStep, 0, 0]);
  33. }
  34. }
  35. function QuatFromEuler(x, y, z) {
  36. var M_PI = 3.14159265358979323846264338327950288;
  37. var q = [0, 0, 0, 0];
  38. x *= (M_PI / 360);
  39. y *= (M_PI / 360);
  40. z *= (M_PI / 360);
  41. var sinX = Math.sin(x);
  42. var cosX = Math.cos(x);
  43. var sinY = Math.sin(y);
  44. var cosY = Math.cos(y);
  45. var sinZ = Math.sin(z);
  46. var cosZ = Math.cos(z);
  47. q[0] = cosY * cosX * cosZ + sinY * sinX * sinZ;
  48. q[1] = cosY * sinX * cosZ + sinY * cosX * sinZ;
  49. q[2] = sinY * cosX * cosZ - cosY * sinX * sinZ;
  50. q[3] = cosY * cosX * sinZ - sinY * sinX * cosZ;
  51. return q;
  52. }
  53. exports.component = component;