Skip to content

Commit

Permalink
Velocity instead of speed; acceleration projectiles expanded
Browse files Browse the repository at this point in the history
Previously the information saved through submunition layers was stored as speed to be used on the current heading when submunitions are launched.  Now the headings are baked into the saved speed, leading to saved velocity (how it works).  Also, projectiles with acceleration are not branched on anymore but included with regularly fired submunitions.
  • Loading branch information
Amazinite authored Apr 22, 2022
1 parent e7a8cfb commit 8152b43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
34 changes: 10 additions & 24 deletions source/Projectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ Projectile::Projectile(const Ship &parent, Point position, Angle angle, const We
if(inaccuracy)
this->angle += Angle::Random(inaccuracy) - Angle::Random(inaccuracy);

// Inaccuracy is only applied to the velocity of the projectile, not the velocity of the ship,
// so we don't include the firing ship's velocity in the recorded speed of the projectile.
speed = weapon->Velocity() + Random::Real() * weapon->RandomVelocity();
velocity += this->angle.Unit() * speed;
dV = this->angle.Unit() * (weapon->Velocity() + Random::Real() * weapon->RandomVelocity());
velocity += dV;

// If a random lifetime is specified, add a random amount up to that amount.
if(weapon->RandomLifetime())
Expand All @@ -76,28 +74,12 @@ Projectile::Projectile(const Projectile &parent, const Point &offset, const Angl
if(inaccuracy)
this->angle += Angle::Random(inaccuracy) - Angle::Random(inaccuracy);

// Calculate the speed of this projectile, which is the speed of the parent
// plus whatever speed the submunition adds.
speed = parent.speed + weapon->Velocity() + Random::Real() * weapon->RandomVelocity();

// Given that submunitions inherit the velocity of the parent projectile,
// it is often the case that submunitions don't add any additional velocity.
// But we still want inaccuracy to have an effect on submunitions. Because of
// this, we tilt the velocity of submunitions in the direction of the inaccuracy.
// This isn't done for submunitions with an accelerating parent, as an accelerating
// projectile's actual velocity won't match the recorded speed value.
if(inaccuracy && !parent.weapon->Acceleration())
{
// Unwind the inaccuracy of the parent.
velocity -= parent.angle.Unit() * parent.speed;
// Apply the inaccuracy of the submunition.
velocity += this->angle.Unit() * speed;
}
else
{
// Only add the velocity that came from the submunition.
velocity += this->angle.Unit() * (speed - parent.speed);
}
dV = this->angle.Unit() * (parent.dV.Length() + weapon->Velocity() + Random::Real() * weapon->RandomVelocity());
velocity += dV - parent.dV;

// If a random lifetime is specified, add a random amount up to that amount.
if(weapon->RandomLifetime())
Expand Down Expand Up @@ -259,8 +241,12 @@ void Projectile::Move(vector<Visual> &visuals, vector<Projectile> &projectiles)

if(accel)
{
velocity *= 1. - weapon->Drag();
velocity += accel * angle.Unit();
double d = 1. - weapon->Drag();
double a = accel * angle.Unit();
velocity *= d;
velocity += a;
dV *= d;
dV += a;
}

position += velocity;
Expand Down
4 changes: 2 additions & 2 deletions source/Projectile.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ class Projectile : public Body {
const Ship *cachedTarget = nullptr;
const Government *targetGovernment = nullptr;

// How much speed has been added by all stages of this projectile
// The change in velocity of all stages of this projectile
// relative to the firing ship.
double speed = 0.;
Point dV;
double clip = 1.;
int lifetime = 0;
double distanceTraveled = 0;
Expand Down

0 comments on commit 8152b43

Please sign in to comment.