From 8236efcffc8c43ad0c29ccf1d34f9dbd576389a5 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 26 Oct 2013 14:25:37 -0400 Subject: [PATCH] Added orbital display. --- punyverse/entity.py | 38 +++++++++++++++++++++++++++++++++++++- punyverse/game.py | 6 ++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/punyverse/entity.py b/punyverse/entity.py index 1274d51..b13d925 100644 --- a/punyverse/entity.py +++ b/punyverse/entity.py @@ -1,7 +1,8 @@ from punyverse import framedata -from math import radians, sin, cos from punyverse.orbit import KeplerOrbit +from pyglet.gl import * + class Entity(object): def __init__(self, id, location, rotation=(0, 0, 0), direction=(0, 0, 0), background=False): @@ -60,11 +61,46 @@ class Satellite(Planet): longitude = kwargs.pop('longitude', 0) argument = kwargs.pop('argument', 0) + # Orbit calculation + self.orbit_id = None + self.orbit_cache = None + self.theta = 0 # OpenGL's z-axis is reversed self.orbit = KeplerOrbit(distance, eccentricity, inclination, longitude, argument) super(Satellite, self).__init__(*args, **kwargs) + def get_orbit(self): + # Cache key is the three orbital plane parameters and eccentricity + cache = (self.orbit.eccentricity, self.orbit.longitude, self.orbit.inclination, self.orbit.argument) + if self.orbit_cache == cache: + return self.orbit_id + + print 'Orbit cache miss' + if self.orbit_id is not None: + glDeleteLists(self.orbit_id, 1) + + id = glGenLists(1) + glNewList(id, GL_COMPILE) + glDisable(GL_LIGHTING) + glDisable(GL_TEXTURE_2D) + glPushAttrib(GL_LINE_BIT | GL_CURRENT_BIT) + glColor3f(0, 1, 0) + glLineWidth(3) + glBegin(GL_LINE_LOOP) + for theta in xrange(360): + x, z, y = self.orbit.orbit(theta) + glVertex3f(x, y, z) + glEnd() + glPopAttrib() + glEnable(GL_TEXTURE_2D) + glEnable(GL_LIGHTING) + glEndList() + + self.orbit_id = id + self.orbit_cache = cache + return id + def update(self): super(Planet, self).update() diff --git a/punyverse/game.py b/punyverse/game.py index 72ff54e..15d89cd 100644 --- a/punyverse/game.py +++ b/punyverse/game.py @@ -265,6 +265,12 @@ class Applet(pyglet.window.Window): glDisable(GL_ALPHA_TEST) glPopMatrix() + if self.debug and hasattr(entity, 'get_orbit') and hasattr(entity, 'parent'): + glPushMatrix() + glTranslatef(*entity.parent.location) + glCallList(entity.get_orbit()) + glPopMatrix() + glColor4f(1, 1, 1, 1) glDisable(GL_TEXTURE_2D)