On this page
Animation: triggers, bundles, the skeleton and the ragdoll
A soldier's pose is not decoration. A bullet is tested against capsules on his bones, so where his arm is at the tick of a shot decides whether it is hit. The animation runtime therefore runs on the dedicated server too, and the port holds it to the original bit for bit: it is physics by another name.
An animation system is a script#
Each soldier template names two animation systems, one for first person and one for third (ObjectTemplate.animationSystem1P / animationSystem3P); weapons and seats name theirs. A system is a console script that builds three kinds of things (ModuleBoneAnimation::init, bf2_w32ded.exe!0x0070d53f):
| Thing | What it is | Script object |
|---|---|---|
| animation | one .baf file: for each bone a track of rotations and positions at 24 frames a second | animationManager.* |
| bundle | one or more animations that play together, with a length, fade-in (0.2 s) and fade-out (0.1 s), looping or not, and events (a footstep, a sound) at set times | animationBundle.* |
| trigger | a node of a decision tree that picks bundles from the soldier's state | animationTrigger.* |
Animations are shared by every system of the process: a script that sets the length of a file sets it for all users, and the last one wins. At a level's load every system's script runs in name order, then every animation that some script named is loaded. The port runs every shipped animation script through its own interpreter and through the original's, with no error in either.
What a trigger sees#
Each frame the soldier hands its animation instance four things (updateAnimationSystems, 0x005bd420; setMovement, 0x00719380):
- the direction of movement in the frame of his camera: side, up, forward;
- the speed, horizontal; in first person scaled down when crouched or prone;
- the look and turn input;
- the pose: his stance, 0 to 3 (stances).
And messages: bits that say "firing", "reloading", "zoomed", "jumping", which the weapon and the soldier set or pulse for one update.
The direction does not jump. It blends toward the new one by dt x anim_smoothSet, and snaps only when the soldier was standing still or reverses. From the old and new velocity the instance also keeps a deceleration, which a trigger can ask for: that is how a soldier leans when he stops.
A change of pose runs a transition trigger by name: stillToProne when slow, else forwardToProne, backwardToProne, leftToProne, rightToProne by the direction; fromProne on the way up.
One update#
AnimationSystem::update (0x00719e50):
- the first time, the
startuptrigger runs; - the tree is evaluated from
root, then a second tree frompostRoot(layers on top: the aim, the head); - every playing bundle advances; one that ends is removed; a looping bundle that no trigger asked for again this update starts to fade out. A trigger must want a looping bundle every update to keep it;
- an idle clock runs while exactly one bundle plays, so that an idle trigger can start a fidget after some seconds.
Idle fidgets and the head's idle turn draw from the C runtime's random number, shared by the whole process. On the server this stream is part of the state that must match: the port's draws equal the original's in 696 of 696 on a ladder run and 68 of 68 in a hit test run.
Onto the skeleton#
A bundle pushes layers to the skeleton: (animation, time, weight). Skeleton::updateFromAnimations then computes each bone:
- Sampling (
BoneAnimation::sample,0x007206e0): the time picks two frames and a fraction; rotations are interpolated on the sphere (Quaternion::slerp,0x0068d630), positions in a line. - Distribution (
0x00716f30): a layer goes to every bone its animation has a track for. A bone keeps at most five layers; a layer of weight 1 discards the ones before it. - Blending (
0x00716fc0): the first layer replaces, the others blend in by their weight. - World matrices: each bone's local transform times its parent's.
The skeleton is persistent: a bone that no layer reaches keeps the transform it had. That is what a dead soldier's body relies on: the body bundles end and the pose stays.
The aim#
In third person the upper body follows the camera (0x005bdcc0): a pitch and a yaw are taken from the angle between the soldier's frame and his camera's forward direction, and applied over the animated pose. Swimming gives pitch 0. The head adds an idle turn: it holds, picks a target a little to one side, and moves there at a speed proportional to the distance.
In first person the camera sits on a spring instead (updateFirstPersonAnimations, BF2.exe!0x0061d8a0), so the weapon lags and settles when the view turns. Arms that must hold a weapon in two places are solved by inverse kinematics.
Why the server animates#
Soldier::getSkeleton3PForPose (0x0054dfb0) is called by the collision code: the soldier's collision response, the projectile pass and the projectile's collision components. On the host, once per tick per soldier, it evaluates the third-person skeleton with a step of 1/30 s; the capsules on its bones (setSkeletonCollisionBone) are what a bullet is tested against. For a remote player the server first rewinds by his measured ping, so that the shooter hits what he saw (lag compensation).
The ragdoll#
When a soldier goes down outside a vehicle, his body becomes a ragdoll (RagDoll.cpp, update 0x0070b690): 13 particles on chosen bones, held together by 18 distance constraints, 12 angular and 4 dihedral ones, with 12 capsules for the limbs. It steps at a fixed 1/90 s from an accumulator: gravity, forces, a Verlet step, the constraints, collisions against objects and the terrain, then a sleep test. Its start velocity is the soldier's at the moment of death, so a soldier shot while running keeps his motion. The ragdoll's particles then drive the skeleton's bones.
What the port has#
The notes' status, which is unusually complete for this subsystem:
| Part | Check |
|---|---|
| sampling, slerp | 139,176 cases a precision against the original functions, the same bits |
| bundle playback, layers | 20,000 random bundles and states a precision |
| trigger evaluation, the instance's update | 10,000 random trigger trees a precision |
| the skeleton's pose | 1,200 skeleton updates on the shipped skeletons and animations |
| arm IK, the ragdoll and its bone driver, the death camera | verified against the original functions |
| the soldier's inputs, the first-person spring | verified |
| server poses, moving, crouched and prone | 606 of 606 hit-test skeletons identical to the original server's |
| the soldier's aim | implemented from the instructions; the function itself is not run against the original |
| seated soldiers | implemented; one seated target compared with the original server |
How it is tested#
- Oracle tests: the original's matrix and quaternion primitives, skeleton poses, trigger trees, bundle playback and sampler 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-anim/tests/oracle.rs); a second program does the same for the soldier's animation update and the first person spring (soldier_oracle.rs). - The aim (
crates/bf2-anim/tests/aim.rs): look limits per game, the quarter of the pitch and third of the yaw the extra transforms take. Its expectations are derived from the instruction sequence, not produced by running the original: the page's table says so. - On the server (
crates/bf2-server/src/damage_anim.rs): the skeleton evaluated for hit detection, the ragdoll's variables.
Ten of twelve addresses stand in a test.
In the code#
| What | Where |
|---|---|
| the runtime | crates/bf2-anim (sample, system, system::script, system::runtime, pose, aim, soldier, first_person, ik, ragdoll) |
| the server's soldier poses | crates/bf2-server/src/damage_anim.rs |
| the file formats | animation files |
Notes: notes/subsystems/animation.md, notes/formats/animation.md.
docs/dev/soldier/animation.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.