class SolarSystem(Axon.ThreadedComponent.threadedcomponent): def __init__(self, *cat_args): super(SolarSystem, self).__init__() self.the_sun = LikeFile(make_cat(*cat_args), extrainboxes=("translation", "imaging")) self.the_sun.activate() self.planet = LikeFile(make_cat(*cat_args), extrainboxes=("translation", "rotator", "imaging")) self.planet.activate() self.sun_position = tuple([x / 2 for x in screensize]) self.planet_position = (screensize[0] / 4.0, screensize[1] / 2) self.planet_velocity = (0.0, 10) # ugh, I should be using numpy but it works, that's the important thing # This is merely a test of likefile. Really, kamaelia components should be written for a physics simulation like this. def acceleration(self, pos_planet, pos_sun): g = 200 # fudge factor # F = ma, but F is proportional to distance ** -2 # neatly removing the need to calculate a square root for the distance direction = (pos_planet[0] - pos_sun[0], pos_planet[1] - pos_sun[1]) magnitude = direction[0]**2 + direction[1]**2 return tuple([g * x / magnitude for x in direction]) def apply_acceleration_to_velocity(self, velocity, accn): return (velocity[0] + accn[0], velocity[1] + accn[1]) def apply_velocity_to_position(self, position, velocity): return (position[0] + velocity[0], position[1] + velocity[1]) def main(self): self.the_sun.put(self.sun_position, "translation") while True: time.sleep(0.01) self.planet.put(self.planet_position, "translation") accn = self.acceleration(self.sun_position, self.planet_position) self.planet_velocity = self.apply_acceleration_to_velocity( self.planet_velocity, accn) self.planet_position = self.apply_velocity_to_position( self.planet_position, self.planet_velocity)
class SolarSystem(Axon.ThreadedComponent.threadedcomponent): def __init__(self, *cat_args): super(SolarSystem, self).__init__() self.the_sun = LikeFile(make_cat(*cat_args), extrainboxes = ("translation", "imaging")) self.the_sun.activate() self.planet = LikeFile(make_cat(*cat_args), extrainboxes = ("translation", "rotator", "imaging")) self.planet.activate() self.sun_position = tuple([x/2 for x in screensize]) self.planet_position = (screensize[0]/4.0, screensize[1]/2) self.planet_velocity = (0.0, 10) # ugh, I should be using numpy but it works, that's the important thing # This is merely a test of likefile. Really, kamaelia components should be written for a physics simulation like this. def acceleration(self, pos_planet, pos_sun): g = 200 # fudge factor # F = ma, but F is proportional to distance ** -2 # neatly removing the need to calculate a square root for the distance direction = (pos_planet[0] - pos_sun[0], pos_planet[1] - pos_sun[1]) magnitude = direction[0] ** 2 + direction[1] ** 2 return tuple([g * x/magnitude for x in direction]) def apply_acceleration_to_velocity(self, velocity, accn): return (velocity[0] + accn[0], velocity[1] + accn[1]) def apply_velocity_to_position(self,position, velocity): return (position[0] + velocity[0], position[1] + velocity[1]) def main(self): self.the_sun.put(self.sun_position, "translation") while True: time.sleep(0.01) self.planet.put(self.planet_position, "translation") accn = self.acceleration(self.sun_position, self.planet_position) self.planet_velocity = self.apply_acceleration_to_velocity(self.planet_velocity, accn) self.planet_position = self.apply_velocity_to_position(self.planet_position, self.planet_velocity)
# ugh, I should be using numpy but it works, that's the important thing # This is merely a test of likefile. Really, kamaelia components should be written for a physics simulation like this. def acceleration(pos_planet, pos_sun): g = 200 # fudge factor # F = ma, but F is proportional to distance ** -2 # neatly removing the need to calculate a square root for the distance direction = (pos_planet[0] - pos_sun[0], pos_planet[1] - pos_sun[1]) magnitude = direction[0] ** 2 + direction[1] ** 2 return tuple([g * x/magnitude for x in direction]) def apply_acceleration_to_velocity(velocity, accn): return (velocity[0] + accn[0], velocity[1] + accn[1]) def apply_velocity_to_position(position, velocity): return (position[0] + velocity[0], position[1] + velocity[1]) the_sun.put(sun_position, "translation") while True: time.sleep(0.01) planet.put(planet_position, "translation") accn = acceleration(sun_position, planet_position) planet_velocity = apply_acceleration_to_velocity(planet_velocity, accn) planet_position = apply_velocity_to_position(planet_position, planet_velocity) time.sleep(5)