BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Projectiles: flight, impact and damage over the distance

BF22142Heroes

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.

A projectile, from the muzzle to its end the launch velocity = launch speed x forward + the shooter's own velocity; the deviation turns it each tick: the step drag, then the new velocity and position, then gravity for the next step each tick: the collision pass the segment it moved against terrain, static objects, vehicles, soldiers' bone capsules a hit: bounce, ricochet or penetrate by the material and two draws seeded with the tick; a penetration costs damage the end the detonation, the impact effect, and the damage by the distance from the launch bf2_w32ded.exe!0x005c0e00 (launch), 0x006a81c0 (step), 0x005b3240 (collision), 0x0056a530 (impact), 0x005b1a70 (damage)
A projectile's life. The upper rows happen once, the middle rows every tick, the lower rows at the 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 typeNodeUsed for
Point (1)a point with velocity: no rotation, no sizebullets, shells, most rockets
RotationalPoint (2)a point that also spins and has a bounding radiushand grenades and other thrown things
Mesh (3)a full rigid body with a collision meshclaymores, AT mines, the AA missiles
ComponentScript prefixWhat it adds
collisioncollision.*what a hit does; bouncing; sticking (C4)
detonationdetonation.*the explosion: damage, radius, force, triggers, the arming
ricochet, penetratewhether a hit sends the projectile on or lets it through
seek, followseek.*, follow.*a target and steering toward it (guided missiles)
soundthe 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

text
velocity = launch speed x forward + the velocity of the shooter's root node

So 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:

text
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 step

Gravity 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
Battlefield 2142 and Heroes

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):

OutcomeWhenThen
bounceon the terrain, slower than minDetonationSpeed; or a bouncing collision component on a material without water physicsthe projectile goes on
ricochetthe ricochet test says soDefaultRicochetComp::ricochet (0x005b9720) reflects it, spread by 0.2
penetrateotherwiseDefaultPenetrateComp::penetrate (0x005b8e80) casts a ray out of the object's back; the projectile leaves there with less damage, or ends
endneither lets it go onthe 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):

text
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#

PartState
the point node's step, drag, speed limitported; the same bits as the original
damage over the distanceverified against the original function in both precisions
ricochet and penetrationverified: 20,000 random cases a function and precision, the same bits
static objects in the point passverified on 249 of 249 recorded rays
impact outcome, messages, detonationported from the listing (a draft in the notes' words)
thrown projectiles, C4, minesported; equal to the original server in recorded runs
guided missilesported 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#

WhatWhere
the point node, the launch velocity, the message queuecrates/bf2-world/src/projectile.rs
ricochet and penetrationcrates/bf2-world/src/projectile_comps.rs
thrown thingscrates/bf2-world/src/projectile_rot.rs
damage over the distancecrates/bf2-gameplay/src/projectile.rs (DamageFalloff)
the chain on the servercrates/bf2-server/src/damage.rs, damage_explosion.rs
the oracle of the componentscrates/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.

Source: docs/dev/weapons/projectiles.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.