Skip to content

Commit

Permalink
OMG IT WORKS (thrust manager uses available thrust efficiently)
Browse files Browse the repository at this point in the history
when attempting to move down & gravity is down, thrust manager just goes up (opposing gravity) less
means gravity dampers make flying in gravity have identical acceleration to flying in zero gravity
  • Loading branch information
Penguin-Spy committed Jan 21, 2024
1 parent 4b360a1 commit 931f299
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
10 changes: 9 additions & 1 deletion client/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Debug {
/** @type {boolean} */
#physicsWireframeEnabled

#debugFrame; #renderSpan; #positionDebug; #velocityDebug; #angularVelocityDebug; #controllerManager
#debugFrame; #renderSpan; #positionDebug; #velocityDebug; #angularVelocityDebug; #thrustOutputDebug; #controllerManager

constructor() {
this.#wireframeMeshes = []
Expand All @@ -55,6 +55,7 @@ class Debug {
this.#positionDebug = generateDebugRow("pos", physicsDebugTable)
this.#velocityDebug = generateDebugRow("vel", physicsDebugTable)
this.#angularVelocityDebug = generateDebugRow("ang", physicsDebugTable)
this.#thrustOutputDebug = generateDebugRow("thr", physicsDebugTable)

this.#debugFrame.appendChild(this.#renderSpan)
this.#debugFrame.appendChild(physicsDebugTable)
Expand Down Expand Up @@ -171,6 +172,13 @@ class Debug {
this.#angularVelocityDebug[0].innerText = formatNumber("p", body.angularVelocity.x)
this.#angularVelocityDebug[1].innerText = formatNumber("y", body.angularVelocity.y)
this.#angularVelocityDebug[2].innerText = formatNumber("r", body.angularVelocity.z)

const tm = this.#controllerManager.activeController?.thrustManager
if(tm) {
this.#thrustOutputDebug[0].innerText = formatNumber("x", tm._logicalAcceleration.x)
this.#thrustOutputDebug[1].innerText = formatNumber("y", tm._logicalAcceleration.y)
this.#thrustOutputDebug[2].innerText = formatNumber("z", tm._logicalAcceleration.z)
}
}

if(this.#physicsWireframeEnabled) {
Expand Down
85 changes: 62 additions & 23 deletions common/ThrustManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const _logicalAcceleration = new THREE.Vector3()

const _q1 = new THREE.Quaternion()

/*
const _v1 = new THREE.Vector3()
const _availablePositiveThrust = new THREE.Vector3()
const _availableNegativeThrust = new THREE.Vector3()

/*
const _v1 = new THREE.Vector3()
*/

const min = Math.min, max = Math.max, sign = Math.sign
Expand Down Expand Up @@ -186,6 +187,8 @@ export default class ThrustManager {
// convert from full thrust (Newtons, kg*m/s²) to desired change in velocity (m/s)
_desiredAcceleration.multiplyScalar(this.#thrustSensitivity * DT * this.#rigidBody.invMass)

_availablePositiveThrust.copy(this.#totalPositiveThrust).multiplyScalar(DT * this.#rigidBody.invMass)
_availableNegativeThrust.copy(this.#totalNegativeThrust).multiplyScalar(DT * this.#rigidBody.invMass)

// temp, don't need once dampeners code is done & always sets all 3 fields
_effectiveAcceleration.set(0, 0, 0)
Expand All @@ -199,27 +202,18 @@ export default class ThrustManager {

// do **math** to cancel out components of totalGravityVector that need to be canceled out
{
// calculate the necessary ideal acceleration to cancel gravity & achieve the desired acceleration
let result = _desiredAcceleration.x - _gravityAcceleration.x
// if result is the same sign as the input, clamp it with the same direction's max thrust
if(sign(result) === sign(_desiredAcceleration.x)) {
// same A
if(sign(result) === 1) {
result = min(result, this.#totalPositiveThrust.x)
} else {
result = max(result, -this.#totalNegativeThrust.x)
}
} else { // if result is the opposite sign of the input (or input == zero), clamp it with the opposite direction's max thrust
// same A
if(sign(result) === 1) {
result = min(result, this.#totalPositiveThrust.x)
} else {
result = max(result, -this.#totalNegativeThrust.x)
}
// clamp result for whichever direction it's in
if(sign(result) === 1) {
result = min(result, _availablePositiveThrust.x)
} else {
result = max(result, -_availableNegativeThrust.x)
}
// is clamped for the correct direction
// the result is how much acceleration we're technically doing with the thrusters
_logicalAcceleration.x = result

let thing = _gravityAcceleration.x - result
let thing = _gravityAcceleration.x + result
// if opposite sign of gravity, = effective thrust, zero gravity (was all canceled out)
if(sign(thing) !== sign(_gravityAcceleration.x)) {
_effectiveAcceleration.x = thing
Expand All @@ -229,6 +223,54 @@ export default class ThrustManager {
_gravityAcceleration.x = thing
}
}
{
// calculate the necessary ideal acceleration to cancel gravity & achieve the desired acceleration
let result = _desiredAcceleration.y - _gravityAcceleration.y
// clamp result for whichever direction it's in
if(sign(result) === 1) {
result = min(result, _availablePositiveThrust.y)
} else {
result = max(result, -_availableNegativeThrust.y)
}
// the result is how much acceleration we're technically doing with the thrusters
_logicalAcceleration.y = result

let thing = _gravityAcceleration.y + result
// if opposite sign of gravity, = effective thrust, zero gravity (was all canceled out)
if(sign(thing) !== sign(_gravityAcceleration.y)) {
_effectiveAcceleration.y = thing
_gravityAcceleration.y = 0
} else { // if same sign as gravity, = remaining gravity, zero effective thrust (was all used canceling gravity)
_effectiveAcceleration.y = 0
_gravityAcceleration.y = thing
}
}
{
// calculate the necessary ideal acceleration to cancel gravity & achieve the desired acceleration
let result = _desiredAcceleration.z - _gravityAcceleration.z
// clamp result for whichever direction it's in
if(sign(result) === 1) {
result = min(result, _availablePositiveThrust.z)
} else {
result = max(result, -_availableNegativeThrust.z)
}
// the result is how much acceleration we're technically doing with the thrusters
_logicalAcceleration.z = result

let thing = _gravityAcceleration.z + result
// if opposite sign of gravity, = effective thrust, zero gravity (was all canceled out)
if(sign(thing) !== sign(_gravityAcceleration.z)) {
_effectiveAcceleration.z = thing
_gravityAcceleration.z = 0
} else { // if same sign as gravity, = remaining gravity, zero effective thrust (was all used canceling gravity)
_effectiveAcceleration.z = 0
_gravityAcceleration.z = thing
}
}

// apply changes to gravity vector
_gravityAcceleration.applyQuaternion(this.#rigidBody.quaternion)
this.#body.totalGravityVector.copy(_gravityAcceleration)

// determine additional acceleration to apply to change actual velocity to desired velocity

Expand All @@ -239,14 +281,11 @@ export default class ThrustManager {
// else set logical & effective acceleration to desired acceleration
// END IF DAMPENERS

// apply changes to gravity vector
_gravityAcceleration.applyQuaternion(this.#rigidBody.quaternion)
this.#body.totalGravityVector.copy(_gravityAcceleration)

// apply effective acceleration
//this.#rigidBody.velocity.vadd(_effectiveAcceleration, this.#rigidBody.velocity)
this.#rigidBody.applyLocalImpulse(_effectiveAcceleration)

this._logicalAcceleration = _logicalAcceleration


// TODO: probably way easier (and better performance) to just multiply _v by the rigidBody's invMass and add to velocity directly
Expand Down

0 comments on commit 931f299

Please sign in to comment.