AvatarController.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Atomic Component
  2. var glmatrix = require("gl-matrix");
  3. var quat = glmatrix.quat;
  4. var vec3 = glmatrix.vec3;
  5. var game = Atomic.game;
  6. var node = self.node;
  7. var onGround = true;
  8. var okToJump = true;
  9. var inAirTime = 0;
  10. var MOVE_FORCE = 1.8;
  11. var INAIR_MOVE_FORCE = 0.02;
  12. var BRAKE_FORCE = 0.2;
  13. var JUMP_FORCE = 7.0;
  14. var YAW_SENSITIVITY = 0.1;
  15. var INAIR_THRESHOLD_TIME = 0.1;
  16. var cameraMode = 0;
  17. var yaw = 0;
  18. var pitch = 0;
  19. var moveForward = false;
  20. var moveBackwards = false;
  21. var moveLeft = false;
  22. var moveRight = false;
  23. var mouseMoveX = 0.0;
  24. var mouseMoveY = 0.0;
  25. var button0 = false;
  26. var button1 = false;
  27. var lastButton0 = false;
  28. var lastButton1 = false;
  29. self.idle = true;
  30. function start() {
  31. // Create rigidbody, and set non-zero mass so that the body becomes dynamic
  32. var body = node.createComponent("RigidBody");
  33. body.mass = 1.0;
  34. // Set zero angular factor so that physics doesn't turn the character on its own.
  35. // Instead we will control the character yaw manually
  36. body.angularFactor = [0, 0, 0];
  37. // Set the rigidbody to signal collision also when in rest, so that we get ground collisions properly
  38. body.collisionEventMode = Atomic.COLLISION_ALWAYS;
  39. // Set a capsule shape for collision
  40. var shape = node.createComponent("CollisionShape");
  41. shape.setCapsule(2, 4, [0, 2, 0]);
  42. }
  43. function fixedUpdate(timestep) {
  44. var body = node.getComponent("RigidBody");
  45. // Update the in air timer. Reset if grounded
  46. if (!onGround)
  47. inAirTimer += timeStep;
  48. else
  49. inAirTimer = 0.0;
  50. // When character has been in air less than 1/10 second, it's still interpreted as being on ground
  51. var softGrounded = inAirTimer < INAIR_THRESHOLD_TIME;
  52. var rot = node.getRotation();
  53. var moveDir = [0, 0, 0];
  54. // Update movement & animation
  55. var velocity = body.getLinearVelocity();
  56. // Velocity on the XZ plane
  57. var planeVelocity = [velocity[0], 0.0, velocity[2]];
  58. if (cameraMode != 2) {
  59. if (moveForward) {
  60. vec3.add(moveDir, moveDir, [0, 0, 1])
  61. }
  62. if (moveBackwards) {
  63. vec3.add(moveDir, moveDir, [0, 0, -1])
  64. }
  65. if (moveLeft) {
  66. vec3.add(moveDir, moveDir, [-1, 0, 0])
  67. }
  68. if (moveRight) {
  69. vec3.add(moveDir, moveDir, [1, 0, 0])
  70. }
  71. }
  72. if (vec3.length(moveDir) > 0.0)
  73. vec3.normalize(moveDir, moveDir);
  74. vec3.transformQuat(moveDir, moveDir, [rot[1], rot[2], rot[3], rot[0]]);
  75. vec3.scale(moveDir, moveDir, (softGrounded ? MOVE_FORCE : INAIR_MOVE_FORCE));
  76. body.applyImpulse(moveDir);
  77. if (softGrounded) {
  78. // When on ground, apply a braking force to limit maximum ground velocity
  79. vec3.negate(planeVelocity, planeVelocity);
  80. vec3.scale(planeVelocity, planeVelocity, BRAKE_FORCE);
  81. body.applyImpulse(planeVelocity);
  82. // Jump. Must release jump control inbetween jumps
  83. if (button1) {
  84. if (okToJump) {
  85. var jumpforce = [0, 1, 0];
  86. vec3.scale(jumpforce, jumpforce, JUMP_FORCE);
  87. body.applyImpulse(jumpforce);
  88. okToJump = false;
  89. }
  90. } else
  91. okToJump = true;
  92. }
  93. if (softGrounded && vec3.length(moveDir) > 0.0)
  94. self.idle = false;
  95. else
  96. self.idle = true;
  97. // Reset grounded flag for next frame
  98. onGround = true;
  99. }
  100. function MoveCamera(timeStep) {
  101. // Movement speed as world units per second
  102. var MOVE_SPEED = 10.0;
  103. // Mouse sensitivity as degrees per pixel
  104. var MOUSE_SENSITIVITY = 0.1;
  105. yaw = yaw + MOUSE_SENSITIVITY * mouseMoveX;
  106. pitch = pitch + MOUSE_SENSITIVITY * mouseMoveY;
  107. if (pitch < -90)
  108. pitch = -90;
  109. if (pitch > 90)
  110. pitch = 90;
  111. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  112. var cameraNode = game.cameraNode;
  113. cameraNode.rotation = QuatFromEuler(pitch, yaw, 0.0);
  114. var speed = MOVE_SPEED * timeStep;
  115. if (moveForward)
  116. cameraNode.translate([0.0, 0.0, speed])
  117. if (moveBackwards)
  118. cameraNode.translate([0.0, 0.0, -speed])
  119. if (moveLeft)
  120. cameraNode.translate([-speed, 0.0, 0.0])
  121. if (moveRight)
  122. cameraNode.translate([speed, 0.0, 0.0])
  123. }
  124. function UpdateControls() {
  125. var input = game.input;
  126. moveForward = false;
  127. moveBackwards = false;
  128. moveLeft = false;
  129. moveRight = false;
  130. mouseMoveX = 0.0;
  131. mouseMoveY = 0.0;
  132. button0 = false;
  133. button1 = false;
  134. // Movement speed as world units per second
  135. var MOVE_SPEED = 20.0;
  136. // Mouse sensitivity as degrees per pixel
  137. var MOUSE_SENSITIVITY = 0.1;
  138. if (input.getKeyDown(Atomic.KEY_W))
  139. moveForward = true;
  140. if (input.getKeyDown(Atomic.KEY_S))
  141. moveBackwards = true;
  142. if (input.getKeyDown(Atomic.KEY_A))
  143. moveLeft = true;
  144. if (input.getKeyDown(Atomic.KEY_D))
  145. moveRight = true;
  146. if (input.getKeyPress(Atomic.KEY_F))
  147. button0 = true;
  148. if (input.getKeyPress(Atomic.KEY_SPACE))
  149. button1 = true;
  150. mouseMoveX = input.getMouseMoveX();
  151. mouseMoveY = input.getMouseMoveY();
  152. }
  153. function update(timeStep) {
  154. UpdateControls();
  155. if (cameraMode != 2) {
  156. yaw += mouseMoveX * YAW_SENSITIVITY;
  157. pitch += mouseMoveY * YAW_SENSITIVITY;
  158. }
  159. if (pitch < -80)
  160. pitch = -80;
  161. if (pitch > 80)
  162. pitch = 80;
  163. if (button0) {
  164. cameraMode++;
  165. if (cameraMode == 3)
  166. cameraMode = 0;
  167. }
  168. }
  169. function postUpdate(timestep) {
  170. // Get camera lookat dir from character yaw + pitch
  171. var rot = node.getRotation();
  172. dir = quat.create();
  173. quat.setAxisAngle(dir, [1, 0, 0], (pitch * Math.PI / 180.0));
  174. quat.multiply(dir, [rot[1], rot[2], rot[3], rot[0]], dir);
  175. var headNode = node.getChild("Head_Tip", true);
  176. var cameraNode = game.cameraNode;
  177. if (cameraMode == 1) {
  178. var headPos = headNode.getWorldPosition();
  179. var offset = [0.0, 0.15, 0.2];
  180. vec3.add(headPos, headPos, vec3.transformQuat(offset, offset, [rot[1], rot[2], rot[3], rot[0]]));
  181. cameraNode.setPosition(headPos);
  182. cameraNode.setRotation([dir[3], dir[0], dir[1], dir[2]]);
  183. quat.setAxisAngle(dir, [0, 1, 0], (yaw * Math.PI / 180.0));
  184. node.setRotation([dir[3], dir[0], dir[1], dir[2]]);
  185. }
  186. if (cameraMode == 0) {
  187. var aimPoint = node.getPosition();
  188. var aimOffset = [0, 1.7, 0];
  189. vec3.transformQuat(aimOffset, aimOffset, dir);
  190. vec3.add(aimPoint, aimPoint, aimOffset);
  191. var rayDir = vec3.create();
  192. vec3.transformQuat(rayDir, [0, 0, -1], dir);
  193. vec3.scale(rayDir, rayDir, 12);
  194. vec3.add(aimPoint, aimPoint, rayDir);
  195. cameraNode.setPosition(aimPoint);
  196. cameraNode.setRotation([dir[3], dir[0], dir[1], dir[2]]);
  197. quat.setAxisAngle(dir, [0, 1, 0], (yaw * Math.PI / 180.0));
  198. node.setRotation([dir[3], dir[0], dir[1], dir[2]]);
  199. } else
  200. MoveCamera(timestep);
  201. }