长话短说,我需要使一堆球在空间中朝随机方向移动。我正在使用带有VRED(3D渲染软件)的python脚本在屏幕上显示球。
我正在尝试使用球坐标,但不知何故,球在空间中的分布偏向Z轴。我真的无法弄清楚哪里出了错。
我正在这样进行:
我生成一个随机的偏航起始方向(-180,180)和一个随机的起始俯仰(0,180)
在每个新帧处,我都会稍微改变偏航角和俯仰角,并沿该新方向移动球。
这是我的Python代码(我希望它不太难读; vrAEBase
是与VRED相关的类,它允许loop()
每帧更新一次):
import random
import math
populationsize = 1000
balllist = []
#________________________________________BALL CLASS______________________________________________
class Ball(vrAEBase):
def __init__(self):
vrAEBase.__init__(self)
self.addLoop()
self.body = createSphere(2,100,1,1,1) # create sphere
self.isplaying = false
self.steplength = 20 #step length between each frame
self.yaw = random.uniform(-180, 180) #set starting yaw
self.pitch = random.uniform(0, 180) #set starting pitch
self.maxsteering = 1 # max angular change for yaw/pitch for each frame
self.x = 0 #startting X location
self.y = 0 #startting Y location
self.z = 0 #startting Z location
def loop(self): #loop is executed every frame
if self.isplaying:
self.yaw = self.yaw + random.uniform(-1*self.maxsteering, self.maxsteering) #set new yaw
self.pitch = self.pitch + random.uniform(-1*self.maxsteering, self.maxsteering) #set new pitch
localX = self.steplength * (math.sin(self.pitch)) * (math.cos(self.yaw)) #calculate X step length
localY = self.steplength * (math.sin(self.pitch)) * (math.sin(self.yaw)) #calculate Y step length
localZ = self.steplength * (math.cos(self.pitch)) #calculate Z step length
self.x += localX
self.y += localY
self.z += localZ
setTransformNodeTranslation(self.body, self.x,self.y,self.z,true)
def rewind(self):
self.isplaying = false
self.x = 0
self.y = 0
self.z = 0
setTransformNodeTranslation(self.body, self.x,self.y,self.z,true)
#__________________________________PLAY__________________________________
def play():
global balllist
for ball in balllist:
if ball.isplaying == false:
ball.isplaying = true
else:
ball.isplaying = false
#__________________________________REWIND_________________________________
def rewind():
global balllist
for ball in balllist:
ball.rewind()
#_______________________________SPAWN BALLS________________________________
for x in range(0, populationsize):
newball = Ball() #create ball
balllist.append(newball) #add ball to list
play()
print("end")
这是最终发行的图片:
答案 0 :(得分:0)
问题在于,为了在球体周围生成均匀的点分布,您不能进行phi = [0,pi]和theta = [-pi,pi],因为这将导致表面元素dA = dphi * dtheta代替正确的dA = sin(phi)* dphi * dtheta。
为了实现正确的体积元素变化
def __init__( self):
...
self.yaw = random.uniform(-180, 180) #set starting yaw
self.pitch = random.uniform(0, 180) #set starting pitch
...
到
def __init__( self):
...
u = random.uniform(0,1)
v = random.uniform(0,1)
self.yaw = 2 * math.pi * u #set starting yaw
self.pitch = math.acos( 2*v -1) #set starting pitch
...
有关更多文档,请参见http://mathworld.wolfram.com/SpherePointPicking.html。
还请注意您的时间步例程的行为,截至目前,这些点似乎倾向于朝该分布方向塌陷。我不知道这是否是您的预期行为