def get_all_bodies(): # if there are no bodies, recreate the default bodies if len(simulator.get_bodies())==0: simulator.build_bodies() bodies = simulator.get_bodies() # gets all the plant data (name, xPos, yPos, size, and color) # format data to be JSON dumpable all_bodies = [] for body in bodies: all_bodies.append({"name":body.name, "px":body.px, "py":body.py, "size":body.size, "color":body.color}) # return planet data return json.dumps(all_bodies)
def update_all_bodies(width,height): simulator.update(width,height) # runs one loop of the simulation to get next position of planets bodies = simulator.get_bodies() # gets the plant data (name, xPos, yPos) # format data to be JSON dumpable all_bodies_update = [] for body in bodies: all_bodies_update.append({"name":body.name, "px":body.px, "py":body.py}) # return planet data return json.dumps(all_bodies_update)
def reset_bodies(): # gets all the bodies (necessary because we need their name to remove them from the D3 chart) bodies = simulator.get_bodies() # format data to be JSON dumpable all_bodies_Ids = [] for body in bodies: all_bodies_Ids.append({"name":body.name}) # clears all bodies from app simulator.all_bodies = []; # returns JSON data of all the past bodies (so they can be removed) return json.dumps(all_bodies_Ids)