<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Integrator on My Blog</title><link>https://EMEEEEMMMM.github.io/tags/integrator/</link><description>Recent content in Integrator on My Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 13 Mar 2026 23:07:28 +0800</lastBuildDate><atom:link href="https://EMEEEEMMMM.github.io/tags/integrator/index.xml" rel="self" type="application/rss+xml"/><item><title>The Integrator</title><link>https://EMEEEEMMMM.github.io/posts/theintegrator/</link><pubDate>Fri, 13 Mar 2026 23:07:28 +0800</pubDate><guid>https://EMEEEEMMMM.github.io/posts/theintegrator/</guid><description>&lt;img src="https://EMEEEEMMMM.github.io/" alt="Featured image of post The Integrator" /&gt;&lt;h2 id="the-physical-model"&gt;The Physical Model
&lt;/h2&gt;&lt;p&gt;All integration algorithms are derived from Newton&amp;rsquo;s second law of motion. For a particle subjected to a force $F$:&lt;/p&gt;
$$
\begin{align}
F = ma = m \frac{d^2x}{dt^2}
\end{align}
$$&lt;p&gt;We usually split it into two first-order differential equation:&lt;/p&gt;
$$
\left\{
\begin{aligned}
\frac{dx}{dt} &amp; = v \\
\frac{dv}{dt} &amp; = a = \frac{F_{net}}{m}
\end{aligned}
\right.
$$&lt;h3 id="explicit-euler"&gt;Explicit Euler
&lt;/h3&gt;&lt;p&gt;This is the most intuitive solution, assume the step is $\Delta t$:
&lt;/p&gt;
$$
x_{n+1} = x_{n} + v_{n} \Delta t \\
v_{n+1} = v_{n} + a_{n} \Delta t
$$&lt;p&gt;But it is extremely unreliable since it actually assumes that in a step, the velocity and the acceleration are constant.&lt;/p&gt;
&lt;h3 id="semi-implicit-euler"&gt;Semi-Implicit Euler
&lt;/h3&gt;&lt;p&gt;This is what I used in my project and it has just a slightly change compare to Explicit Euler, it updates the velocity and uses the new velocity to update the position.&lt;/p&gt;
$$
v_{n+1} = v_{n} + a_{n} \Delta t \\
x_{n+1} = x_{n} + v_{n+1} \Delta t
$$&lt;p&gt;It is much more reliable than the Explicit Euler.&lt;/p&gt;
&lt;p&gt;Note: Rk4 and Implicit Euler is not considered here since they much more complicated than the above two methods and in most cases, semi-implicit euler can do its job pretty well.&lt;/p&gt;
&lt;h2 id="the-moment-of-inertia-matrix"&gt;The Moment of Inertia Matrix
&lt;/h2&gt;&lt;p&gt;In 3D space, the &amp;ldquo;resistance to rotation&amp;rdquo; of an object is different for different axis. This cannot be expressed by a constant, but rather as a matrix of 3x3, which completely describes the distribution of mass in all directions. As long as the shape of the object does not collapse, this $I_{body}$ will not change.&lt;/p&gt;
&lt;p&gt;For the world coordinate system, the &amp;ldquo;resistance to rotation&amp;rdquo; of the object is keep changing with its posture. Which means that, we must convert the $I_{body}$ to the $I_{world}$ all the time so that we can calculate the effect of the external forces on it correctly.&lt;/p&gt;
&lt;p&gt;We know the Angular Momentum Theorem:&lt;/p&gt;
$$
L = I \omega
$$&lt;p&gt;It is true for both the world space and the local space:
&lt;/p&gt;
$$
\begin{align}
L_{local} &amp;= I_{body} \omega_{local} \\
L_{world} &amp;= I_{world} \omega_{world} 
\end{align}
$$&lt;p&gt;To convert a vector from the local space to the world space, multiply current rotation matrix $R$:&lt;/p&gt;
$$
\begin{align}
L_{world} &amp;= R L_{local} \\
\omega_{world} &amp;= R \omega_{local} \\
\omega_{local} &amp;= R^{-1} \omega_{world}
\end{align}
$$&lt;blockquote class="alert alert-note"&gt;
 &lt;div class="alert-header"&gt;
 &lt;span class="alert-icon"&gt;📝&lt;/span&gt;
 &lt;span class="alert-title"&gt;Note:&lt;/span&gt;
 &lt;/div&gt;
 &lt;div class="alert-body"&gt;
 &lt;p&gt;&lt;/p&gt;
$$
\left[
\begin{matrix}
R_{11} &amp; R_{12} &amp; R_{13} \\
R_{21} &amp; R_{22} &amp; R_{23} \\
R_{31} &amp; R_{32} &amp; R_{33}
\end{matrix}
\right]
\left[
\begin{matrix}
x_{local} \\
y_{local} \\
z_{local}
\end{matrix}
\right] = \left[
\begin{matrix}
x_{world} \\
y_{world} \\
z_{world}
\end{matrix}
\right]
$$
 &lt;/div&gt;
 &lt;/blockquote&gt;
&lt;p&gt;Substitutes:&lt;/p&gt;
$$
\begin{align}
L_{world} &amp;= R (I_{body} \omega_{local}) \\
L_{world} &amp;= R I_{body} R^{-1} \omega_{world}
\end{align}
$$&lt;p&gt;So $I_{world}$ is equal to:&lt;/p&gt;
$$
I_{world} = R I_{body} R^{-1}
$$&lt;p&gt;And in the physics engine, we care about its inverse matrix because we need it to calculate the angular acceleration.&lt;/p&gt;
$$
\begin{align}
I_{world}^{-1} &amp;= (R I_{body} R^{-1})^{-1} \\ 
I_{world}^{-1} &amp;= (R^{-1})^{-1} I_{body}^{-1} R^{-1} \\
R^{-1} &amp;= R^T \\
I_{world}^{-1} &amp;= R I_{body}^{-1} R^T \\
\end{align}
$$&lt;blockquote class="alert alert-note"&gt;
 &lt;div class="alert-header"&gt;
 &lt;span class="alert-icon"&gt;📝&lt;/span&gt;
 &lt;span class="alert-title"&gt;Note: Replace the $R^{-1}$ by $R^T$ is because calculate $R^T$ is much faster than $R^{-1}$.&lt;/span&gt;
 &lt;/div&gt;
 &lt;div class="alert-body"&gt;
 
 &lt;/div&gt;
 &lt;/blockquote&gt;
&lt;h2 id="the-angular-acceleration"&gt;The Angular Acceleration
&lt;/h2&gt;$$
\begin{align}
\tau &amp;= \frac{dL}{dt} \\
L &amp;= I \omega 
\end{align}
$$&lt;p&gt;Assume $I$ stays constant in a short period of time:
&lt;/p&gt;
$$
\tau = \frac{d(I\omega)}{dt} = I\frac{d\omega}{dt} \\
\tau = I \alpha
$$&lt;p&gt;In 3D space, $\tau$ and $\alpha$ are vectors with size 3x1. $I$ is a matrix of size 3x3.&lt;/p&gt;
$$
\left[
\begin{matrix}
\tau_{x} \\
\tau_{y} \\
\tau_{z}
\end{matrix}
\right]
= \left[
\begin{matrix}
I_{xx} &amp; I_{xy} &amp; I_{xz} \\
I_{yx} &amp; I_{yy} &amp; I_{yz} \\
I_{zx} &amp; I_{zy} &amp; I_{zz} \\
\end{matrix}
\right]
\left[
\begin{matrix}
\alpha_{x} \\
\alpha_{y} \\ 
\alpha_{z}
\end{matrix}
\right] \\
\begin{align}
I^{-1} \tau &amp;= (I^{-1}I)\alpha \\
I^{-1} \tau &amp;= \alpha \\
\alpha &amp;= I_{world}^{-1} \tau \\
\omega_{n+1} &amp;= \omega_{n} + \alpha \Delta t
\end{align}
$$&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;So, for each object in every time step, these should be updated by the integrator:
&lt;/p&gt;
$$
\begin{align}
v_{n+1} &amp;= v_{n} + a_{n} \Delta t \\
x_{n+1} &amp;= x_{n} + v_{n+1} \Delta t \\
I_{world}^{-1} &amp;= R I_{body}^{-1} R^T \\
\alpha &amp;= I_{world}^{-1} \tau \\
\omega_{n+1} &amp;= \omega_{n} + \alpha \Delta t
\end{align}
$$</description></item></channel></rss>