我正在maya中尝试一个脚本,看看我是否可以获得重力以及与球形物体一起使用的很多东西,目前它工作正常(尝试制作月球和地球来缩放并校准重力效果),但我会喜欢尝试更进一步并添加碰撞以使对象相互反弹,但是在昨天环顾四周之后,我发现 2d 对象已经够难了,更不用说 3d 了。
我设法写了一些可以检测碰撞的东西,它获取两个对象之间的距离并将其与两个对象的半径进行比较,这似乎有效(尽管需要重写),但我不知道是什么接下来做。老实说,我什至不确定它是否
只是为了了解它是如何工作的,这里是主要部分的当前阶段 - objSel[j] 是当前选定的对象, allObjects 是除了当前选定的对象之外的所有内容
def moveObjects(originalTime,objSel,objMultiplySize):
moveAmounts = []
crashed = False
for j in range(len(objSel)):
#get initial values
originalVelocity = getVelocity(objSel[j],originalTime,objMultiplySize)
objVolume = getVolume(objSel[j],objMultiplySize)
allObjects = selectAllOtherObjects(objSel[j], objSel)
#calculate gravity effect on object
xDist = 0
yDist = 0
zDist = 0
for i in range (0, len(allObjects) ):
attraction = calcuateForce(objSel[j],allObjects[i],objMultiplySize)
distanceFromObj = getDistance(allObjects[i],objSel[j],objMultiplySize)[1]
xDist += distanceFromObj[0] * attraction / (objVolume*2.15*math.pi)
yDist += distanceFromObj[1] * attraction / (objVolume*2.15*math.pi)
zDist += distanceFromObj[2] * attraction / (objVolume*2.15*math.pi)
gravityEffect = [xDist,yDist,zDist]
newX = (originalVelocity[0]+gravityEffect[0])/objMultiplySize
newY = (originalVelocity[1]+gravityEffect[1])/objMultiplySize
newZ = (originalVelocity[2]+gravityEffect[2])/objMultiplySize
newVelocity = [newX,newY,newZ]
moveAmounts.append( newVelocity )
#-----------this whole bit needs rewriting--------
py.currentTime( originalTime + 1, edit = True, update = True)
for j in range(len(moveAmounts)):
#collision detection
allObjects = selectAllOtherObjects(objSel[j], objSel)
originalRadius = getRadius(objSel[j],objMultiplySize)
for i in range (0, len(allObjects) ):
objRadius = getRadius(allObjects[i],objMultiplySize)
objDistance = getDistance(allObjects[i],objSel[j],objMultiplySize)
if objDistance[0] < objRadius + originalRadius:
force1 = moveAmounts[j][0]*objMultiplySize * objRadius
print "Crashed"
crashed = True
if crashed != True:
#move object
py.move( objSel[j], float(moveAmounts[j][0]), float(moveAmounts[j][1]), float(moveAmounts[j][2]), relative = True )
py.setKeyframe( objSel[j], attribute='translateX')
py.setKeyframe( objSel[j], attribute='translateY')
py.setKeyframe( objSel[j], attribute='translateZ')
else:
#dunno what to do here
for i in range (0, len(allObjects) ):
mass1 = getVolume(allObjects[i],objMultiplySize)
velocity1 = getVelocity(allObjects[i],originalTime,objMultiplySize)
mass2 = getVolume(objSel[j],objMultiplySize)
velocity2 = getVelocity(objSel[j],originalTime,objMultiplySize)
m1v1m2v2X = mass1*velocity1[0] + mass2*velocity2[0]
m1v1m2v2Y = mass1*velocity1[1] + mass2*velocity2[1]
m1v1m2v2Z = mass1*velocity1[2] + mass2*velocity2[2]
totalMass = mass1+mass2