tgdwyer / tgdwyer.github.io

Apps and Demos
MIT License
30 stars 44 forks source link

Fix bullet velocity #118

Closed snicklepickles closed 1 month ago

snicklepickles commented 1 month ago

I may be mistaken, but wouldn't scaling the added velocity of the bullet by -2 result in it firing "backwards" (relative to the velocity of the ship)? For example, assuming linear motion:

This velocity value also appears to be inconsistent with the StackBlitz implementation: https://stackblitz.com/edit/asteroids2023?file=src%2Fstate.ts%3AL107:

class Shoot implements Action {
    apply = (s: State) => ({ ...s,
        bullets: s.bullets.concat([
            ((unitVec: Vec) =>
                createBullet
                    ({ id: String(s.objCount), createTime: s.time })
                    ({ radius: Constants.BulletRadius, 
                       pos: s.ship.pos.add(unitVec.scale(s.ship.radius)) })
                    (s.ship.vel.add(unitVec.scale(Constants.BulletVelocity)))
            )(Vec.unitVecInDirection(s.ship.angle))]),
        objCount: s.objCount + 1
    })
}

https://stackblitz.com/edit/asteroids2023?file=src%2Ftypes.ts: BulletVelocity: 2

tgdwyer commented 1 month ago

Thanks! Yes, well spotted. Positive velocity is in the direction the ship is facing.