You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Manim无法同时播放两个动画:轮形对象平移与旋转不同步

解决Manim中轮子同步平移与旋转的问题

你的核心问题是用wheel.animate.rotate和wheel.animate.shift分别创建了两个独立的动画对象,要么分开播放导致先后执行,要么同时播放时因animate的状态覆盖问题,只有一个变换生效。下面是两种可行的解决方法:

方法一:链式animate调用

将旋转和平移操作链式写在同一个animate调用中,Manim会自动同步执行这两个变换:

from manim import *
import numpy as np

class TwoDShapes(Scene):
    def construct(self):
        circle = Circle(radius=3).shift(LEFT * 4)

        points = []
        squares = []
        
        num_squares = 20
        angle_increment = 2 * PI / num_squares  
       
        for i in range(num_squares):
            points.append(circle.point_at_angle(angle_increment * i))

            square = Square(side_length=0.25)
            square.move_to(points[i])
            squares.append(square)

        START = (-8, -3, 0)
        END = (8, -3, 0)
        line = Line(START, END)

        self.play(Create(line))
        self.play(Create(circle))

        for k in range(num_squares): 
            self.play(Create(squares[k], run_time=0.2))
            
        for k in range(num_squares): 
            self.play(Rotate(squares[k], angle_increment * k, run_time=0.2))

        wheel = Group(*squares, circle)

        # 关键修改:链式合并旋转和平移动画
        roll_animation = wheel.animate.rotate(4*2*PI).shift(RIGHT * 8)
        self.play(roll_animation, run_time=4)

        self.wait(1)
        text = Text("It works now! :)")
        text.move_to(LEFT * 3)
        self.play(Write(text))
        self.wait(1)

方法二:使用基础动画类+AnimationGroup

直接创建Rotate和Shift动画实例,放入AnimationGroup中同步播放,这种方式更灵活,适合需要单独控制每个动画参数的场景:

from manim import *
import numpy as np

class TwoDShapes(Scene):
    def construct(self):
        circle = Circle(radius=3).shift(LEFT * 4)

        points = []
        squares = []
        
        num_squares = 20
        angle_increment = 2 * PI / num_squares  
       
        for i in range(num_squares):
            points.append(circle.point_at_angle(angle_increment * i))

            square = Square(side_length=0.25)
            square.move_to(points[i])
            squares.append(square)

        START = (-8, -3, 0)
        END = (8, -3, 0)
        line = Line(START, END)

        self.play(Create(line))
        self.play(Create(circle))

        for k in range(num_squares): 
            self.play(Create(squares[k], run_time=0.2))
            
        for k in range(num_squares): 
            self.play(Rotate(squares[k], angle_increment * k, run_time=0.2))

        wheel = Group(*squares, circle)

        # 关键修改:创建基础动画实例并放入AnimationGroup
        rotation_animation = Rotate(wheel, 4*2*PI)
        shift_animation = Shift(wheel, RIGHT * 8)
        self.play(AnimationGroup(rotation_animation, shift_animation, lag_ratio=0), run_time=4)

        self.wait(1)
        text = Text("It works now! :)")
        text.move_to(LEFT * 3)
        self.play(Write(text))
        self.wait(1)

额外优化:物理真实的滚动匹配

为了让轮子滚动更符合现实,平移距离应该等于轮子周长乘以旋转圈数。轮子半径3,周长是2*PI*3=6*PI,4圈对应的平移距离是4*6*PI≈75.4,你可以把RIGHT * 8改成RIGHT * (4*2*PI*3),这样动画会更真实。

内容的提问来源于stack exchange,提问作者Pablo Dibildox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 09:21:11