From fa15fce4ef31773e1b29379366e2d8a020d87ee2 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Mon, 24 Nov 2025 17:03:41 -0600 Subject: [PATCH] Fixed some physics problems. --- src/core/gameConfig.ts | 10 ++++++---- src/ship/input/inputControlManager.ts | 2 ++ src/ship/shipPhysics.ts | 22 +++++++++++++++------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/core/gameConfig.ts b/src/core/gameConfig.ts index c4a857e..480f03f 100644 --- a/src/core/gameConfig.ts +++ b/src/core/gameConfig.ts @@ -4,13 +4,14 @@ const DEFAULT_SHIP_PHYSICS = { maxLinearVelocity: 200, maxAngularVelocity: 1.4, - linearForceMultiplier: 100, + linearForceMultiplier: 500, angularForceMultiplier: 1.5, - linearFuelConsumptionRate: 0.00002778, // 1 minute at full thrust (60 Hz) + linearFuelConsumptionRate: 0.0002778, // 1 minute at full thrust (60 Hz) angularFuelConsumptionRate: 0.0001389, // 2 minutes at full thrust (60 Hz) linearDamping: 0.2, - angularDamping: 0.3, // Moderate damping for 2-3 second coast - alwaysActive: true // Prevent physics sleep (false may cause abrupt stops at zero velocity) + angularDamping: 0.5, // Moderate damping for 2-3 second coast + alwaysActive: true, // Prevent physics sleep (false may cause abrupt stops at zero velocity) + reverseThrustFactor: 0.3 // Reverse thrust at 50% of forward thrust power }; /** @@ -85,6 +86,7 @@ export class GameConfig { linearDamping: config.shipPhysics.linearDamping ?? DEFAULT_SHIP_PHYSICS.linearDamping, angularDamping: config.shipPhysics.angularDamping ?? DEFAULT_SHIP_PHYSICS.angularDamping, alwaysActive: config.shipPhysics.alwaysActive ?? DEFAULT_SHIP_PHYSICS.alwaysActive, + reverseThrustFactor: config.shipPhysics.reverseThrustFactor ?? DEFAULT_SHIP_PHYSICS.reverseThrustFactor, }; } } else { diff --git a/src/ship/input/inputControlManager.ts b/src/ship/input/inputControlManager.ts index 83637ba..b7c4fcd 100644 --- a/src/ship/input/inputControlManager.ts +++ b/src/ship/input/inputControlManager.ts @@ -119,10 +119,12 @@ export class InputControlManager { } // Enable pointer selection + console.log(`[InputControlManager] About to update pointer feature...`); this.updatePointerFeature(); // Emit state change event this.emitStateChange(requester); + console.log(`[InputControlManager] ===== Ship controls disabled =====`); } /** diff --git a/src/ship/shipPhysics.ts b/src/ship/shipPhysics.ts index 2ba5cb9..055b8c4 100644 --- a/src/ship/shipPhysics.ts +++ b/src/ship/shipPhysics.ts @@ -71,21 +71,29 @@ export class ShipPhysics { // Only apply force if we haven't reached max velocity if (currentSpeed < this._config.maxLinearVelocity) { // Get local direction (Z-axis for forward/backward thrust) - const localDirection = new Vector3(0, 0, -leftStick.y); + const thrustDirection = -leftStick.y; // negative = forward, positive = reverse + const localDirection = new Vector3(0, 0, thrustDirection); + // Transform to world space const worldDirection = Vector3.TransformNormal( localDirection, transformNode.getWorldMatrix() ); - const force = worldDirection.scale(this._config.linearForceMultiplier); - // Apply force at center of mass to avoid unintended torque - // (applying at an offset point creates rotation, noticeable at zero linear velocity) - const thrustPoint = Vector3.TransformCoordinates( - physicsBody.getMassProperties().centerOfMass, - transformNode.getWorldMatrix() + // Apply reverse thrust factor: forward at full power, reverse at reduced power + const thrustMultiplier = thrustDirection < 0 + ? 1.0 // Forward thrust at full power + : this._config.reverseThrustFactor; // Reverse thrust scaled down + + const force = worldDirection.scale( + this._config.linearForceMultiplier * thrustMultiplier ); + // Apply force at ship's world position (center of mass) + // Since we overrode center of mass to (0,0,0) in local space, the transform origin is the CoM + // Using getAbsolutePosition() instead of transforming CoM avoids gyroscopic coupling during rotation + const thrustPoint = transformNode.getAbsolutePosition(); + physicsBody.applyForce(force, thrustPoint); // Consume fuel based on config rate (tuned for 1 minute at full thrust)