Skip to content

Add Acceleration to a C++ Simulation Before Refactoring

The new request is that some moving bodies accelerate. Implement it in records first, so architectural comparisons address a change we have actually made.

Specify the Rule

Acceleration is optional and requires movement. Stationary acceleration is rejected. Its values must be finite.

1
2
3
4
velocity += acceleration * dt
position += updated_velocity * dt
remaining -= dt
remove expired bodies

This chosen discrete integration rule is not the exact continuous-motion formula. Changing it is a behaviour change, not a harmless refactor.

Calculate Before Running

A starts at x=0, vx=1, ax=2. With dt=0.5:

Step Updated vx New x
1 1 + 2 × 0.5 = 2 0 + 2 × 0.5 = 1
2 2 + 2 × 0.5 = 3 1 + 3 × 0.5 = 2.5

B starts at x=10, vx=-1, is invisible, and has lifetime=1. It moves twice before removal. C is stationary and visible at (5,5). These shared fixtures are in model.hpp.

Inspect the Actual Change

Seed extends the input record with optional acceleration. validate_seed rejects acceleration without movement. advance updates velocity before position.

Seed : BasicSeed reuses fields in the input record; it is not the polymorphic motion hierarchy compared in chapter 4. The optional value distinguishes “no acceleration capability” from “acceleration happens to be zero.”

The actual update in model.hpp is short enough to trace directly:

1
2
3
4
5
inline void advance(Seed& body, double dt) {
    if (body.acceleration) accelerate(body.velocity, *body.acceleration, dt);
    if (body.moving) move(body.position, body.velocity, dt);
    if (body.expiring) body.remaining -= dt;
}

The world validates input and dt before calling this helper. Cleanup follows the loop; advance does not erase the record it is updating.

Edit point Reason
Seed input Represent optional acceleration
Validation Reject a meaningless combination
Advance Establish acceleration-before-movement
Observation Include acceleration in comparisons
Tests Check calculated motion and retained behaviour

baseline.hpp remains an executable no-acceleration reference. It does not secretly contain the new capability.

Run and Check

/tmp/pi-design-main/design_demo acceleration
/tmp/pi-design-main/design_tests acceleration

The first two output lines are:

t=0.5 x=1 vx=2 survivors=3
t=1 x=2.5 vx=3 survivors=2

The demo also prints alternative designs' final x values. The test independently checks the numbers above and compares revised records with the baseline when acceleration is absent.

Follow One Writer Chain

For A, validation accepts movement plus acceleration; advance changes velocity; move changes position; lifetime does nothing. Rendering reads the result if visible.

The chain is still short. Another abstraction is justified only if it improves a requirement we care about.

Exercise

Predict movement-before-acceleration: the first x would be 0.5, not 1. Temporarily put that wrong expectation in a local test and observe failure; restore the correct expectation before continuing.

Next: compare representations without changing the mathematics.


Previous: Chapter 2 · Course overview · Next: Chapter 4

Donate