Input.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. //start function called when component attached to the node, after constructor
  11. self.start = function() {
  12. Atomic.audio.listener = node.getComponent("SoundListener");
  13. };
  14. //update function called once per each frame
  15. self.update = function(timeStep) {
  16. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  17. var mouseMove = Atomic.input.mouseMove;
  18. // if we are on mobile
  19. if(Atomic.platform == "Android" || Atomic.platform == "iOS") {
  20. //iterate through each TouchState, if it doesn't touch any widgets, use it as a `mouse`
  21. for(var i = 0; i < Atomic.input.getNumTouches(); i++) {
  22. var touchState = Atomic.input.getTouch(i);
  23. if(touchState.touchedWidget == null) {
  24. var delta = touchState.delta;
  25. mouseMove[0] = delta[0];
  26. mouseMove[1] = delta[1];
  27. }
  28. }
  29. }
  30. yaw += MOUSE_SENSITIVITY * mouseMove[0];
  31. pitch += MOUSE_SENSITIVITY * mouseMove[1];
  32. if (pitch < -90)
  33. pitch = 90;
  34. if (pitch > 90)
  35. pitch = 90;
  36. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  37. node.rotation = QuatFromEuler(pitch, yaw, 0.0);
  38. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  39. if (Atomic.input.getKeyDown(Atomic.KEY_W) || Atomic.input.getKeyDown(Atomic.KEY_UP))
  40. node.translate([0, 0, MOVE_SPEED * timeStep]);
  41. if (Atomic.input.getKeyDown(Atomic.KEY_S) || Atomic.input.getKeyDown(Atomic.KEY_DOWN))
  42. node.translate([0, 0, -MOVE_SPEED * timeStep]);
  43. if (Atomic.input.getKeyDown(Atomic.KEY_D) || Atomic.input.getKeyDown(Atomic.KEY_RIGHT))
  44. node.translate([MOVE_SPEED * timeStep, 0, 0]);
  45. if (Atomic.input.getKeyDown(Atomic.KEY_A) || Atomic.input.getKeyDown(Atomic.KEY_LEFT))
  46. node.translate([-MOVE_SPEED * timeStep, 0, 0]);
  47. };
  48. };
  49. //Math function to get Quaternion from Euler angles
  50. function QuatFromEuler(x, y, z) {
  51. var M_PI = 3.14159265358979323846264338327950288;
  52. var q = [0, 0, 0, 0];
  53. x *= (M_PI / 360);
  54. y *= (M_PI / 360);
  55. z *= (M_PI / 360);
  56. var sinX = Math.sin(x);
  57. var cosX = Math.cos(x);
  58. var sinY = Math.sin(y);
  59. var cosY = Math.cos(y);
  60. var sinZ = Math.sin(z);
  61. var cosZ = Math.cos(z);
  62. q[0] = cosY * cosX * cosZ + sinY * sinX * sinZ;
  63. q[1] = cosY * sinX * cosZ + sinY * cosX * sinZ;
  64. q[2] = sinY * cosX * cosZ - cosY * sinX * sinZ;
  65. q[3] = cosY * cosX * sinZ - sinY * sinX * cosZ;
  66. return q;
  67. }
  68. exports.component = component;