1. 概要

最近用Manim制作一个数学动画时,用到了一些旋转的动画。

使用Manim来实现单个物体的旋转,或者绕着固定物体旋转都很简单,
但是实现多个物体之间旋转的联动则稍微麻烦一些,
这里借用太阳,地球,月亮的自转公转来介绍下多个物体旋转联动的实现思路,以供参考。

2. 地球自转

地球每天自己旋转一天就是自转。
在Manim中,自转就是绕着自己的中心点旋转。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from manim import *
import numpy as np
def make_earth(r):
g = VGroup(Circle(radius=r, color=BLUE_D, fill_color=BLUE_C,
fill_opacity=1, stroke_width=2))
for dx, dy, w, h in ((-0.35, 0.3, 0.75, 0.5), (0.38, -0.22, 0.6, 0.42),
(-0.1, -0.55, 0.42, 0.26)):
e = Ellipse(width=w * r, height=h * r, color=GREEN_B,
fill_opacity=1, stroke_width=0)
e.shift(r * np.array([dx, dy, 0]))
g.add(e)
return g
class Demo(Scene):
def construct(self):
earth = make_earth(1.2)
self.play(FadeIn(earth), run_time=0.6)
self.play(Rotate(earth, angle=TAU, axis=IN, run_time=4.0, rate_func=linear))
self.wait(1.0)

下载 Python 脚本

下载 Python 脚本

3. 地球绕太阳公转

地球绕着太阳转就是公转。
和自转的实现类似,公转就是围绕指定的中心点旋转。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from manim import *
import numpy as np
def make_earth(r):
g = VGroup(Circle(radius=r, color=BLUE_D, fill_color=BLUE_C,
fill_opacity=1, stroke_width=2))
for dx, dy, w, h in ((-0.35, 0.3, 0.75, 0.5), (0.38, -0.22, 0.6, 0.42),
(-0.1, -0.55, 0.42, 0.26)):
e = Ellipse(width=w * r, height=h * r, color=GREEN_B,
fill_opacity=1, stroke_width=0)
e.shift(r * np.array([dx, dy, 0]))
g.add(e)
return g
def make_sun(r):
g = VGroup(Circle(radius=r, color=YELLOW, fill_opacity=1, stroke_width=0))
for i in range(12):
a = i * TAU / 12
p1 = r * 1.25 * np.array([np.cos(a), np.sin(a), 0])
p2 = r * 1.7 * np.array([np.cos(a), np.sin(a), 0])
g.add(Line(p1, p2, color=YELLOW, stroke_width=6))
return g
class Demo(Scene):
def construct(self):
sun = make_sun(1.0).shift(DOWN * 1.0)
earth = make_earth(0.42).shift(UP * 1.6)
self.play(FadeIn(sun), FadeIn(earth), run_time=0.6)
self.play(Rotate(earth, about_point=sun.get_center(), angle=TAU,
axis=IN, run_time=4.0, rate_func=linear))
self.wait(1.0)

下载 Python 脚本

下载 Python 脚本

4. 太阳,地球和月亮联动

地球绕着太阳转,月亮绕着地球转。
地球绕着太阳旋转和上一步一样,重点在于如何控制月亮的旋转。

我实现的思路是:

  1. 计算地球相对于太阳旋转的角度
  2. 设置此时月亮相对于地球旋转的角度
  3. 通过Manim的update机制,当地球旋转 角度之后
  4. 更新月亮相对于地球旋转的角度
  5. 下面的示例中,设置了 (设置太大,月亮会旋转太快看不清)
  6. 所以地球绕太阳1圈时,月亮绕了地球5圈。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from manim import *
import numpy as np
def make_earth(r):
g = VGroup(Circle(radius=r, color=BLUE_D, fill_color=BLUE_C,
fill_opacity=1, stroke_width=2))
for dx, dy, w, h in ((-0.35, 0.3, 0.75, 0.5), (0.38, -0.22, 0.6, 0.42),
(-0.1, -0.55, 0.42, 0.26)):
e = Ellipse(width=w * r, height=h * r, color=GREEN_B,
fill_opacity=1, stroke_width=0)
e.shift(r * np.array([dx, dy, 0]))
g.add(e)
return g
def make_sun(r):
g = VGroup(Circle(radius=r, color=YELLOW, fill_opacity=1, stroke_width=0))
for i in range(12):
a = i * TAU / 12
p1 = r * 1.25 * np.array([np.cos(a), np.sin(a), 0])
p2 = r * 1.7 * np.array([np.cos(a), np.sin(a), 0])
g.add(Line(p1, p2, color=YELLOW, stroke_width=6))
return g
class Demo(Scene):
def construct(self):
sun = make_sun(0.75).shift(DOWN * 0.8)
earth = make_earth(0.42).shift(UP * 1.5 + LEFT * 0.3)
moon = Circle(radius=0.16, color=GREY_B, fill_opacity=1,
stroke_width=0).shift(UP * 2.1 + LEFT * 1.2)
system = VGroup(earth, moon)
self.play(FadeIn(sun), FadeIn(system), run_time=0.6)
self.play(Rotate(system, about_point=sun.get_center(), angle=TAU,
axis=IN, run_time=4.0, rate_func=linear))
self.wait(1.0)

下载 Python 脚本

下载 Python 脚本

5. 总结

Manim本身提供的Rotate接口可以实现大部分常见的旋转操作,
本篇主要介绍的是多个物体联动的实现思路。

核心就是每个旋转的物体只要关心它所围绕的物体的位置,根据它所围绕物体的位置来更新自己的位置。
比如,上面的示例中,地球的旋转只要关注太阳的位置,月亮的旋转只要关注地球的位置。
如果有更多的物体联动旋转,也是一样的思路。