From d26c7dd536c4c98a1847edbb618d8d549f85ba06 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 26 Oct 2013 11:48:58 -0400 Subject: [PATCH] Added handling of inclination, longitude of ascending node, and argument of periapsis. Signed-off-by: Xiaomao Chen --- punyverse/entity.py | 12 ++++---- punyverse/game.py | 4 +-- punyverse/orbit.py | 73 ++++++++++++++++++++++++++++++++++++++++----- punyverse/world.py | 3 +- 4 files changed, 77 insertions(+), 15 deletions(-) diff --git a/punyverse/entity.py b/punyverse/entity.py index b6d28e9..1274d51 100644 --- a/punyverse/entity.py +++ b/punyverse/entity.py @@ -52,15 +52,17 @@ class Satellite(Planet): self.orbit_speed = kwargs.pop('orbit_speed', 1) # Semi-major axis and eccentricity defines orbit - self.distance = kwargs.pop('distance', 100) - self.eccentricity = kwargs.pop('eccentricity', 0) + distance = kwargs.pop('distance', 100) + eccentricity = kwargs.pop('eccentricity', 0) - # Inclination and longitude of ascending node defines orbital plane - self.inclination = kwargs.pop('inclination', 0) + # Inclination, longitude of ascending node, and argument of periapsis defines orbital plane + inclination = kwargs.pop('inclination', 0) + longitude = kwargs.pop('longitude', 0) + argument = kwargs.pop('argument', 0) self.theta = 0 # OpenGL's z-axis is reversed - self.orbit = KeplerOrbit(self.distance, self.eccentricity, -self.inclination) + self.orbit = KeplerOrbit(distance, eccentricity, inclination, longitude, argument) super(Satellite, self).__init__(*args, **kwargs) def update(self): diff --git a/punyverse/game.py b/punyverse/game.py index 202d09f..72ff54e 100644 --- a/punyverse/game.py +++ b/punyverse/game.py @@ -159,9 +159,9 @@ class Applet(pyglet.window.Window): elif symbol == key.NUM_DIVIDE: self.speed -= 10 elif symbol == key.PAGEUP: - self.speed += 1000 + self.speed += 100 elif symbol == key.PAGEDOWN: - self.speed -= 1000 + self.speed -= 100 elif symbol == key.I: self.info = not self.info elif symbol == key.D: diff --git a/punyverse/orbit.py b/punyverse/orbit.py index fb21589..eac60c7 100644 --- a/punyverse/orbit.py +++ b/punyverse/orbit.py @@ -1,15 +1,69 @@ -from math import sin, cos, tan, atan, sqrt, radians +from math import sin, cos, tan, atan, sqrt, radians, degrees class KeplerOrbit(object): - def __init__(self, sma, eccentricity, inclination=0): + def __init__(self, sma, eccentricity, inclination=0, longitude=0, argument=0): self.sma = sma self.eccentricity = eccentricity - self.inclination = radians(inclination) + self.inclination = inclination + self.longitude = longitude + self.argument = argument self.__true_anomaly_factor = sqrt((1 + eccentricity)/(1 - eccentricity)) self.__distance_factor = sma * (1 - eccentricity ** 2) - self.__sin_inclination = sin(self.inclination) - self.__cos_inclination = cos(self.inclination) + + @property + def inclination(self): + return degrees(self._inclination) + + @inclination.setter + def inclination(self, value): + self.inclination_radian = radians(value) + + @property + def inclination_radian(self): + return self._inclination + + @inclination_radian.setter + def inclination_radian(self, value): + self._inclination = value + self.__sin_inclination = sin(self._inclination) + self.__cos_inclination = cos(self._inclination) + + @property + def longitude(self): + return degrees(self._longitude) + + @longitude.setter + def longitude(self, value): + self.longitude_radian = radians(value) + + @property + def longitude_radian(self): + return self._longitude + + @longitude_radian.setter + def longitude_radian(self, value): + self._longitude = value + self.__sin_longitude = sin(self._longitude) + self.__cos_longitude = cos(self._longitude) + + @property + def argument(self): + return degrees(self._argument) + + @argument.setter + def argument(self, value): + self.argument_radian = radians(value) + + @property + def argument_radian(self): + return self._argument + + @argument_radian.setter + def argument_radian(self, value): + self._argument = value + self.__sin_argument = sin(self._argument) + self.__cos_argument = cos(self._argument) def eccentric_anomaly(self, mean_anomaly): e1 = mean_anomaly @@ -30,7 +84,12 @@ class KeplerOrbit(object): y = r * sin(phi) z = 0 - x, z = (x * self.__cos_inclination - z * self.__sin_inclination, - x * self.__sin_inclination + z * self.__cos_inclination) + # phi = longitude, theta = inclination, psi = argument + x, y = (x * self.__cos_longitude + y * self.__sin_longitude, + -x * self.__sin_longitude + y * self.__cos_longitude) + x, z = (x * self.__cos_inclination + z * self.__sin_inclination, + -x * self.__sin_inclination + z * self.__cos_inclination) + x, y = (x * self.__cos_argument + y * self.__sin_argument, + -x * self.__sin_argument + y * self.__cos_argument) return x, y, z diff --git a/punyverse/world.py b/punyverse/world.py index e684059..2b8d921 100644 --- a/punyverse/world.py +++ b/punyverse/world.py @@ -109,7 +109,8 @@ def load_world(file): x -= distance type = partial(Satellite, parent=parent, orbit_speed=info.get('orbit_speed', 1), distance=distance, eccentricity=info.get('eccentricity', 0), - inclination=info.get('inclination', 0)) + inclination=info.get('inclination', 0), longitude=info.get('longitude', 0), + argument=info.get('argument', 0)) atmosphere_id = 0 cloudmap_id = 0