From 226ec5f51acb6fdf651e2498be69c0f8d8b7ce3f Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Sat, 29 Nov 2025 08:17:14 -0600 Subject: [PATCH] Fix bullet trajectory when ship is rotating MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calculate velocity at bullet spawn point (8.4 units from center) instead of ship center. Accounts for tangential velocity from angular rotation using cross product: velocity_at_spawn = linear + (angular × offset). This fixes bullets appearing to drift when ship is moving and rotating. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/ship/ship.ts | 24 +++++++++++++++++++++--- src/ship/weaponSystem.ts | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ship/ship.ts b/src/ship/ship.ts index 8680ac0..117c3a7 100644 --- a/src/ship/ship.ts +++ b/src/ship/ship.ts @@ -697,11 +697,29 @@ export class Ship { } if (this._weapons && this._ship && this._ship.physicsBody) { - // Calculate projectile velocity: ship forward + ship velocity - const shipVelocity = this._ship.physicsBody.getLinearVelocity(); + // Get ship velocities + const linearVelocity = this._ship.physicsBody.getLinearVelocity(); + const angularVelocity = this._ship.physicsBody.getAngularVelocity(); + + // Spawn offset in local space (must match weaponSystem.ts) + const localSpawnOffset = new Vector3(0, 0.5, 8.4); + + // Transform spawn offset to world space (direction only) + const worldSpawnOffset = Vector3.TransformNormal( + localSpawnOffset, + this._ship.getWorldMatrix() + ); + + // Calculate tangential velocity at spawn point: ω × r + const tangentialVelocity = angularVelocity.cross(worldSpawnOffset); + + // Velocity at spawn point = linear + tangential + const velocityAtSpawn = linearVelocity.add(tangentialVelocity); + + // Final projectile velocity: forward direction + spawn point velocity const projectileVelocity = this._ship.forward .scale(200000) - .add(shipVelocity); + .add(velocityAtSpawn); this._weapons.fire(this._ship, projectileVelocity); } diff --git a/src/ship/weaponSystem.ts b/src/ship/weaponSystem.ts index 3d35419..cc6855a 100644 --- a/src/ship/weaponSystem.ts +++ b/src/ship/weaponSystem.ts @@ -83,7 +83,7 @@ export class WeaponSystem { // Create projectile instance const ammo = new InstancedMesh("ammo", this._ammoBaseMesh as Mesh); ammo.parent = shipTransform; - ammo.position.y = 0.1; + ammo.position.y = 0.5; ammo.position.z = 8.4; // Detach from parent to move independently