Fix bullet trajectory when ship is rotating
All checks were successful
Build / build (push) Successful in 1m48s
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:
parent
b46f44e32d
commit
226ec5f51a
@ -697,11 +697,29 @@ export class Ship {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this._weapons && this._ship && this._ship.physicsBody) {
|
if (this._weapons && this._ship && this._ship.physicsBody) {
|
||||||
// Calculate projectile velocity: ship forward + ship velocity
|
// Get ship velocities
|
||||||
const shipVelocity = this._ship.physicsBody.getLinearVelocity();
|
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
|
const projectileVelocity = this._ship.forward
|
||||||
.scale(200000)
|
.scale(200000)
|
||||||
.add(shipVelocity);
|
.add(velocityAtSpawn);
|
||||||
|
|
||||||
this._weapons.fire(this._ship, projectileVelocity);
|
this._weapons.fire(this._ship, projectileVelocity);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -83,7 +83,7 @@ export class WeaponSystem {
|
|||||||
// Create projectile instance
|
// Create projectile instance
|
||||||
const ammo = new InstancedMesh("ammo", this._ammoBaseMesh as Mesh);
|
const ammo = new InstancedMesh("ammo", this._ammoBaseMesh as Mesh);
|
||||||
ammo.parent = shipTransform;
|
ammo.parent = shipTransform;
|
||||||
ammo.position.y = 0.1;
|
ammo.position.y = 0.5;
|
||||||
ammo.position.z = 8.4;
|
ammo.position.z = 8.4;
|
||||||
|
|
||||||
// Detach from parent to move independently
|
// Detach from parent to move independently
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user