| | } |
| | |
| | + altitudeReached(currentAltitude, currentSpeed) { |
| | + const G = 6.67384e-11; |
| | + const R0 = 6378137.0; |
| | + const M = 5.97219e24; |
| | + const mu = G * M; |
| | + |
| | + const r1 = R0 + currentAltitude; |
| | + const r2 = mu / (-0.5 * currentSpeed ** 2 + mu / r1); |
| | + return r2 - R0; |
| | + } |
| | + |
| | /** |
| | * Checks if the rocket is still flying, i.e., if it has fuel and has not |
| | * reached the target altitude. |
| | * |
| | * @returns {boolean} True if the rocket is flying. |
| | */ |
| | flying() { |
| | - return this.mass > this.dryMass && this.altitude < this.targetAltitude && this.altitude > 0; |
| | + return ( |
| | + this.mass > this.dryMass && |
| | + this.altitude < this.targetAltitude && |
| | + this.altitude > 0 |
| | + ); |
| | } |
| | |
 |
| | this.tThrust = this.exhaustVelocity * this.massFlowRate; |
| | |
| | - const thrusting = this.tThrust > 0; |
| | - const vThrust = thrusting ? -drag[1] : 0; |
| | - const hThrust = thrusting ? Math.sqrt(this.tThrust ** 2 - vThrust ** 2) : 0; |
| | + let vThrust = 0.0; |
| | + let hThrust = 0.0; |
| | + |
| | + if (this.tThrust > 0) { |
| | + vThrust = -drag[1]; |
| | + |
| | + const reached = this.altitudeReached(this.altitude, this.vVelocity); |
| | + |
| | + // Increase vertical thrust until orbital altitude is reached. |
| | + if (reached < this.targetAltitude) { |
| | + vThrust = 0.99 * this.tThrust; |
| | + } |
| | + |
| | + if (vThrust > this.tThrust) { |
| | + vThrust = this.tThrust; |
| | + } |
| | + |
| | + hThrust = Math.sqrt(this.tThrust ** 2 - vThrust ** 2) |
| | + } |
| | |
| | const g = this.planet.gravity(this.latitude, this.altitude); |