Fix bullet trajectory when ship is rotating
All checks were successful
Build / build (push) Successful in 1m48s

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 <noreply@anthropic.com>
This commit is contained in:
Michael Mainguy 2025-11-29 08:17:14 -06:00
parent b46f44e32d
commit 226ec5f51a
2 changed files with 22 additions and 4 deletions

View File

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

View File

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