我正在尝试创建3D动画,每个球体都沿随机方向(向上,向下,向左,向右,向后或向前)移动。它们都在3D世界中移动(每个球体都有随机的X,Y和Z值),并且投影在2D屏幕上。我期望每个球体在随机方向上独立于其他球体而运动,但这些球体似乎完全在一个方向上运动,如下所示。
鉴于我的代码,我不明白为什么它会这样工作:# random X, Y, Z: fill X, Y and Z coordinates with uniform random values that serve as coordinates
for sphere in spheres:
sphere.position.xy = np.random.uniform(-20, 20, size=2)
sphere.position.z = np.random.uniform(z_near, z_far)
# Create two angles for each sphere, theta and phi, to compute the change of direction.
sphere.theta_deg = np.random.rand(1) * 360; # Angle thera controls the horizontal orientation of the gaze.
sphere.phi_deg = np.random.rand(1) * 360; # Angle Phi controls the vertical orientation of the gaze.
# Different directions
speed = 3; #degrees/seconds
theta_rad = sphere.theta_deg * np.pi /180; #direction converted to radians
phi_rad = sphere.phi_deg* np.pi /180; #direction converted to radians
dx = speed*np.sin(-phi_rad-theta_rad)/frameRate;
dy = -speed*np.cos(phi_rad + theta_rad)/frameRate;
dz = -speed*np.cos(theta_rad)/frameRate;
while 1:
# Modulate the angular directions
dx = speed*np.sin(-phi_rad-theta_rad)/frameRate;
dy = -speed*np.cos(phi_rad + theta_rad)/frameRate;
dz = -speed*np.cos(theta_rad)/frameRate;
for sphere in spheres:
# Update Spheres Positions
sphere.position.x += dx
sphere.position.y += dy
sphere.position.z += dz
球体位置应该在每帧更新,并且应该是随机的。
非常感谢您的帮助!