找回密码
 立即注册
首页 业界区 业界 Manim实现旋转扭曲特效

Manim实现旋转扭曲特效

账暴 3 天前
在数学动画制作中,特殊效果可以极大地增强视觉表现力和吸引力。
本文将介绍如何使用Manim框架实现一个旋转扭曲特效,通过自定义动画类来创建独特的视觉效果。
实现原理

旋转扭曲特效的核心是通过修改对象上每个点的坐标来实现扭曲效果。
在Manim中,我们可以通过继承Animation类并重写interpolate_mobject方法来创建自定义动画。
自定义动画类的结构
  1. class TwistAnimation(Animation):
  2.     def __init__(
  3.         self,
  4.         mobject,
  5.         center=ORIGIN,
  6.         twist_angle=TAU,
  7.         strength=1.0,
  8.         direction="clockwise",
  9.         **kwargs
  10.     ):
  11.         super().__init__(mobject, **kwargs)
  12.         
  13.         # 存储初始状态以便在动画过程中重置
  14.         self.center = center
  15.         self.twist_angle = twist_angle
  16.         self.strength = strength
  17.         self.direction = direction
  18.         
  19.         # 根据方向调整扭曲角度
  20.         if direction == "counterclockwise":
  21.             self.twist_angle = -self.twist_angle
复制代码
这个类定义了几个关键参数:

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

扭曲效果的核心在于interpolate_mobject方法,它在动画的每一帧被调用,根据当前的进度alpha更新对象的形状:
  1. def interpolate_mobject(self, alpha):
  2.     # 重置对象到初始状态
  3.     self.mobject.become(self.starting_mobject)
  4.    
  5.     # 计算当前的扭曲角度
  6.     current_twist_angle = alpha * self.twist_angle
  7.    
  8.     # 获取对象的所有点
  9.     points = self.mobject.points
  10.    
  11.     # 对每个点应用扭曲变换
  12.     for i in range(len(points)):
  13.         # 计算点相对于中心的位置
  14.         point = points[i]
  15.         rel_point = point - self.center
  16.         
  17.         # 计算点到中心的距离
  18.         distance = np.linalg.norm(rel_point)
  19.         
  20.         # 如果点在中心,则不进行变换
  21.         if distance == 0:
  22.             continue
  23.         
  24.         # 计算点的极角
  25.         angle = np.arctan2(rel_point[1], rel_point[0])
  26.         
  27.         # 计算扭曲后的角度:距离中心越远,扭曲角度越大
  28.         twisted_angle = angle + current_twist_angle * (distance * self.strength)
  29.         
  30.         # 计算扭曲后的坐标
  31.         twisted_x = self.center[0] + distance * np.cos(twisted_angle)
  32.         twisted_y = self.center[1] + distance * np.sin(twisted_angle)
  33.         
  34.         # 更新点的位置
  35.         points[i] = np.array([twisted_x, twisted_y, point[2]])
  36.    
  37.     # 将更新后的点应用到对象上
  38.     self.mobject.set_points(points)
复制代码
这个算法的核心思想是:

  • 将对象上的每个点转换为相对于扭曲中心的极坐标
  • 根据点到中心的距离计算扭曲角度(距离越远,扭曲越大)
  • 将扭曲后的极坐标转换回笛卡尔坐标
  • 更新对象上所有点的位置
这种实现方式使得扭曲效果非常自然,尤其是对于几何形状对象。
使用示例

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

Example01类展示了基本的扭曲效果,包括顺时针和逆时针扭曲:
  1. class Example01(Scene):
  2.     """基本的扭曲效果"""
  3.    
  4.     def construct(self):
  5.         # 创建一个矩形作为扭曲对象
  6.         rect = Rectangle(width=4, height=2, color=BLUE, fill_opacity=0.5)
  7.         
  8.         # 添加扭曲动画
  9.         self.play(Create(rect))
  10.         self.wait()
  11.         self.play(TwistAnimation(rect, run_time=2))
  12.         self.play(TwistAnimation(rect, twist_angle=-TAU, run_time=2))  # 反向扭曲
  13.         self.wait()
复制代码
这个示例创建了一个蓝色矩形,然后先应用顺时针扭曲,再应用逆时针扭曲。
1.gif

不同扭曲中心的效果

Example02类展示了使用不同扭曲中心的效果:
  1. class Example02(Scene):
  2.     """不同扭曲中心的效果"""
  3.    
  4.     def construct(self):
  5.         # 创建多个对象,并设置不同的扭曲中心
  6.         circle1 = Circle(radius=0.8, color=RED, fill_opacity=0.5)
  7.         circle1.shift(LEFT * 2)
  8.         
  9.         circle2 = Circle(radius=0.8, color=GREEN, fill_opacity=0.5)
  10.         
  11.         circle3 = Circle(radius=0.8, color=BLUE, fill_opacity=0.5)
  12.         circle3.shift(RIGHT * 2)
  13.         
  14.         # 添加中心标记
  15.         center_marker1 = Dot(color=WHITE).shift(LEFT * 3)
  16.         center_marker3 = Dot(color=WHITE).shift(RIGHT * 3)
  17.         
  18.         # 添加对象到场景
  19.         self.play(Create(circle1), Create(circle2), Create(circle3))
  20.         self.play(Create(center_marker1), Create(center_marker3))
  21.         self.wait()
  22.         
  23.         # 应用扭曲动画,使用不同的中心
  24.         self.play(
  25.             TwistAnimation(circle1, center=center_marker1.get_center(), run_time=2),
  26.             TwistAnimation(circle2, center=ORIGIN, run_time=2),
  27.             TwistAnimation(circle3, center=center_marker3.get_center(), run_time=2),
  28.         )
  29.         self.wait()
复制代码
这个示例创建了三个不同颜色的圆,并分别使用不同的中心点进行扭曲,直观地展示了扭曲中心对效果的影响。
2.gif

不同扭曲强度的效果

Example03类展示了使用不同扭曲强度的效果:
  1. class Example03(Scene):
  2.     """演示不同扭曲强度的效果"""
  3.    
  4.     def construct(self):
  5.         # 创建多个对象,并设置不同的扭曲强度
  6.         square1 = Square(side_length=1.5, color=YELLOW, fill_opacity=0.5)
  7.         square1.shift(LEFT * 2)
  8.         
  9.         square2 = Square(side_length=1.5, color=MAROON, fill_opacity=0.5)
  10.         
  11.         square3 = Square(side_length=1.5, color=TEAL, fill_opacity=0.5)
  12.         square3.shift(RIGHT * 2)
  13.         
  14.         # 添加对象到场景
  15.         self.play(Create(square1), Create(square2), Create(square3))
  16.         self.wait()
  17.         
  18.         # 应用扭曲动画,使用不同的强度
  19.         self.play(
  20.             TwistAnimation(square1, strength=0.5, run_time=2),
  21.             TwistAnimation(square2, strength=1.0, run_time=2),
  22.             TwistAnimation(square3, strength=2.0, run_time=2),
  23.         )
  24.         self.wait()
复制代码
这个示例创建了三个不同颜色的正方形,并分别应用不同强度的扭曲,展示了扭曲强度对效果的影响。
3.gif

总结

特效特点

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

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

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

  • 数学教学:用于展示几何变换、极坐标转换等数学概念
  • 视觉效果增强:为动画添加独特的视觉效果,增强观众的注意力
  • 转场动画:作为场景之间的转场效果
  • 强调重点:通过扭曲效果突出显示重要的对象或概念
  • 创意动画:用于创建具有艺术感的动画效果
通过这个简单而强大的特效,我们可以为Manim动画增添更多的视觉表现力和创意可能性。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册