在数学动画制作中,特殊效果可以极大地增强视觉表现力和吸引力。

本文将介绍如何使用Manim框架实现一个旋转扭曲特效,通过自定义动画类来创建独特的视觉效果。

实现原理

旋转扭曲特效的核心是通过修改对象上每个点的坐标来实现扭曲效果。

在Manim中,我们可以通过继承Animation类并重写interpolate_mobject方法来创建自定义动画。

自定义动画类的结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from manim import *
class TwistAnimation(Animation):
def __init__(
self,
mobject,
center=ORIGIN,
twist_angle=TAU,
strength=1.0,
direction="clockwise",
**kwargs
):
super().__init__(mobject, **kwargs)

# 存储初始状态以便在动画过程中重置
self.center = center
self.twist_angle = twist_angle
self.strength = strength
self.direction = direction

# 根据方向调整扭曲角度
if direction == "counterclockwise":
self.twist_angle = -self.twist_angle

下载 Python 脚本

这个类定义了几个关键参数:

  • mobject:要进行扭曲的Manim对象
  • center:扭曲中心点,默认为原点
  • twist_angle:总扭曲角度,默认为TAU(360度)
  • strength:扭曲强度,默认为1.0
  • direction:扭曲方向,可以是”clockwise”(顺时针)或”counterclockwise”(逆时针)

扭曲算法的核心实现

扭曲效果的核心在于interpolate_mobject方法,它在动画的每一帧被调用,根据当前的进度alpha更新对象的形状:

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
32
33
34
35
36
37
38
39
from manim import *
def interpolate_mobject(self, alpha):
# 重置对象到初始状态
self.mobject.become(self.starting_mobject)

# 计算当前的扭曲角度
current_twist_angle = alpha * self.twist_angle

# 获取对象的所有点
points = self.mobject.points

# 对每个点应用扭曲变换
for i in range(len(points)):
# 计算点相对于中心的位置
point = points[i]
rel_point = point - self.center

# 计算点到中心的距离
distance = np.linalg.norm(rel_point)

# 如果点在中心,则不进行变换
if distance == 0:
continue

# 计算点的极角
angle = np.arctan2(rel_point[1], rel_point[0])

# 计算扭曲后的角度:距离中心越远,扭曲角度越大
twisted_angle = angle + current_twist_angle * (distance * self.strength)

# 计算扭曲后的坐标
twisted_x = self.center[0] + distance * np.cos(twisted_angle)
twisted_y = self.center[1] + distance * np.sin(twisted_angle)

# 更新点的位置
points[i] = np.array([twisted_x, twisted_y, point[2]])

# 将更新后的点应用到对象上
self.mobject.set_points(points)

下载 Python 脚本

这个算法的核心思想是:

  1. 将对象上的每个点转换为相对于扭曲中心的极坐标
  2. 根据点到中心的距离计算扭曲角度(距离越远,扭曲越大)
  3. 将扭曲后的极坐标转换回笛卡尔坐标
  4. 更新对象上所有点的位置

这种实现方式使得扭曲效果非常自然,尤其是对于几何形状对象。

使用示例

代码中提供了三个示例场景,展示了如何使用这个扭曲特效。

基本扭曲效果

Example01类展示了基本的扭曲效果,包括顺时针和逆时针扭曲:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from manim import *
import numpy as np
import sympy as sp
def head(cn, en):
t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
return VGroup(t, s)
def foot(cn):
return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
def bg_image(h=60, w=100, mode=0):
a = np.zeros((h, w, 4), dtype=np.uint8)
a[:, :, 3] = 255
if mode == 0:
a[:, :, 0] = np.linspace(10, 90, w, dtype=np.uint8)[None, :]
a[:, :, 2] = np.linspace(60, 160, w, dtype=np.uint8)[None, :]
elif mode == 1:
a[:, :, 1] = np.linspace(20, 110, h, dtype=np.uint8)[:, None]
a[:, :, 2] = np.linspace(40, 130, h, dtype=np.uint8)[:, None]
else:
a[:, :, 0] = 120
a[:, :, 1] = 70
a[:, :, 2] = np.linspace(30, 140, w, dtype=np.uint8)[None, :]
return ImageMobject(a)
class TwistAnimation(Animation):
def __init__(self, mobject, angle=PI, strength=1.0, **kwargs):
self.base = mobject.get_all_points().copy()
super().__init__(mobject, **kwargs)
self.angle = angle
self.strength = strength
def interpolate_mobject(self, alpha):
new = self.base.copy()
for k, p in enumerate(self.base):
r = np.linalg.norm(p[:2])
th = alpha * self.angle * self.strength * max(0.0, 1 - r / 4.5)
c, s = np.cos(th), np.sin(th)
new[k, 0] = p[0] * c - p[1] * s
new[k, 1] = p[0] * s + p[1] * c
self.mobject.set_points(new)
class TwistSquare(Scene):
def construct(self):
self.add(head("旋转扭曲:按半径改变扭转角", "TwistAnimation / set_points"))
sq = Square(side_length=4.2, color=BLUE).set_fill(BLUE, opacity=0.3).set_stroke(width=5)
self.add(sq)
self.play(TwistAnimation(sq, angle=PI, strength=1.0, run_time=4.4))
self.play(FadeIn(foot("离中心越远扭转角越小,形成非刚性形变")), run_time=0.4)
self.wait(1.2)

下载 Python 脚本

这个示例创建了一个蓝色矩形,然后先应用顺时针扭曲,再应用逆时针扭曲。

不同扭曲中心的效果

Example02类展示了使用不同扭曲中心的效果:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from manim import *
import numpy as np
import sympy as sp
def head(cn, en):
t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
return VGroup(t, s)
def foot(cn):
return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
def bg_image(h=60, w=100, mode=0):
a = np.zeros((h, w, 4), dtype=np.uint8)
a[:, :, 3] = 255
if mode == 0:
a[:, :, 0] = np.linspace(10, 90, w, dtype=np.uint8)[None, :]
a[:, :, 2] = np.linspace(60, 160, w, dtype=np.uint8)[None, :]
elif mode == 1:
a[:, :, 1] = np.linspace(20, 110, h, dtype=np.uint8)[:, None]
a[:, :, 2] = np.linspace(40, 130, h, dtype=np.uint8)[:, None]
else:
a[:, :, 0] = 120
a[:, :, 1] = 70
a[:, :, 2] = np.linspace(30, 140, w, dtype=np.uint8)[None, :]
return ImageMobject(a)
class TwistAnimation(Animation):
def __init__(self, mobject, angle=PI, strength=1.0, **kwargs):
self.base = mobject.get_all_points().copy()
super().__init__(mobject, **kwargs)
self.angle = angle
self.strength = strength
def interpolate_mobject(self, alpha):
new = self.base.copy()
for k, p in enumerate(self.base):
r = np.linalg.norm(p[:2])
th = alpha * self.angle * self.strength * max(0.0, 1 - r / 4.5)
c, s = np.cos(th), np.sin(th)
new[k, 0] = p[0] * c - p[1] * s
new[k, 1] = p[0] * s + p[1] * c
self.mobject.set_points(new)
class TwistCircle(Scene):
def construct(self):
self.add(head("旋转扭曲:圆形与网格", "strength = 1.6"))
circ = Circle(radius=2.0, color=GREEN).set_stroke(width=6)
grid = VGroup(*[Line(LEFT * 2.4 + UP * y, RIGHT * 2.4 + UP * y, color=GREY_B)
for y in (-1.2, -0.4, 0.4, 1.2)]).set_stroke(width=1.5, opacity=0.6)
self.add(grid, circ)
self.play(TwistAnimation(grid, angle=PI * 1.5, strength=1.6, run_time=4.4))
self.play(FadeIn(foot("直接改写 mobject 的点数组,就能得到任意扭曲")), run_time=0.4)
self.wait(1.2)

下载 Python 脚本

这个示例创建了三个不同颜色的圆,并分别使用不同的中心点进行扭曲,直观地展示了扭曲中心对效果的影响。

不同扭曲强度的效果

Example03类展示了使用不同扭曲强度的效果:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from manim import *
import numpy as np
import sympy as sp
def head(cn, en):
t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
return VGroup(t, s)
def foot(cn):
return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
def bg_image(h=60, w=100, mode=0):
a = np.zeros((h, w, 4), dtype=np.uint8)
a[:, :, 3] = 255
if mode == 0:
a[:, :, 0] = np.linspace(10, 90, w, dtype=np.uint8)[None, :]
a[:, :, 2] = np.linspace(60, 160, w, dtype=np.uint8)[None, :]
elif mode == 1:
a[:, :, 1] = np.linspace(20, 110, h, dtype=np.uint8)[:, None]
a[:, :, 2] = np.linspace(40, 130, h, dtype=np.uint8)[:, None]
else:
a[:, :, 0] = 120
a[:, :, 1] = 70
a[:, :, 2] = np.linspace(30, 140, w, dtype=np.uint8)[None, :]
return ImageMobject(a)
class TwistAnimation(Animation):
def __init__(self, mobject, angle=PI, strength=1.0, **kwargs):
self.base = mobject.get_all_points().copy()
super().__init__(mobject, **kwargs)
self.angle = angle
self.strength = strength
def interpolate_mobject(self, alpha):
new = self.base.copy()
for k, p in enumerate(self.base):
r = np.linalg.norm(p[:2])
th = alpha * self.angle * self.strength * max(0.0, 1 - r / 4.5)
c, s = np.cos(th), np.sin(th)
new[k, 0] = p[0] * c - p[1] * s
new[k, 1] = p[0] * s + p[1] * c
self.mobject.set_points(new)
class TwistRestore(Scene):
def construct(self):
self.add(head("旋转扭曲:来回形变", "rate_func = there_and_back"))
poly = RegularPolygon(n=6, color=PURPLE).scale(2.0).set_fill(PURPLE, opacity=0.3)
poly.set_stroke(width=5)
self.add(poly)
self.play(TwistAnimation(poly, angle=PI, strength=1.3, run_time=4.6,
rate_func=there_and_back))
self.play(FadeIn(foot("用速率函数让对象扭过去再回到原形")), run_time=0.4)
self.wait(1.2)

下载 Python 脚本

这个示例创建了三个不同颜色的正方形,并分别应用不同强度的扭曲,展示了扭曲强度对效果的影响。

下载 Python 脚本

总结

特效特点

这个旋转扭曲特效具有以下特点:

  1. 高度可定制性:通过调整扭曲中心、扭曲角度、扭曲强度和扭曲方向,可以创建各种不同的扭曲效果
  2. 自然流畅:基于极坐标变换的算法使得扭曲效果非常自然流畅
  3. 适用范围广:可以应用于各种Manim对象,包括几何形状和文本
  4. 易于集成:作为一个自定义Animation类,可以很容易地集成到现有Manim项目中

使用场景

这个扭曲特效可以用于以下场景:

  1. 数学教学:用于展示几何变换、极坐标转换等数学概念
  2. 视觉效果增强:为动画添加独特的视觉效果,增强观众的注意力
  3. 转场动画:作为场景之间的转场效果
  4. 强调重点:通过扭曲效果突出显示重要的对象或概念
  5. 创意动画:用于创建具有艺术感的动画效果

通过这个简单而强大的特效,我们可以为Manim动画增添更多的视觉表现力和创意可能性。