Simulating a mechanical system using RGSS3
Our goal is to simulate a mechanical system according to its Hamiltonian .
To utilize the canonical equations we need to calculate the partial derivatives of . Here is a simple code to calculate partial derivatives.
1
2
3
4
5
6
7
|
|
(RGSS do not have matrix.rb, you can copy one from the attached file below.) Here x0 is a Vector, f is a call-able object as a function of vectors, dx is a small scalar which we are going to take 1e-6.
Let x = Vector[*q, *p], and then Formula 1 has the form To solve this equation numerically, we need to use a famous method called the (explicit) Runge–Kutta method.
1
2
3
4
5
6
7
8
|
|
Note that Runge–Kutta is a family of methods. The argument (*pyramid, coefs) takes one of the following, each of which is a single Runge–Kutta method.
1
2
3
4
5
6
7
8
9
10
11
12
|
|
Here we are going to take CLASSIC_4TH.
The $canvas appearing here is an object that is going to draw the result onto the screen.
Here we also need to have some patches to get it work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
|
(Again, note that this is Ruby 1.9.2.)
Finally, just combine them up, and we can solve a Hamiltonian numerically.
1
2
3
4
5
6
|
|
For example, let’s simulate a double pendulum.
1
2
|
|
The codes are not complete in this post. See the attached file for details. You can open the project using RPG Maker VX Ace. The Game.exe file is not the official Game.exe executable but the third-party improved version of it called RGD (of version 1.3.2, while the latest till now is 1.5.1).