## Oct. 1, 2002, A. Middleton ## gravity.py ## This module defines two functions - one calculates the ## acceleration due to gravity on a body at position ra due ## to an object at position rb with mass mb. The second function ## updates the position, velocity, and track of a planet. The ## planet must be initialized properly (see fakesolarsystem.py) ## and have a list of "others": these are the planets which exert ## a force on it. from visual import * forcetype = -3 dt = 0.01 ## Acceleration in the direction separating objects a and b, with magnitude ## 1/distance**2. ('one over r squared' law.) def gravaccel(ra, rb, mb): return(-(ra-rb)*mag(ra-rb)**forcetype*mb) ## Update position based on velocity. Velocity changes by acceleration, ## with acceleration given by the sum from all 'others'. Also update track. def update(planet): planet.pos = planet.pos + dt * planet.vel totalaccel = vector(0,0,0) for other in planet.others: totalaccel = totalaccel + gravaccel(planet.pos, other.pos, other.mass) planet.vel = planet.vel + dt * totalaccel planet.track.append(pos=planet.pos)