i'm trying script in maya see if can gravity , lot working spherical objects, , works fine (tried making moon , earth scale , calibrated gravity effect that), i'd try step further , add collisions make objects bounce off each other, after looking around ages yesterday, found it's hard enough 2d objects, never mind 3d.
i've managed write bit can detect collision, gets distance between both objects , compares against radius of both objects, seems work (needs rewriting though), couldn't figure out next. honest, i'm not sure if it's possible without advanced university knowledge why gave :p
idea of how works, here's current stage of main part - objsel[j] current selected object, allobjects current selected object
def moveobjects(originaltime,objsel,objmultiplysize): moveamounts = [] crashed = false 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 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) j in range(len(moveamounts)): #collision detection allobjects = selectallotherobjects(objsel[j], objsel) originalradius = getradius(objsel[j],objmultiplysize) 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 here 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
referencing http://en.wikipedia.org/wiki/elastic_collision :
for 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) v1new = velocity1; //just initialization v2new = velocity2; //just initialization v1new[0] = (velocity1[0] *( mass1-mass2) + 2*mass2*velocity2[0])/(mass1 + mass2); v2new[0] = (velocity2[0] *( mass2-mass1) + 2*mass1*velocity1[0])/(mass1 + mass2); v1new[1] = (velocity1[1] *( mass1-mass2) + 2*mass2*velocity2[1])/(mass1 + mass2); v2new[1] = (velocity2[1] *( mass2-mass1) + 2*mass1*velocity1[1])/(mass1 + mass2); v1new[2] = (velocity1[2] *( mass1-mass2) + 2*mass2*velocity2[2])/(mass1 + mass2); v2new[2] = (velocity2[2] *( mass2-mass1) + 2*mass1*velocity1[2])/(mass1 + mass2);
assuming elastic collision, can collision calculations each dimension separately.
Comments
Post a Comment