Convert degrees to radians at expression evaluation; use radians for rotation internally.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 17 Mar 2010 04:29:57 +0000 (21:29 -0700)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 17 Mar 2010 04:29:57 +0000 (21:29 -0700)
bulletml/impl.py
bulletml/parser.py

index 65d234a699f267754d3ece0253bfda4185ff1878..cc693e15cea69f02af60a6e9c60e082a2b5dbe6f 100644 (file)
@@ -12,6 +12,8 @@ from bulletml import parser
 # TODO(jfw): This is very non-Pythonic, it's pretty much just the
 # BulletML reference ActionImpl translated to Python.
 
+PI_2 = math.pi * 2
+
 class Action(object):
     """Running action implementation."""
 
@@ -177,7 +179,7 @@ class Action(object):
                     if type == "absolute":
                         self.aiming = False
                         self.direction = (
-                            direction - self.owner.direction) % 360
+                            direction - self.owner.direction) % PI_2
                     elif type == "relative":
                         self.aiming = False
                         self.direction = direction
@@ -186,12 +188,12 @@ class Action(object):
                         self.direction = (
                             direction
                             + self.owner.aim
-                            - self.owner.direction) % 360
+                            - self.owner.direction) % PI_2
 
-                    while self.direction > 180:
-                        self.direction -= 360
-                    while self.direction < -180:
-                        self.direction += 360
+                    while self.direction > math.pi:
+                        self.direction -= PI_2
+                    while self.direction < -math.pi:
+                        self.direction += PI_2
                     self.direction /= self.direction_frames
 
             elif isinstance(action, parser.Accel):
@@ -257,8 +259,7 @@ class Bullet(object):
         if self.target is None:
             return self.direction
         else:
-            return math.degrees(
-                math.atan2(self.target.x - self.x, self.y - self.target.y))
+            return math.atan2(self.target.x - self.x, self.y - self.target.y)
 
     @property
     def finished(self):
@@ -291,11 +292,10 @@ class Bullet(object):
         for action in self.actions:
             created.extend(action.step())
 
-        direction = math.radians(self.direction)
         self.px = self.x
         self.py = self.y
-        self.x += self.mx + math.sin(direction) * self.speed
-        self.y += self.my - math.cos(direction) * self.speed
+        self.x += self.mx + math.sin(self.direction) * self.speed
+        self.y += self.my - math.cos(self.direction) * self.speed
 
         return created
 
index e2b7c800d03f7d30f5bb0d0fc1c04ee47120e24d..b39437ac8dd951d3c20def3b6523c2b68f12f1ed 100644 (file)
@@ -5,6 +5,8 @@ http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html
 
 from __future__ import division
 
+import math
+
 from xml.etree.ElementTree import ElementTree
 
 try:
@@ -61,7 +63,7 @@ class Direction(object):
         return cls(element.get("type", default), NumberDef(element.text))
 
     def __call__(self, params, rank):
-        return (self.value(params, rank), self.type)
+        return (math.radians(self.value(params, rank)), self.type)
 
     def __repr__(self):
         return "%s(%r, type=%r)" % (