From 61562a0bd4c65b914c4fd758072e543b2e948716 Mon Sep 17 00:00:00 2001 From: Sakimori Date: Sun, 2 May 2021 21:38:42 -0400 Subject: [PATCH] added orbit line --- OrbitSim.py | 31 +++++++++++++++++++++++++++---- renderer.py | 9 +++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/OrbitSim.py b/OrbitSim.py index f72af42..eade6ff 100644 --- a/OrbitSim.py +++ b/OrbitSim.py @@ -52,15 +52,37 @@ class Planet: if self.rotationPercentage >= 100.0: self.rotationPercentage -= 100.0 +class DisplayPoint: + """A single point of any color""" + def __init__(self, location, color): + self.location = location + self.color = color + +class DecayPoint(DisplayPoint): + """A display point that slowly fades to black""" + decayTick = 10 + currentDecayTick = 0 + + def update(self): + self.currentDecayTick += 1 + if self.currentDecayTick > self.decayTick: + self.currentDecayTick = 0 + self.color = (max((self.color[0]-5, 0)), max((self.color[1]-5, 0)), max((self.color[2]-5, 0))) + Planet.Earth = Planet("Earth", config()["earthMass"], config()["earthRadius"], 86400) -def physicsUpdate(objects, deltaTime): +def physicsUpdate(objects, orbitlines, deltaTime): """updates the positions of all orbiting objects in [objects] with timestep deltaTime""" for obj in objects: if type(obj).__name__ == "OrbitingBody": + orbitlines.append(DecayPoint(deepcopy(obj.location), (255,255,255))) + if len(orbitlines) > 500: + orbitlines.pop(0) accel = Point.scalarMult(Point.subtract(obj.location, obj.parentPlanet.location).normalize(),-(config()["G"] * obj.parentPlanet.mass)/(Point.subtract(obj.location, obj.parentPlanet.location).magnitude() ** 2)) obj.velocity = Point.add(obj.velocity, Point.scalarMult(accel, deltaTime)) obj.location = Point.add(obj.location, Point.scalarMult(obj.velocity, deltaTime)) + for line in orbitlines: + line.update() if __name__=="__main__": pygame.init() @@ -75,8 +97,9 @@ if __name__=="__main__": running = True display = False thisEarth = deepcopy(Planet.Earth) - sat = OrbitingBody(Point(config()["earthRadius"] * 1.1, 0, 0), Point(0,1000,-6500), "BoSLOO", 3, thisEarth) - renderObjects = [thisEarth, sat] + sat = OrbitingBody(Point(config()["earthRadius"] * 1.1, 0, 0), Point(0,6000,-6500), "BoSLOO", 3, thisEarth) + orbitlines = [] + renderObjects = [thisEarth, sat, orbitlines] while running: for event in pygame.event.get(): @@ -95,7 +118,7 @@ if __name__=="__main__": pygame.display.flip() if display: deltaTime = frameTime * config()["timeScale"] - physicsUpdate(renderObjects, deltaTime) + physicsUpdate(renderObjects, orbitlines, deltaTime) camera.renderFrame() pygame.display.flip() time.sleep(frameTime) diff --git a/renderer.py b/renderer.py index 87efc06..1b3b1fc 100644 --- a/renderer.py +++ b/renderer.py @@ -96,6 +96,15 @@ class Camera: if intersectPoint is not None: intersectPoint = Point.add(intersectPoint, Point(int(winWidth/2), int(winHeight/2), 0)) pygame.draw.circle(screenSurface, (255,255,150), (int(intersectPoint.vector[0]), int(intersectPoint.vector[1])), 15) + elif isinstance(obj, list): + for orbitline in obj: + if orbitline.color != (0,0,0): + lineToCamera = Line(orbitline.location, self.location) + intersectPoint = lineToCamera.intersectWithPlane(screenPlane) + if intersectPoint is not None: + intersectPoint = Point.add(intersectPoint, Point(int(winWidth/2), int(winHeight/2), 0)) + pygame.draw.circle(screenSurface, orbitline.color, (int(intersectPoint.vector[0]), int(intersectPoint.vector[1])), 1) + screenSurface = pygame.transform.flip(screenSurface, False, True) self.surface.blit(screenSurface, (0,0))