简体   繁体   English

以随机运动分离的 Manim 对象

[英]Manim objects separated in random motion

I am trying to animate a particle and a vector that is attached to its center as a random motion is applied to the particle.我正在尝试为粒子设置动画,并在将随机运动应用于粒子时附加到其中心的矢量。 The particle behaves as intended, but the vector always has a offset from the particle.粒子按预期运行,但向量始终与粒子有偏移。 I tried to set the random seed of each scene, but it didn't work.我尝试设置每个场景的随机种子,但没有奏效。

from manim import *
import numpy as np 

class Particles(ThreeDScene):
    def construct(self):

        self.camera.background_color = WHITE
        coordinate = np.array([ (0,0,0) ])
        angle = 90 
        radius = 1.25

        #Polar coordinates
        def polar2cart(theta_degrees, rho):
            theta = theta_degrees*(np.pi/180)
            x = rho * np.cos(theta) 
            y = rho * np.sin(theta) 
            return(x, y, 0)

        start_arrow = np.array(coordinate) - np.array( [polar2cart(angle,radius )] )
        end_arrow =  np.array(coordinate) + np.array( [polar2cart(angle,radius )] )
        arrow = Arrow( start= start_arrow, end = end_arrow, color = '#ff0000')  
        dot = Dot(point=coordinate, radius=0.2, color = '#000000')
        
        #Display
        self.add(arrow, dot)

        ##############################################################################################################################
        #Random motion
        valor = ValueTracker(0)
        dot.add_updater(lambda m: m.move_to( m.get_center() + (np.random.normal(0,0.05),np.random.normal(0,0.05),np.random.normal(0,0.05)) ) )
        arrow.add_updater(lambda m: m.move_to( dot.get_center() + (np.random.normal(0,0.05),np.random.normal(0,0.05),np.random.normal(0,0.05)) ) )

        self.play(valor.animate.set_value(10),rate_func=smooth, run_time=5)
        self.wait()

Particle and vector粒子和矢量

How can I make that the vector and the particle remain fixed in all frames?如何使矢量和粒子在所有帧中保持固定?

Here is some things that you need to know:以下是您需要了解的一些事项:

  • The order in which updaters attached to mobjects are executed is the order in which their mobjects have been added to the scene.附加到 mobject 的更新程序的执行顺序是它们的 mobject 被添加到场景中的顺序。 In your code, the updater attached to arrow runs before the updater attached to dot (which will make the arrow always lag behind).在您的代码中,附加到arrow的更新程序在附加dot的更新程序之前运行(这将使箭头始终落后)。
  • Even if you fix the seed, subsequent calls to np.random.norm will not yield the same numbers (fortunately, otherwise your dot would move in a straight line out of the scene).即使您修复了种子,对np.random.norm的后续调用也不会产生相同的数字(幸运的是,否则您的点会直线移动到场景之外)。 If you want to fix the arrow to the center of the dot, then there is no need to add the random offset to that as well.如果您想将箭头固定在点的中心,那么也不需要添加随机偏移量。

Here are possible solutions:以下是可能的解决方案:

  • Add the objects in the other way around, self.add(dot, arrow) , and remove the random offset from the updater attached to the arrow, arrow.add_updater(lambda m: m.move_to(dot.get_center())) .以相反的方式添加对象self.add(dot, arrow) ,并从附加到箭头的更新器中删除随机偏移量arrow.add_updater(lambda m: m.move_to(dot.get_center())) If the order of mobjects on the screen is important to you, you can also run dot.set_z_index(1) .如果屏幕上 mobjects 的顺序对您很重要,您也可以运行dot.set_z_index(1)
  • Alternatively, you could also simply create a group of your two objects and attach an updater to that;或者,您也可以简单地创建一个包含两个对象的组并将更新程序附加到该组; particle_group = VGroup(arrow, dot) followed by particle_group.add_updater(lambda m: m.shift(...)) . particle_group = VGroup(arrow, dot)后跟particle_group.add_updater(lambda m: m.shift(...))

I'd personally go for the group, but it really depends on what else you intend to do in this scene.我个人会为该组选择 go,但这真的取决于你打算在这个场景中做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM