Bladeren bron

tweaking character controller; adding fall reset

Justin Gilman 1 jaar geleden
bovenliggende
commit
508a8ef0a4

File diff suppressed because it is too large
+ 0 - 0
Assets/Main.rogueScene


File diff suppressed because it is too large
+ 0 - 0
Assets/Prefabs/Stairs.roguePrefab


+ 26 - 2
Assets/rogue_packages/EyeOfMidas/rapier-movement-controller/RapierMovementController.re.ts

@@ -20,10 +20,18 @@ export default class RapierMovementController extends RE.Component {
   objectVectorCalculator: GetForwardVector
   bodyComponent: RapierBody
   velocity: THREE.Vector3 = new THREE.Vector3(0,0,0)
+  gravityAcceleration: THREE.Vector3 = new THREE.Vector3(0,0,0)
 
   @RE.props.vector3() drag: THREE.Vector3 = new THREE.Vector3(0.002, 0.002, 0.002)
+  @RE.props.num() snapToGroundDistance: number = 1
+  @RE.props.num() autostepMaxHeight: number = 0.7
+  @RE.props.num() autostepMinWidth: number = 0.3
+  @RE.props.checkbox() autostepIncludeDynamicBodies: boolean = true
 
+  @RE.props.num() gravityScale: number = 2
 
+  @RE.props.num() minimumHeight: number = -10
+  @RE.props.vector3() resetPosition: THREE.Vector3 = new THREE.Vector3(0,0,0)
 
   // private jumpVector: RAPIER.Vector3 = new RAPIER.Vector3(0,1,0)
   private scaledVelocity: THREE.Vector3 = new THREE.Vector3(0,0,0)
@@ -38,13 +46,18 @@ export default class RapierMovementController extends RE.Component {
   @RE.props.checkbox() rotateLeft = false
   @RE.props.checkbox() rotateRight = false
 
+  config: RapierConfig
+
   awake() {
     this.bodyComponent = RE.getComponent(RapierBody, this.object3d) as RapierBody
+    
   }
 
   start() {
     this.cameraVectorCalculator = new GetForwardVector(RE.Runtime.camera)
     this.objectVectorCalculator = new GetForwardVector(this.object3d)
+
+    this.config = RE.getComponent(RapierConfig) as RapierConfig
   }
 
   beforeUpdate(): void {
@@ -54,8 +67,8 @@ export default class RapierMovementController extends RE.Component {
 
   init() {
     this.characterController = RogueRapier.world.createCharacterController(0.1)
-    // this.characterController.enableAutostep(this.autostepMaxHeight, this.autostepMinWidth, this.autostepIncludeDynamicBodies)
-    // this.characterController.enableSnapToGround(this.snapToGroundDistance)
+    this.characterController.enableAutostep(this.autostepMaxHeight, this.autostepMinWidth, this.autostepIncludeDynamicBodies)
+    this.characterController.enableSnapToGround(this.snapToGroundDistance)
     this.characterController.setCharacterMass(100)
     this.characterController.setApplyImpulsesToDynamicBodies(true)
     // this.characterController.setSlideEnabled(this.slideEnabled)
@@ -108,6 +121,10 @@ export default class RapierMovementController extends RE.Component {
       this.moveRight(1 * this.speed)
     }
 
+    // console.log(this.config)
+    this.gravityAcceleration.set(this.config.gravity.x, this.config.gravity.y, this.config.gravity.z)
+    this.velocity.add(this.gravityAcceleration.multiplyScalar(this.gravityScale * RE.Runtime.deltaTime))
+
     const fixedStep = RE.Runtime.deltaTime
     const nextPosition = this.bodyComponent.body.translation()
     const nextVelocity = this.convertRapierToThreeVec3(this.bodyComponent.body.linvel())
@@ -138,6 +155,13 @@ export default class RapierMovementController extends RE.Component {
     // nextPosition.y = 0
     nextPosition.z += characterMovement.z
 
+
+    if(nextPosition.y < this.minimumHeight) {
+      nextPosition.x = this.resetPosition.x
+      nextPosition.y = this.resetPosition.y
+      nextPosition.z = this.resetPosition.z
+    }
+
     this.bodyComponent.body.setNextKinematicTranslation(nextPosition)
 
     this.thrust = false

+ 46 - 5
dist/rogue-engine-user-scripts.js

@@ -315,9 +315,10 @@ __webpack_require__.r(__webpack_exports__);
 /* harmony import */ var rogue_engine__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(rogue_engine__WEBPACK_IMPORTED_MODULE_1__);
 /* harmony import */ var three__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! three */ "three");
 /* harmony import */ var three__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(three__WEBPACK_IMPORTED_MODULE_2__);
-/* harmony import */ var _dimforge_rapier3d_compat__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! @dimforge/rapier3d-compat */ "./node_modules/@dimforge/rapier3d-compat/rapier.es.js");
+/* harmony import */ var _dimforge_rapier3d_compat__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! @dimforge/rapier3d-compat */ "./node_modules/@dimforge/rapier3d-compat/rapier.es.js");
 /* harmony import */ var _RE_RogueEngine_rogue_rapier_Components_RapierBody_re__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! @RE/RogueEngine/rogue-rapier/Components/RapierBody.re */ "./Assets/rogue_packages/RogueEngine/rogue-rapier/Components/RapierBody.re.ts");
-/* harmony import */ var _RE_RogueEngine_rogue_rapier_Lib_RogueRapier__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! @RE/RogueEngine/rogue-rapier/Lib/RogueRapier */ "./Assets/rogue_packages/RogueEngine/rogue-rapier/Lib/RogueRapier.ts");
+/* harmony import */ var _RE_RogueEngine_rogue_rapier_Components_RapierConfig_re__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! @RE/RogueEngine/rogue-rapier/Components/RapierConfig.re */ "./Assets/rogue_packages/RogueEngine/rogue-rapier/Components/RapierConfig.re.ts");
+/* harmony import */ var _RE_RogueEngine_rogue_rapier_Lib_RogueRapier__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! @RE/RogueEngine/rogue-rapier/Lib/RogueRapier */ "./Assets/rogue_packages/RogueEngine/rogue-rapier/Lib/RogueRapier.ts");
 var __defProp = Object.defineProperty;
 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
 var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
@@ -336,6 +337,7 @@ var __decorateClass = (decorators, target, key, kind) => {
 
 
 
+
 class RapierMovementController extends rogue_engine__WEBPACK_IMPORTED_MODULE_1__.Component {
   constructor() {
     super(...arguments);
@@ -345,9 +347,17 @@ class RapierMovementController extends rogue_engine__WEBPACK_IMPORTED_MODULE_1__
     this.useCameraForward = true;
     this.useObjectForward = false;
     this.velocity = new three__WEBPACK_IMPORTED_MODULE_2__.Vector3(0, 0, 0);
+    this.gravityAcceleration = new three__WEBPACK_IMPORTED_MODULE_2__.Vector3(0, 0, 0);
     this.drag = new three__WEBPACK_IMPORTED_MODULE_2__.Vector3(2e-3, 2e-3, 2e-3);
+    this.snapToGroundDistance = 1;
+    this.autostepMaxHeight = 0.7;
+    this.autostepMinWidth = 0.3;
+    this.autostepIncludeDynamicBodies = true;
+    this.gravityScale = 2;
+    this.minimumHeight = -10;
+    this.resetPosition = new three__WEBPACK_IMPORTED_MODULE_2__.Vector3(0, 0, 0);
     this.scaledVelocity = new three__WEBPACK_IMPORTED_MODULE_2__.Vector3(0, 0, 0);
-    this.rapierScaledVelocity = new _dimforge_rapier3d_compat__WEBPACK_IMPORTED_MODULE_5__["default"].Vector3(0, 0, 0);
+    this.rapierScaledVelocity = new _dimforge_rapier3d_compat__WEBPACK_IMPORTED_MODULE_6__["default"].Vector3(0, 0, 0);
     this.elapsed = 0;
     this.thrust = false;
     this.brake = false;
@@ -362,14 +372,17 @@ class RapierMovementController extends rogue_engine__WEBPACK_IMPORTED_MODULE_1__
   start() {
     this.cameraVectorCalculator = new _GetForwardVector__WEBPACK_IMPORTED_MODULE_0__["default"](rogue_engine__WEBPACK_IMPORTED_MODULE_1__.Runtime.camera);
     this.objectVectorCalculator = new _GetForwardVector__WEBPACK_IMPORTED_MODULE_0__["default"](this.object3d);
+    this.config = rogue_engine__WEBPACK_IMPORTED_MODULE_1__.getComponent(_RE_RogueEngine_rogue_rapier_Components_RapierConfig_re__WEBPACK_IMPORTED_MODULE_4__["default"]);
   }
   beforeUpdate() {
-    if (!_RE_RogueEngine_rogue_rapier_Lib_RogueRapier__WEBPACK_IMPORTED_MODULE_4__["default"].initialized)
+    if (!_RE_RogueEngine_rogue_rapier_Lib_RogueRapier__WEBPACK_IMPORTED_MODULE_5__["default"].initialized)
       return;
     !this.initialized && this.init();
   }
   init() {
-    this.characterController = _RE_RogueEngine_rogue_rapier_Lib_RogueRapier__WEBPACK_IMPORTED_MODULE_4__["default"].world.createCharacterController(0.1);
+    this.characterController = _RE_RogueEngine_rogue_rapier_Lib_RogueRapier__WEBPACK_IMPORTED_MODULE_5__["default"].world.createCharacterController(0.1);
+    this.characterController.enableAutostep(this.autostepMaxHeight, this.autostepMinWidth, this.autostepIncludeDynamicBodies);
+    this.characterController.enableSnapToGround(this.snapToGroundDistance);
     this.characterController.setCharacterMass(100);
     this.characterController.setApplyImpulsesToDynamicBodies(true);
   }
@@ -396,6 +409,8 @@ class RapierMovementController extends rogue_engine__WEBPACK_IMPORTED_MODULE_1__
     if (this.strafeRight) {
       this.moveRight(1 * this.speed);
     }
+    this.gravityAcceleration.set(this.config.gravity.x, this.config.gravity.y, this.config.gravity.z);
+    this.velocity.add(this.gravityAcceleration.multiplyScalar(this.gravityScale * rogue_engine__WEBPACK_IMPORTED_MODULE_1__.Runtime.deltaTime));
     const fixedStep = rogue_engine__WEBPACK_IMPORTED_MODULE_1__.Runtime.deltaTime;
     const nextPosition = this.bodyComponent.body.translation();
     const nextVelocity = this.convertRapierToThreeVec3(this.bodyComponent.body.linvel());
@@ -413,6 +428,11 @@ class RapierMovementController extends rogue_engine__WEBPACK_IMPORTED_MODULE_1__
     nextPosition.x += characterMovement.x;
     nextPosition.y += characterMovement.y;
     nextPosition.z += characterMovement.z;
+    if (nextPosition.y < this.minimumHeight) {
+      nextPosition.x = this.resetPosition.x;
+      nextPosition.y = this.resetPosition.y;
+      nextPosition.z = this.resetPosition.z;
+    }
     this.bodyComponent.body.setNextKinematicTranslation(nextPosition);
     this.thrust = false;
     this.brake = false;
@@ -503,6 +523,27 @@ __decorateClass([
 __decorateClass([
   rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.vector3()
 ], RapierMovementController.prototype, "drag", 2);
+__decorateClass([
+  rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.num()
+], RapierMovementController.prototype, "snapToGroundDistance", 2);
+__decorateClass([
+  rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.num()
+], RapierMovementController.prototype, "autostepMaxHeight", 2);
+__decorateClass([
+  rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.num()
+], RapierMovementController.prototype, "autostepMinWidth", 2);
+__decorateClass([
+  rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.checkbox()
+], RapierMovementController.prototype, "autostepIncludeDynamicBodies", 2);
+__decorateClass([
+  rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.num()
+], RapierMovementController.prototype, "gravityScale", 2);
+__decorateClass([
+  rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.num()
+], RapierMovementController.prototype, "minimumHeight", 2);
+__decorateClass([
+  rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.vector3()
+], RapierMovementController.prototype, "resetPosition", 2);
 __decorateClass([
   rogue_engine__WEBPACK_IMPORTED_MODULE_1__.props.checkbox()
 ], RapierMovementController.prototype, "thrust", 2);

File diff suppressed because it is too large
+ 0 - 0
dist/rogue-engine-user-scripts.js.map


Some files were not shown because too many files changed in this diff