AvatarController.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /// <reference path="../TypeScript/Atomic.d.ts"/>
  2. /// <reference path="../TypeScript/AtomicWork.d.ts" />
  3. /// <reference path="../Modules/gl-matrix.d.ts" />
  4. var __extends = (this && this.__extends) || function (d, b) {
  5. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. __.prototype = b.prototype;
  8. d.prototype = new __();
  9. };
  10. var gl_matrix_1 = require('gl-matrix');
  11. "atomic component";
  12. var MOVE_FORCE = 1.8;
  13. var BRAKE_FORCE = 0.2;
  14. var YAW_SENSITIVITY = 0.1;
  15. var PITCH_SENSITIVITY = 0.1;
  16. var AvatarController = (function (_super) {
  17. __extends(AvatarController, _super);
  18. function AvatarController() {
  19. _super.apply(this, arguments);
  20. this.inspectorFields = {
  21. speed: 1.0,
  22. cameraDist: 12
  23. };
  24. this.yaw = 0;
  25. this.pitch = 0;
  26. this.mouseMoveX = 0;
  27. this.mouseMoveY = 0;
  28. this.moveForward = false;
  29. this.moveBackwards = false;
  30. this.moveLeft = false;
  31. this.moveRight = false;
  32. this.idle = true;
  33. this.speed = 1;
  34. this.cameraDist = 12;
  35. }
  36. AvatarController.prototype.start = function () {
  37. this.camera = this.node.scene.getMainCamera();
  38. this.cameraNode = this.camera.node;
  39. this.body = this.node.createComponent("RigidBody");
  40. this.body.mass = 1.0;
  41. this.body.angularFactor = [0, 0, 0];
  42. this.body.collisionEventMode = Atomic.COLLISION_ALWAYS;
  43. var shape = this.node.createComponent("CollisionShape");
  44. shape.setCapsule(2, 4, [0, 2, 0]);
  45. };
  46. AvatarController.prototype.update = function (timeStep) {
  47. this.updateControls();
  48. };
  49. AvatarController.prototype.fixedUpdate = function (timestep) {
  50. var rot = this.node.getRotation();
  51. var moveDir = [0, 0, 0];
  52. var velocity = this.body.getLinearVelocity();
  53. var planeVelocity = [velocity[0], 0.0, velocity[2]];
  54. if (this.moveForward) {
  55. gl_matrix_1.vec3.add(moveDir, moveDir, [0, 0, 1]);
  56. }
  57. if (this.moveBackwards) {
  58. gl_matrix_1.vec3.add(moveDir, moveDir, [0, 0, -1]);
  59. }
  60. if (this.moveLeft) {
  61. gl_matrix_1.vec3.add(moveDir, moveDir, [-1, 0, 0]);
  62. }
  63. if (this.moveRight) {
  64. gl_matrix_1.vec3.add(moveDir, moveDir, [1, 0, 0]);
  65. }
  66. if (gl_matrix_1.vec3.length(moveDir) > 0.0)
  67. gl_matrix_1.vec3.normalize(moveDir, moveDir);
  68. gl_matrix_1.vec3.transformQuat(moveDir, moveDir, [rot[1], rot[2], rot[3], rot[0]]);
  69. gl_matrix_1.vec3.scale(moveDir, moveDir, (MOVE_FORCE));
  70. gl_matrix_1.vec3.scale(moveDir, moveDir, this.speed);
  71. this.body.applyImpulse(moveDir);
  72. gl_matrix_1.vec3.negate(planeVelocity, planeVelocity);
  73. gl_matrix_1.vec3.scale(planeVelocity, planeVelocity, BRAKE_FORCE);
  74. this.body.applyImpulse(planeVelocity);
  75. if (gl_matrix_1.vec3.length(moveDir) > 0.0)
  76. this.idle = false;
  77. else
  78. this.idle = true;
  79. };
  80. AvatarController.prototype.updateControls = function () {
  81. var input = Atomic.input;
  82. this.moveForward = false;
  83. this.moveBackwards = false;
  84. this.moveLeft = false;
  85. this.moveRight = false;
  86. this.mouseMoveX = 0.0;
  87. this.mouseMoveY = 0.0;
  88. var MOVE_SPEED = 20.0;
  89. var MOUSE_SENSITIVITY = 0.1;
  90. if (input.getKeyDown(Atomic.KEY_W))
  91. this.moveForward = true;
  92. if (input.getKeyDown(Atomic.KEY_S))
  93. this.moveBackwards = true;
  94. if (input.getKeyDown(Atomic.KEY_A))
  95. this.moveLeft = true;
  96. if (input.getKeyDown(Atomic.KEY_D))
  97. this.moveRight = true;
  98. this.yaw += input.mouseMoveX * YAW_SENSITIVITY;
  99. this.pitch += input.mouseMoveY * PITCH_SENSITIVITY;
  100. if (this.pitch < -80)
  101. this.pitch = -80;
  102. if (this.pitch > 80)
  103. this.pitch = 80;
  104. };
  105. AvatarController.prototype.postUpdate = function (timestep) {
  106. var rot = this.node.getRotation();
  107. var dir = gl_matrix_1.quat.create();
  108. gl_matrix_1.quat.setAxisAngle(dir, [1, 0, 0], (this.pitch * Math.PI / 180.0));
  109. gl_matrix_1.quat.multiply(dir, [rot[1], rot[2], rot[3], rot[0]], dir);
  110. var headNode = this.node.getChild("Head_Tip", true);
  111. var aimPoint = this.node.getWorldPosition();
  112. var aimOffset = [0, 1.7, 0];
  113. gl_matrix_1.vec3.transformQuat(aimOffset, aimOffset, dir);
  114. gl_matrix_1.vec3.add(aimPoint, aimPoint, aimOffset);
  115. var rayDir = gl_matrix_1.vec3.create();
  116. gl_matrix_1.vec3.transformQuat(rayDir, [0, 0, -1], dir);
  117. gl_matrix_1.vec3.scale(rayDir, rayDir, this.cameraDist);
  118. gl_matrix_1.vec3.add(aimPoint, aimPoint, rayDir);
  119. this.cameraNode.setPosition(aimPoint);
  120. this.cameraNode.setRotation([dir[3], dir[0], dir[1], dir[2]]);
  121. gl_matrix_1.quat.setAxisAngle(dir, [0, 1, 0], (this.yaw * Math.PI / 180.0));
  122. this.node.setRotation([dir[3], dir[0], dir[1], dir[2]]);
  123. };
  124. return AvatarController;
  125. })(Atomic.JSComponent);
  126. function QuatFromEuler(x, y, z) {
  127. var M_PI = 3.14159265358979323846264338327950288;
  128. var q = [0, 0, 0, 0];
  129. x *= (M_PI / 360);
  130. y *= (M_PI / 360);
  131. z *= (M_PI / 360);
  132. var sinX = Math.sin(x);
  133. var cosX = Math.cos(x);
  134. var sinY = Math.sin(y);
  135. var cosY = Math.cos(y);
  136. var sinZ = Math.sin(z);
  137. var cosZ = Math.cos(z);
  138. q[0] = cosY * cosX * cosZ + sinY * sinX * sinZ;
  139. q[1] = cosY * sinX * cosZ + sinY * cosX * sinZ;
  140. q[2] = sinY * cosX * cosZ - cosY * sinX * sinZ;
  141. q[3] = cosY * cosX * sinZ - sinY * sinX * cosZ;
  142. return q;
  143. }
  144. module.exports = AvatarController;