This post is somewhat a continuation to my constrained dynamics series:

In Constrained Dynamics (II) I argued that springs are a dead end for modeling rigid constraints. In this new series, however, springs are the whole point: we will be simulating deformable cars, which are anything but rigid.

This series grew out of sheer fascination with projects such as Rigs of Rods and BeamNG.drive, which simulate an entire vehicle as a lattice of nodes joined by springs and dampers, so that bodywork crumples, frames twist and suspensions flex the way real ones do.

Two beam cars crashing in 3D

Little sim as described in this series: Two node-and-beam roadsters in a head-on at 55 km/h each.

A tiny 2D beam car

Game-quality (3D) vehicles need hundreds of nodes and thousands of beams in 3D. But everything important can be explained and implemented with a super-simplified 2D car like the one above:

  • A box-ish chassis made of 10 nodes and 18 beams.
  • Two wheels, each one a ring of 16 nodes around a hub node.
  • A tiny drivetrain: engine, gear ratio, drive shaft.

All the animations and plots in this series come out of a small 2D simulator that implements exactly the C++ snippets in the posts, with exactly the constants given there. So if you assemble the snippets, you should get these very animations.

This is going to be a mini-series of three posts:

  • Part I (this one): building the car. Nodes, beams, why trusses matter, wheels as rings of nodes, how an engine torque becomes node forces, and the step loop.
  • Part II: making it drive well. Ground contact, friction, damping integrated implicitly per node, a drivetrain lesson, and why semi-implicit Euler.
  • Part III: the playground, and crashes. Crumple zones, plastic deformation, and a body that bends with the beams.

Why nodes and beams?

Most racing sims essentially model a car as a rigid body with four wheels attached by joints, with separate models for the tires, the drivetrain and the aerodynamic drag added on top. Done right, this delivers an excellent driving feel. But such cars won’t bend on impact. Real cars flex, twist over bumps, squat under power, and crumple when they crash.

An interesting lower-level alternative is to build the car’s structure out of point masses (nodes) connected by damped springs (beams). The whole vehicle becomes one big mass-spring network: the chassis, the suspension, the tires, everything. Flex, twist and roll come out for free because they are just the network doing its thing.

This approach was popularized by the open-source game Rigs of Rods (and later by its spiritual successor, BeamNG). These posts describe my own understanding and interpretation of the ideas and the math those projects popularized.

In Constrained Dynamics (II) the springs were a hack: I wanted a rigid distance constraint, and a spring could only approximate it with goopy (soft) or jittery (stiff) results. Here the springs are not approximating anything. The body is deformable, so a spring is the honest model of a metal beam that feels rigid until it is pushed past a certain level of stress.

The main price to pay is: stiff springs need very small time steps. Real beam cars must be simulated at thousands of steps per second. I use 2 kHz (2000 steps per second) in the Topotoy engine and here in these posts:

\[h=\Delta{t}=0.5\ \mathrm{ms}\quad(2000\ \mathrm{Hz})\]

Nodes

A node is the same particle as in Constrained Dynamics (I): a mass, a position, and a velocity. Here we will add a force accumulator, plus two things that will make sense later: a small 2x2 damping matrix \(\mathbf{C}\), and a predicted velocity \(\mathbf{w}\).

struct node_t
{
    f64_t cxx, cxy, cyy;  // Own damping matrix C (2x2, symmetric).
    f64_t m;              // Mass.
  vec2d_c p;              // Position.
  vec2d_c v;              // Velocity.
  vec2d_c F;              // Force accumulator.
  vec2d_c w;              // Predicted velocity (see the step loop).
};

Beams

A beam connects two nodes \(a\) and \(b\), and remembers its rest length \(L\) (when the car is built). It has a stiffness \(k_s\) and a damping \(k_d\).

struct beam_t
{
  s32_t a, b;  // Node indices.
  f64_t L;     // Rest length.
  f64_t ks;    // Stiffness (N/m).
  f64_t kd;    // Damping (N s/m).
};

The force is the damped spring from Constrained Dynamics (II), now between two moving nodes instead of a node and a fixed origin. Let:

\[\begin{flalign} & && \mathbf{d}=\mathbf{p}_b-\mathbf{p}_a & \\ & && l=\lVert \mathbf{d}\rVert & \\ & && \mathbf{u}=\frac{\mathbf{d}}{l} & \\ & && \dot{l}=(\mathbf{v}_b-\mathbf{v}_a)\cdot\mathbf{u} & \end{flalign}\]

\(\mathbf{u}\) is the unit direction from \(a\) to \(b\), and \(\dot{l}\) is how fast the beam is getting longer (positive = stretching). Then:

\[\begin{flalign} & && \mathbf{F}_a=\mathbf{u}\left(k_s(l-L)+k_d\dot{l}\right) & \\ & && \mathbf{F}_b=-\mathbf{F}_a & \end{flalign}\]

A stretched beam (\(l>L\)) pulls \(a\) towards \(b\) and \(b\) towards \(a\). A beam that is getting longer (\(\dot{l}>0\)) resists it. Same story in reverse for compression.

Reminder: In a spring–damper model, ks is the stiffness in Hooke’s law, determining the restoring force per unit extension, while kd is the damping coefficient, determining the resistance per unit rate of length change and representing real-world energy losses from internal friction, rubbing, or fluid resistance.

Two important properties:

  • Equal and opposite: the beam never adds net force to the car. Momentum is conserved.
  • Along the axis only: the damper only resists the stretching velocity \(\dot{l}\), not the sideways relative motion of the two nodes. If you damped the full relative velocity, every beam would also resist rotation, and the whole car would feel like it’s moving through honey.

The straightforward implementation is:

// Explicit beam forces.

for ( const beam_t& beam : beams )
{
  node_t& A = nodes[ beam.a ];
  node_t& B = nodes[ beam.b ];

  const vec2d_c D    = ( B.p - A.p );
  const   f64_t l    =   D.length();
  const vec2d_c u    = ( D / l );
  const   f64_t rate = ( ( B.v - A.v ) * u );

  const vec2d_c f    = ( u * ( ( beam.ks * ( l - beam.L ) ) + ( beam.kd * rate ) ) );

  A.F += f;
  B.F -= f;
}

Nodes are ball joints

A node has no orientation. Two beams meeting at a node can rotate freely around it, like a ball joint (a pin joint in 2D). So a beam network gets all its rigidity from its shape.

A square made of 4 beams has 4 nodes \(\times\) 2 coordinates \(=8\) degrees of freedom. A rigid body in 2D only has 3 (two translations and one rotation). The 4 beams remove 4 of the remaining 5… which leaves one free mode: shearing the square into a rhombus. Nothing resists it. So it folds.

Add the two diagonals and every face is a triangle. Triangles can’t change shape without changing the length of a side, which the beams resist. The box holds:

Triangulation

Both boxes: 4 nodes of 20 kg, \(k_s=10^6\) N/m, \(k_d=500\) N s/m, dropped with a 3 degree tilt. Same everything except the diagonals.

Rule of thumb: every face of a beam structure must be a triangle, unless you want that face to fold (e.g., a hinge).

That’s why our little car’s chassis is a strip of triangles:

// Chassis: 10 nodes of 40 kg, all beams ks = 2e6 N/m, kd = 5000 N s/m.

const vec2d_c chassis[ 10 ] =
{
  vec2d_c( -1.2 , 0.55 ), vec2d_c( -0.5 , 0.55 ), vec2d_c( 0.5 , 0.55 ), vec2d_c( 1.2 , 0.55 ),  // Bottom.
  vec2d_c( -1.2 , 0.95 ), vec2d_c( -0.5 , 0.95 ), vec2d_c( 0.5 , 0.95 ), vec2d_c( 1.2 , 0.95 ),  // Top.
  vec2d_c( -0.9 , 1.35 ), vec2d_c(  0.3 , 1.35 )                                                 // Roof.
};

const s32_t chassis_beams[ 18 ][ 2 ] =
{
  { 0, 1 }, { 1, 2 }, { 2, 3 }, { 4, 5 }, { 5, 6 }, { 6, 7 },  // Rails.
  { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 },                      // Posts.
  { 0, 5 }, { 1, 6 }, { 2, 7 },                                // Diagonals.
  { 8, 9 }, { 4, 8 }, { 5, 8 }, { 5, 9 }, { 6, 9 }             // Cabin.
};

Each beam’s rest length \(L\) is simply the distance between its two nodes at build time.

Wheels

A wheel is a hub node surrounded by a ring of \(N\) nodes at radius \(R\). Each ring node is tied to the hub by a spoke beam and to its neighbor by a tread beam. That’s \(N\) triangles around the hub, so the ring keeps its shape. But the whole ring can spin freely around the hub node, because the hub is a ball joint.

struct wheel_t
{
  s32_t hub;   // Hub node.
  s32_t arm;   // Chassis node that takes the drive reaction (-1 if none).

  std::vector< s32_t > ring;  // Ring nodes.
};

Our wheels: \(R=0.3\) m, \(N=16\), hub 10 kg, ring nodes 0.75 kg each.

wheel_t add_wheel( const vec2d_c& center, const s32_t arm )
{
  const f64_t R        = 0.3;
  const s32_t N        = 16;
  const f64_t spoke_ks = 1e5, spoke_kd = 1000;
  const f64_t tread_ks = 2e5, tread_kd =  500;

  wheel_t wheel;

  wheel.hub = add_node( center, 10 );
  wheel.arm = arm;

  for ( s32_t j = 0; j < N; ++j )
  {
    const f64_t angle = ( ( 2 * PI * j ) / N );

    wheel.ring.push_back( add_node( ( center + ( vec2d_c( cos( angle ), sin( angle ) ) * R ) ), 0.75 ) );
  }

  for ( s32_t j = 0; j < N; ++j )
  {
    add_beam( wheel.hub      , wheel.ring[   j           ], spoke_ks, spoke_kd );  // Spoke.
    add_beam( wheel.ring[ j ], wheel.ring[ ( j + 1 ) % N ], tread_ks, tread_kd );  // Tread.
  }

  return wheel;
}

add_node() and add_beam() just push into the arrays (the beam computes its rest length from the current node positions).

The suspension is simple: each hub is tied to two bottom chassis nodes by two softer beams (\(k_s=10^5\) N/m, \(k_d=2000\) N s/m). The rear hub sits at \((-0.85,0.3)\), tied to chassis nodes 0 and 1. The front hub at \((0.85,0.3)\), tied to nodes 2 and 3. That’s a triangle again (hub plus two chassis nodes), so the hub is located, but softly.

Total: 44 nodes, 86 beams, 444 kg.

Drive torque to node forces

The engine wants to apply a torque \(T\) to the wheel. But there is no “wheel body” with an angle to apply a torque to. There are only nodes, and nodes only take forces.

We therefore apply forces in two places:

  • Tangential drive forces on the outer ring nodes.
  • A balancing force on the hub.

First, we distribute the torque equally among the \(N\) ring nodes. For ring node \(j\):

\[\begin{flalign} & && \mathbf{r}_j=\mathbf{p}_j-\mathbf{p}_{hub} & \\ & && \rho_j=\lVert \mathbf{r}_j\rVert & \\ & && \hat{\mathbf{t}}_j=\frac{1}{\rho_j}\begin{bmatrix}r_{j,y}\\-r_{j,x}\end{bmatrix} & \end{flalign}\]

Here, \(\mathbf{r}_j\) points from the hub to the node, \(\rho_j\) is its current distance from the hub, and \(\hat{\mathbf{t}}_j\) is the unit tangent in the clockwise direction. Clockwise rotation is “forward” for a wheel rolling to the right (\(+x\)), so we define positive \(T\) as clockwise.

Each ring node gets:

\[\mathbf{f}_j=\frac{T}{N\rho_j}\hat{\mathbf{t}}_j\]

The force is perpendicular to the radius, so each node contributes a clockwise torque of \(T/N\) about the hub. Together, the \(N\) forces produce exactly the requested torque \(T\). We divide by each node’s actual radius \(\rho_j\) so this remains true when the ring deforms.

However, we want these forces to turn the wheel without also introducing a net push. On a perfectly round, evenly spaced ring, the forces already sum to zero. On a deformed ring, they may not, so we apply the opposite of their sum to the hub:

\[\mathbf{f}_{hub}=-\sum_j\mathbf{f}_j\]

The forces on the ring and hub now add up to zero net force. The hub force produces no torque about the hub because it acts directly at that point, so the turning effect remains unchanged.

This is what pure torque means here: the motor produces a turning effect without directly pushing the wheel as a whole. The forward force that propels the vehicle comes from contact with the ground.

Here’s a lone wheel with \(T=8\) N m (red arrows: the drive forces). The yellow trace follows one ring node:

A wheel

The reaction

Newton’s 3rd Law: when the engine applies a torque to the wheel, it experiences an equal and opposite torque, which is transmitted to the chassis. The engine is bolted to the chassis, so the chassis must receive \(-T\). Otherwise the vehicle could spin up its wheels for free and the energy books wouldn’t balance.

We must apply \(-T\) as a couple between the hub and one chassis node (the arm).

With \(\mathbf{r}_{arm}=\mathbf{p}_{arm}-\mathbf{p}_{hub}\):

\[\begin{flalign} & && \mathbf{f}_{arm}=\frac{-T}{\lVert \mathbf{r}_{arm}\rVert ^2}\begin{bmatrix}r_{arm,y}\\-r_{arm,x}\end{bmatrix} & \\ & && \mathbf{f}_{hub}\mathrel{-}=\mathbf{f}_{arm} & \end{flalign}\]

Two equal and opposite forces: zero net force, moment \(-T\) about the hub.

void apply_wheel_torque( const wheel_t& wheel, const f64_t T )
{
  node_t& hub = nodes[ wheel.hub ];

  const s32_t N = static_cast< s32_t >( wheel.ring.size() );

  vec2d_c net( 0 );

  for ( const s32_t j : wheel.ring )
  {
    node_t& node = nodes[ j ];

    const vec2d_c r = ( node.p - hub.p );

    const vec2d_c f = ( vec2d_c( r.y, -r.x ) * ( T / ( N * ( r * r ) ) ) );  // |f| = T / ( N rho ).

    node.F += f;
    net    += f;
  }

  hub.F -= net;

  if ( wheel.arm >= 0 )
  {
    node_t& arm = nodes[ wheel.arm ];

    const vec2d_c r = ( arm.p - hub.p );

    const vec2d_c f = ( vec2d_c( r.y, -r.x ) * ( -T / ( r * r ) ) );

    arm.F += f;
    hub.F -= f;
  }
}

My toy uses the nearest chassis node above each axle as the arm: node 1 for the rear wheel and node 2 for the front wheel.

Depending on how the drivetrain transmits the reaction torque, only a fraction may act through this particular connection. If the assumed geometry gives that fraction as follows, I apply it as a couple between the arm and the nearer axle node:

\[\frac{1}{2}\left(1-\frac{2o}{l}\right)T\]

Here, \(l\) is the distance from the arm to the axle, and \(o\) is its lateral offset along the axle. That offset is a 3D feature: our 2D toy has no lateral dimension. The rest of the reaction torque must be transmitted through other connections.

Here’s what happens when I apply the full reaction in the toy model of a light motorbike with a heavy rider sitting high up. The ground pushes the driven rear wheel forward, and the resulting pitching motion lifts the front wheel: a wheelie! When the throttle opens again a little later, the bike rotates too far backward: a loop-out.

Motorbike

Measuring wheel spin

The drivetrain will need to know how fast each wheel spins. There is no angle to differentiate, so I measure it from the ring nodes, averaging the tangential relative velocity over the actual radius:

\[\omega=\frac{1}{N}\sum_j\frac{(\mathbf{v}_j-\mathbf{v}_{hub})\cdot\hat{\mathbf{t}}_j}{\rho_j}\]
f64_t wheel_spin( const wheel_t& wheel )
{
  const node_t& hub = nodes[ wheel.hub ];

  f64_t sum = 0;

  for ( const s32_t j : wheel.ring )
  {
    const node_t& node = nodes[ j ];

    const vec2d_c r   = ( node.p - hub.p );
    const   f64_t rho =   r.length();
    const vec2d_c t   = ( vec2d_c( r.y, -r.x ) / rho );

    sum += ( ( ( node.v - hub.v ) * t ) / rho );
  }

  return ( sum / wheel.ring.size() );
}

Clockwise is positive, so \(\omega R\) matches the forward speed of a rolling wheel.

The step loop

Here’s the whole thing, in order. Two of its steps are the subject of part II: the per-node velocity solve (step 4: for now, read it as plain semi-implicit Euler, \(\mathbf{w}=\mathbf{v}+\frac{h}{m}\mathbf{F}\)) and the ground contact (step 5). The drivetrain in step 3 is in part II too. This order matters: the contact must come after every other force is known.

void car_t::step( const f64_t h )
{
  const vec2d_c gravity( 0, -9.81 );

  // 1. Reset the accumulators: gravity, and no damping.

  for ( node_t& node : nodes )
  {
    node.F   = ( gravity * node.m );
    node.cxx = node.cxy = node.cyy = 0;
  }

  // 2. Beams: springs explicit, own-node damping into C.

  beam_forces();

  // 3. Drivetrain: wheel spin in, torque out, to the ring nodes.

  const f64_t omega_axle = ( ( wheel_spin( wheels[ 0 ] ) + wheel_spin( wheels[ 1 ] ) ) / 2 );
  const f64_t T          = ( driveline.step( h, omega_axle ) / 2 );

  for ( const wheel_t& wheel : wheels )
    apply_wheel_torque( wheel, T );

  // 4. Per node velocity solve: ( I + h/m C ) w = v + h/m F.

  solve_velocities( h );

  // 5. Ground contact, last, on w.

  std::vector< vec2d_c > recovery( nodes.size(), vec2d_c( 0 ) );

  for ( size_t i = 0; i < nodes.size(); ++i )
    contact( nodes[ i ], h, recovery[ i ] );

  // 6. Semi-implicit Euler, plus the depth recovery (position only).

  for ( size_t i = 0; i < nodes.size(); ++i )
  {
    node_t& node = nodes[ i ];

    node.v  =   node.w;
    node.p += ( ( node.v * h ) + recovery[ i ] );
  }
}

With \(h=0.0005\) s. The opening animation of this post is exactly this, with \(G=12\), 60 N m of peak engine torque, and a viscous coupling (\(k=0\), \(c=1000\)), over a few smooth bumps.

Next

The car is built. It has a body that bends, wheels that roll, and an engine that turns them. In part II I’ll make it drive well: how the tire nodes meet the ground, friction, damping that doesn’t blow up, a drivetrain that doesn’t behave like a rubber band, and why the integrator is semi-implicit Euler.

Stay tuned!