[英]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()
How can I make that the vector and the particle remain fixed in all frames?如何使矢量和粒子在所有帧中保持固定?
Here is some things that you need to know:以下是您需要了解的一些事项:
arrow
runs before the updater attached to dot
(which will make the arrow always lag behind).arrow
的更新程序在附加dot
的更新程序之前运行(这将使箭头始终落后)。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:以下是可能的解决方案:
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)
.dot.set_z_index(1)
。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.