Simulating a flexible beam in Godot with GDBlas

Submitted by dmrokan on

GDBlas is a native Godot Game Engine plugin which adds mathematical routines to the engine, such as

  • Real and complex linear algebra routines
  • Running real and complex standard library functions on vector and matrices
  • Ordinary differential equation (ODE) solver
  • Geometric algorithms that operate on arbitrary 2D shapes

GDBlas uses Eigen and Boost C++ as the backends for these mathematical operations. Its GDScript bindings documentation can be found here.

An example

Visualization of the dynamics of a flexible beam is an interesting showcase about the capabilities of GDBlas. This example project can be downloaded from the releases page and can be directly imported to the Godot Engine.

It simulates and visualizes the deformation of an elastic object under force. A real life example can be the bending of a bridge under wind load (collapse of Tacoma Bridge).

Flexible beams have well defined mathematical models which can accurately simulate their physical behavior. Its mathematical model can be represented by this ODE:

$$\dot{x}(t)=A_cx(t)+B_cu(t) ~~ \textrm{(1)}$$ $$y(t)=Cx(t)$$

where \(u\) is the input force, \(y\) is the output vector in which each entry represents the displacement of the points on the beam and \(x\) is a vector of system's internal states. It can be discretized in time and can be put in the form below

$$x_{n+1}=Ax_n+Bu_n ~~ \textrm{(2)}$$ $$y_n=Cx_n$$

Simulation steps are briefly as follows:

  1. Generate system matrices \(A_c,B_c\) and C by using the block matrix operations of GDBlas.
  2. Discretize the system matrices to obtain \(A\) and \(B\) by using matrix product and inversion operations.
  3. Continuously iterate Equation 2 above in _process method of Node2D instance to obtain \(y_t\).
  4. Use the discrete position values in \(y_t\) and cubic bezier curve method to draw the flexible beam.

Result

The animation below shows the result of simulation.

Dynamics of a flexible beam

Fun result

It is a game engine. Why would you limit your self by drawing a line? You can make it fun. The frame around the toad is generated by using the GDBlas's buffer method which is based on line buffer algorithm of Boost Geometry.

Dynamics of a sad toad