From 291fb7e0233dd887cff7fe8fae8e5a9f277b6d3b Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Fri, 11 Nov 2016 10:10:43 +0100 Subject: [PATCH] lerped steering --- ...-lib-component-raycast-vehicle-controls.js | 141 +++++++++++++++--- 1 file changed, 117 insertions(+), 24 deletions(-) diff --git a/src/game-lib-component-raycast-vehicle-controls.js b/src/game-lib-component-raycast-vehicle-controls.js index 84d0317..4e2d462 100644 --- a/src/game-lib-component-raycast-vehicle-controls.js +++ b/src/game-lib-component-raycast-vehicle-controls.js @@ -19,12 +19,92 @@ GameLib.D3.ComponentRaycastVehicleControls = function( this.maxForce = maxForce || 400; this.steering = steering || 0.5; + this.brakeForce = 1000000; + + this.currentSteering = 0; + this.steeringSpeed = 8.0; + + this.keyLeftPressed = false; + this.keyRightPressed = false; + this.keyForwardPressed = false; + this.keyBackPressed = false; + this.keyBreakPressed = false; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentRaycastVehicleControls, GameLib.D3.ComponentInterface); }; ///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentRaycastVehicleControls.prototype.onUpdate = function( + deltaTime, + parentEntity +) { + + + if (this.keyForwardPressed) { // Forward [i] + + this.raycastVehicleComponent.instance.applyEngineForce(-this.maxForce, this.backLWheelIndex); + this.raycastVehicleComponent.instance.applyEngineForce(-this.maxForce, this.backRWheelIndex); + + } else if (this.keyBackPressed) { // Back [k] + + this.raycastVehicleComponent.instance.applyEngineForce(this.maxForce, this.backLWheelIndex); + this.raycastVehicleComponent.instance.applyEngineForce(this.maxForce, this.backRWheelIndex); + + } else if (!this.keyForwardPressed && !this.keyBackPressed) { + this.raycastVehicleComponent.instance.applyEngineForce(0, this.backLWheelIndex); + this.raycastVehicleComponent.instance.applyEngineForce(0, this.backRWheelIndex); + } + + if (this.keyLeftPressed) { // Left [j] + + var t = deltaTime * this.steeringSpeed; + t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); + + this.currentSteering = this.currentSteering + (this.steering - this.currentSteering) * t; + + this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex); + this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex); + + } else if (this.keyRightPressed) { // Right [l] + + var t = deltaTime * this.steeringSpeed; + t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); + this.currentSteering = this.currentSteering + ((-this.steering) - this.currentSteering) * t; + + this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex); + this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex); + + } else if (!this.keyLeftPressed && !this.keyRightPressed) { + + var t = deltaTime * this.steeringSpeed * 2; + t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); + this.currentSteering = this.currentSteering + (0 - this.currentSteering) * t; + + this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex); + this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex); + + } + + + if(this.keyBreakPressed) { + + this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.frontLWheelIndex); + this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.frontRWheelIndex); + this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.backLWheelIndex); + this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.backRWheelIndex); + + } else { + + this.raycastVehicleComponent.instance.setBrake(0, this.frontLWheelIndex); + this.raycastVehicleComponent.instance.setBrake(0, this.frontRWheelIndex); + this.raycastVehicleComponent.instance.setBrake(0, this.backLWheelIndex); + this.raycastVehicleComponent.instance.setBrake(0, this.backRWheelIndex); + + } + +}; + GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity = function( parentScene, parentEntity @@ -38,26 +118,33 @@ GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity = functio } else { var component = this; + document.addEventListener('keydown', function(event) { if (event.keyCode == 73) { // Forward [i] - component.raycastVehicleComponent.instance.applyEngineForce(-component.maxForce, component.backLWheelIndex); - component.raycastVehicleComponent.instance.applyEngineForce(-component.maxForce, component.backRWheelIndex); - - } else if (event.keyCode == 74) { // Left [j] - - component.raycastVehicleComponent.instance.setSteeringValue(component.steering, component.frontLWheelIndex); - component.raycastVehicleComponent.instance.setSteeringValue(component.steering, component.frontRWheelIndex); + component.keyForwardPressed = true; } else if (event.keyCode == 75) { // Back [k] - component.raycastVehicleComponent.instance.applyEngineForce(component.maxForce, component.backLWheelIndex); - component.raycastVehicleComponent.instance.applyEngineForce(component.maxForce, component.backRWheelIndex); + component.keyBackPressed = true; + + } + + if (event.keyCode == 74) { // Left [j] + + component.keyLeftPressed = true; + + } else if (event.keyCode == 76) { // Right [l] + + component.keyRightPressed = true; + + } + + if (event.keyCode == 66) { + + component.keyBreakPressed = true; - } else if (event.keyCode == 76) { // Right [l] - component.raycastVehicleComponent.instance.setSteeringValue(-component.steering, component.frontLWheelIndex); - component.raycastVehicleComponent.instance.setSteeringValue(-component.steering, component.frontRWheelIndex); } }, false); @@ -66,22 +153,28 @@ GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity = functio if (event.keyCode == 73) { // Forward [i] - component.raycastVehicleComponent.instance.applyEngineForce(0, component.backLWheelIndex); - component.raycastVehicleComponent.instance.applyEngineForce(0, component.backRWheelIndex); - - } else if (event.keyCode == 74) { // Left [j] - - component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontLWheelIndex); - component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontRWheelIndex); + component.keyForwardPressed = false; } else if (event.keyCode == 75) { // Back [k] - component.raycastVehicleComponent.instance.applyEngineForce(0, component.backLWheelIndex); - component.raycastVehicleComponent.instance.applyEngineForce(0, component.backRWheelIndex); + component.keyBackPressed = false; + + } + + if (event.keyCode == 74) { // Left [j] + + component.keyLeftPressed = false; + + } else if (event.keyCode == 76) { // Right [l] + + component.keyRightPressed = false; + + } + + if (event.keyCode == 66) { + + component.keyBreakPressed = false; - } else if (event.keyCode == 76) { // Right [l] - component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontLWheelIndex); - component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontRWheelIndex); } }, false);