RapierMovementController.re.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import GetForwardVector from 'Assets/Library/GetForwardVector';
  2. import * as RE from 'rogue-engine';
  3. import * as THREE from 'three'
  4. import RAPIER from '@dimforge/rapier3d-compat';
  5. import RapierBody from '@RE/RogueEngine/rogue-rapier/Components/RapierBody.re';
  6. import RapierConfig from '@RE/RogueEngine/rogue-rapier/Components/RapierConfig.re';
  7. import RogueRapier from '@RE/RogueEngine/rogue-rapier/Lib/RogueRapier';
  8. export default class RapierMovementController extends RE.Component {
  9. initialized = false
  10. characterController: RAPIER.KinematicCharacterController
  11. rapierConfig: RapierConfig
  12. @RE.props.num() speed: number = 1
  13. @RE.props.num() rotationSpeed: number = 1
  14. // @RE.props.num() jumpStrength: number = 10
  15. @RE.props.checkbox() useCameraForward: boolean = true
  16. @RE.props.checkbox() useObjectForward: boolean = false
  17. cameraVectorCalculator: GetForwardVector
  18. objectVectorCalculator: GetForwardVector
  19. bodyComponent: RapierBody
  20. velocity: THREE.Vector3 = new THREE.Vector3(0,0,0)
  21. @RE.props.vector3() drag: THREE.Vector3 = new THREE.Vector3(0.002, 0.002, 0.002)
  22. // private jumpVector: RAPIER.Vector3 = new RAPIER.Vector3(0,1,0)
  23. private scaledVelocity: THREE.Vector3 = new THREE.Vector3(0,0,0)
  24. private rapierScaledVelocity: RAPIER.Vector3 = new RAPIER.Vector3(0,0,0)
  25. elapsed = 0
  26. @RE.props.checkbox() thrust = false
  27. @RE.props.checkbox() brake = false
  28. @RE.props.checkbox() strafeLeft = false
  29. @RE.props.checkbox() strafeRight = false
  30. @RE.props.checkbox() rotateLeft = false
  31. @RE.props.checkbox() rotateRight = false
  32. awake() {
  33. this.bodyComponent = RE.getComponent(RapierBody, this.object3d) as RapierBody
  34. }
  35. start() {
  36. this.cameraVectorCalculator = new GetForwardVector(RE.Runtime.camera)
  37. this.objectVectorCalculator = new GetForwardVector(this.object3d)
  38. }
  39. beforeUpdate(): void {
  40. if (!RogueRapier.initialized) return;
  41. !this.initialized && this.init();
  42. }
  43. init() {
  44. this.characterController = RogueRapier.world.createCharacterController(0.1)
  45. // this.characterController.enableAutostep(this.autostepMaxHeight, this.autostepMinWidth, this.autostepIncludeDynamicBodies)
  46. // this.characterController.enableSnapToGround(this.snapToGroundDistance)
  47. this.characterController.setCharacterMass(100)
  48. this.characterController.setApplyImpulsesToDynamicBodies(true)
  49. // this.characterController.setSlideEnabled(this.slideEnabled)
  50. }
  51. update() {
  52. if(!this.bodyComponent.body) {
  53. return
  54. }
  55. this.elapsed += RE.Runtime.deltaTime
  56. // if(direction.x != 0) {
  57. // this.moveForward(direction.x * this.speed)
  58. // }
  59. // if(direction.y != 0) {
  60. // this.moveRight(direction.y * this.speed)
  61. // }
  62. // if(direction.z != 0) {
  63. // const floorCheckComponent = RE.getComponent(FloorCheckComponent, this.object3d)
  64. // if(floorCheckComponent) {
  65. // if(floorCheckComponent.isOnFloor) {
  66. // this.jumpVector.y = this.jumpStrength
  67. // this.bodyComponent.body.applyImpulse(this.jumpVector, true)
  68. // }
  69. // }
  70. // }
  71. if(this.thrust) {
  72. this.moveForward(1 * this.speed)
  73. }
  74. if(this.brake) {
  75. this.moveForward(-1 * this.speed)
  76. }
  77. if(this.rotateLeft) {
  78. this.rotate(1 * this.rotationSpeed)
  79. }
  80. if(this.rotateRight) {
  81. this.rotate(-1 * this.rotationSpeed)
  82. }
  83. const fixedStep = RE.Runtime.deltaTime
  84. const nextPosition = this.bodyComponent.body.translation()
  85. const nextVelocity = this.convertRapierToThreeVec3(this.bodyComponent.body.linvel())
  86. nextVelocity.x *= (1 - this.drag.x)
  87. nextVelocity.y *= (1 - this.drag.y)
  88. nextVelocity.z *= (1 - this.drag.z)
  89. this.velocity.x *= (1 - this.drag.x)
  90. this.velocity.y *= (1 - this.drag.x)
  91. this.velocity.z *= (1 - this.drag.x)
  92. nextVelocity.x += this.velocity.x
  93. nextVelocity.y += this.velocity.y
  94. nextVelocity.z += this.velocity.z
  95. this.characterController.computeColliderMovement(
  96. this.bodyComponent.body.collider(0),
  97. nextVelocity.clone().multiplyScalar(fixedStep),
  98. )
  99. const characterMovement = this.characterController.computedMovement()
  100. nextPosition.x += characterMovement.x
  101. //nextPosition.y += characterMovement.y
  102. nextPosition.y = 0
  103. nextPosition.z += characterMovement.z
  104. this.bodyComponent.body.setNextKinematicTranslation(nextPosition)
  105. this.thrust = false
  106. this.brake = false
  107. this.strafeLeft = false
  108. this.strafeRight = false
  109. this.rotateLeft = false
  110. this.rotateRight = false
  111. }
  112. moveForward(distance) {
  113. let forwardVector;
  114. if(this.useCameraForward) {
  115. // forwardVector = this.cameraVectorCalculator.getForward()
  116. forwardVector = this.objectVectorCalculator.getForward()
  117. let euler = new THREE.Euler(0,0,0, 'XZY')
  118. euler.setFromQuaternion(RE.Runtime.camera.quaternion.normalize())
  119. euler.x = 0
  120. euler.z = 0
  121. let newRotation = new THREE.Quaternion()
  122. newRotation.setFromEuler(euler)
  123. const nextRotation = this.convertRapierToThreeQuat(this.bodyComponent.body.rotation())
  124. nextRotation.slerp(newRotation, RE.Runtime.deltaTime)
  125. this.bodyComponent.body.setNextKinematicRotation(nextRotation)
  126. } else {
  127. forwardVector = this.objectVectorCalculator.getForward()
  128. }
  129. this.scaledVelocity.set(0,0,0)
  130. this.scaledVelocity.addScaledVector(forwardVector, distance)
  131. // this.velocity.x += this.scaledVelocity.x
  132. // this.velocity.y += this.scaledVelocity.y
  133. // this.velocity.z += this.scaledVelocity.z
  134. this.velocity.x += this.scaledVelocity.x
  135. this.velocity.y += this.scaledVelocity.y
  136. this.velocity.z += this.scaledVelocity.z
  137. }
  138. moveRight(distance) {
  139. let rightVector;
  140. if(this.useCameraForward) {
  141. rightVector = this.cameraVectorCalculator.getRight()
  142. } else {
  143. rightVector = this.objectVectorCalculator.getRight()
  144. }
  145. this.scaledVelocity.set(0,0,0)
  146. this.scaledVelocity.addScaledVector(rightVector, distance)
  147. this.rapierScaledVelocity.x = this.scaledVelocity.x
  148. this.rapierScaledVelocity.y = this.scaledVelocity.y
  149. this.rapierScaledVelocity.z = this.scaledVelocity.z
  150. this.bodyComponent.body.applyImpulse(this.rapierScaledVelocity, true)
  151. }
  152. rotate(amount) {
  153. const nextRotation = this.convertRapierToThreeQuat(this.bodyComponent.body.rotation())
  154. const newRotation = this.quaternionFromAxisAngle(this.object3d.up, amount)
  155. nextRotation.multiply(newRotation)
  156. this.bodyComponent.body.setNextKinematicRotation(nextRotation)
  157. }
  158. convertRapierToThreeQuat(oldQuaternion: RAPIER.Quaternion) {
  159. return new THREE.Quaternion(oldQuaternion.x, oldQuaternion.y, oldQuaternion.z, oldQuaternion.w)
  160. }
  161. convertRapierToThreeVec3(oldVector: RAPIER.Vector3) {
  162. return new THREE.Vector3(oldVector.x, oldVector.y, oldVector.z)
  163. }
  164. quaternionFromAxisAngle(axis, rotationRadians) {
  165. let factor = Math.sin(rotationRadians / 2)
  166. let x = axis.x * factor
  167. let y = axis.y * factor
  168. let z = axis.z * factor
  169. let w = Math.cos(rotationRadians / 2)
  170. let normalized = new THREE.Quaternion(x, y, z, w).normalize()
  171. return new THREE.Quaternion(normalized.x, normalized.y, normalized.z, normalized.w)
  172. }
  173. }
  174. RE.registerComponent(RapierMovementController);