On this page
Projectiles: flight, impact and damage over the distance
The guns of Battlefield 2 fire projectiles, not rays. A bullet is an object: it is launched with a velocity, falls under gravity, may be slowed by drag, is tested against the world each tick, and can bounce off or go through what it meets. This page follows one from the muzzle to its end.
The kinds#
A projectile is an object of the template class GenericProjectile (bf2_w32ded.exe!0x005b1d50 makes the template, 0x005b4670 the object). What it does comes from its components and from its physics type:
| Physics type | Node | Used for |
|---|---|---|
| Point (1) | a point with velocity: no rotation, no size | bullets, shells, most rockets |
| RotationalPoint (2) | a point that also spins and has a bounding radius | hand grenades and other thrown things |
| Mesh (3) | a full rigid body with a collision mesh | claymores, AT mines, the AA missiles |
| Component | Script prefix | What it adds |
|---|---|---|
| collision | collision.* | what a hit does; bouncing; sticking (C4) |
| detonation | detonation.* | the explosion: damage, radius, force, triggers, the arming |
| ricochet, penetrate | whether a hit sends the projectile on or lets it through | |
| seek, follow | seek.*, follow.* | a target and steering toward it (guided missiles) |
| sound | the fly-by sound |
The launch#
FireComp::fireProjectiles (0x005c0e00) takes the projectile's start transform (for a soldier the camera's matrix moved by the weapon's start offset), and gives each projectile of the batch
velocity = launch speed x forward + the velocity of the shooter's root nodeSo a shot from a moving jeep carries the jeep's speed. The deviation then turns each projectile's velocity by a value from a table of 1,024 entries, indexed by the player's last action number and the projectile's index in the batch: the server and the shooter's client pick the same entry.
The flight#
A point projectile's step is three statements (PointPhysicsNode::updatePhysics, 0x006a81c0), 30 times a second:
if drag > 0: acceleration -= pi x 0.1^2 x drag x velocity / mass (0x006a7d30)
new velocity = velocity + acceleration x dt
position += dt x (velocity + new velocity) / 2 (0x006a7e10)
acceleration = (0, gravity x gravityModifier, 0) for the next stepGravity is the physics system's -14.73 (no shipped script changes it): half again the Earth's, which is why shells in this engine arc so visibly. gravityModifier, drag and mass are the template's. The speed is capped at 1,500 m/s. Under water the drag grows with the depth, up to 25 times. The wind the drag subtracts is always zero: the command that sets the wind writes a field that the drag never reads.
A rocket adds thrust in its update (GenericProjectile::update, 0x005b2390): while its speed along its nose is at or below maxSpeed, it accelerates by acceleration along its nose. A guided missile's seek component picks a target and its follow component turns the nose toward it, from their start delays on.
2142 computes the drag in another order (BF2142.exe!0x006c5730: the factor first, then times the velocity), which differs in the last bit and is ported as its own function. Heroes' point node adds a factor by the depth under water (BFHeroes.exe@bfh126!0x00967687).
Hitting something#
Each tick the projectile's motion is a segment from where it was to where it is, tested against the terrain, the static objects, vehicles and soldiers (a soldier is tested as the capsules of his skeleton, posed for the tick the shooter saw). handleCollision (0x005b3240) stores the material, the point and the normal, and the game logic decides the outcome (0x0056a530):
| Outcome | When | Then |
|---|---|---|
| bounce | on the terrain, slower than minDetonationSpeed; or a bouncing collision component on a material without water physics | the projectile goes on |
| ricochet | the ricochet test says so | DefaultRicochetComp::ricochet (0x005b9720) reflects it, spread by 0.2 |
| penetrate | otherwise | DefaultPenetrateComp::penetrate (0x005b8e80) casts a ray out of the object's back; the projectile leaves there with less damage, or ends |
| end | neither lets it go on | the detonation, the impact effect, the damage |
Ricochet and penetration each draw two random numbers from a generator they first seed with the simulation tick modulo 1,023: the outcome depends only on the tick, so the server and a client that replays the tick agree. The launching vehicle's own parts are skipped for one second after the launch.
Damage over the distance#
What a hit does is getDamage (0x005b1a70):
min = minDamage, or damage when minDamage is -1
damage <= min: min
no distToStartLoseDamage: damage
distance <= start: damage
distance > distToMinDamage: min
else: damage - (distance - start) x (damage - min) / (end - start)The distance is measured from the launch position, not along the flight. The number then goes through the material pair (the projectile's material against the struck one) and the armor: see damage and armor.
Thrown and placed things#
A hand grenade is a rotational point: it has drag by its bounding radius, a spin, and a resting rule so that it lies still on a slope instead of jittering (it is put 0.001 m above the surface and loses the part of its velocity along the normal). C4 sticks through a sticky collision component and is limited in number per player. Claymores and AT mines fly as rigid bodies and stick where they land.
What the port has#
| Part | State |
|---|---|
| the point node's step, drag, speed limit | ported; the same bits as the original |
| damage over the distance | verified against the original function in both precisions |
| ricochet and penetration | verified: 20,000 random cases a function and precision, the same bits |
| static objects in the point pass | verified on 249 of 249 recorded rays |
| impact outcome, messages, detonation | ported from the listing (a draft in the notes' words) |
| thrown projectiles, C4, mines | ported; equal to the original server in recorded runs |
| guided missiles | ported and verified on the TOW; the AA missiles fly as rigid bodies |
| the thrown bags (ammunition, medic) | not ported, by the notes' status line |
How it is tested#
- Oracle tests: the original's point node update, bone transform and ray against a skeleton are run in a process of its own on a list of inputs, and the port must give the same output bit for bit (the oracle) (
crates/bf2-world/tests/it_bf2/projectile_oracle.rs). - The flight's rules (
crates/bf2-world/src/projectile.rs): velocity components below a threshold zeroed, the segment after a ricochet starting on the contact "as the traced original", 2142's drag, the time to live drawn from the shared generator. - The damage chain (
crates/bf2-server/src/damage.rs, 33 tests): equal contacts taken in the check's order, what a removed kit's charges do, the kill's event members. - In play: the physics notes record rifle, knife and grenade hits as equal to the original's.
Thirteen of fourteen addresses stand in a test. The thrown bags are not ported and have none.
In the code#
| What | Where |
|---|---|
| the point node, the launch velocity, the message queue | crates/bf2-world/src/projectile.rs |
| ricochet and penetration | crates/bf2-world/src/projectile_comps.rs |
| thrown things | crates/bf2-world/src/projectile_rot.rs |
| damage over the distance | crates/bf2-gameplay/src/projectile.rs (DamageFalloff) |
| the chain on the server | crates/bf2-server/src/damage.rs, damage_explosion.rs |
| the oracle of the components | crates/bf2-world/tests/it_bf2/projectile_comps_oracle.rs |
Notes: notes/subsystems/gameplay-projectiles.md, notes/subsystems/physics-dynamics.md. See also a fire arm's components and deviation.
docs/dev/weapons/projectiles.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.