Fixed some physics problems.

This commit is contained in:
Michael Mainguy 2025-11-24 17:03:41 -06:00
parent e31e25f9e5
commit fa15fce4ef
3 changed files with 23 additions and 11 deletions

View File

@ -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 {

View File

@ -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 =====`);
}
/**

View File

@ -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)