Disabled actor rotation for speed
[flail.git] / tracker.py
blob95fe8573164bc46d2a4abc6e54916f4708d7961b
1 from __future__ import division
5 class tracker (object):
7 __slots__ = 'xtol', 'vtol', 'a', 'v', 'x', 'y'
9 def __init__ (self, a = 0, xtol = 0, vtol = 0):
10 self.xtol = xtol
11 self.vtol = vtol
13 self.a = a
14 self.v = 0
15 self.x = 0
16 self.y = 0
18 def __str__ (self):
19 return '(%f -> %f)' % (self.y, self.x)
21 __repr__ = __str__
23 def __add__ (self, other):
24 return self.y + float (other)
26 def __sub__ (self, other):
27 return self.y - float (other)
29 def __mul__ (self, other):
30 return self.y * float (other)
32 def __floordiv__ (self, other):
33 return self.y // float (other)
35 def __mod__ (self, other):
36 return self.y % float (other)
38 def __divmod__ (self, other):
39 return divmod (self.y, float (other))
41 def __pow__ (self, other):
42 return self.y ** float (other)
44 def __truediv__ (self, other):
45 return self.y / float (other)
47 __div__ = __truediv__
49 def __radd__ (self, other):
50 return float (other) + self.y
52 def __rsub__ (self, other):
53 return float (other) - self.y
55 def __rmul__ (self, other):
56 return float (other) * self.y
58 def __rfloordiv__ (self, other):
59 return float (other) // self.y
61 def __rmod__ (self, other):
62 return float (other) % self.y
64 def __rdivmod__ (self, other):
65 return divmod (float (other), self.y)
67 def __rpow__ (self, other):
68 return float (other) ** self.y
70 def __rtruediv__ (self, other):
71 return float (other) / self.y
73 __rdiv__ = __rtruediv__
75 def __iadd__ (self, other):
76 self.y += float (other)
78 def __isub__ (self, other):
79 self.y -= float (other)
81 def __imul__ (self, other):
82 self.y *= float (other)
84 def __ifloordiv__ (self, other):
85 self.y //= float (other)
87 def __imod__ (self, other):
88 self.y %= float (other)
90 def __ipow__ (self, other):
91 self.y **= float (other)
93 def __itruediv__ (self, other):
94 self.y /= float (other)
96 __idiv__ = __itruediv__
98 def __neg__ (self):
99 return -self.y
101 def __pos__ (self):
102 return self.y
104 def __abs__ (self):
105 return abs (self.y)
107 def __float__ (self):
108 return self.y
110 def update (self, dt):
111 a = self.a
112 v = self.v
113 x = self.x
114 y = self.y
116 if x < y:
117 a = -a
119 try:
120 from math import sqrt
121 disc = sqrt (v * v / 2 + a * (x - y))
123 if x < y:
124 t = (-v - disc) / a
125 else:
126 t = (-v + disc) / a
127 except ZeroDivisionError:
128 pass
130 if t >= dt:
131 dv = a * dt
132 self.y += (v + dv / 2) * dt
133 self.v += dv
134 elif t >= 0:
135 dv = a * t
136 self.y += (v + dv / 2) * t
137 self.v += dv
138 dt -= t
139 dv = -a * dt
140 self.y += (self.v + dv / 2) * dt
141 self.v += dv
142 else:
143 dv = -a * dt
144 self.y += (v + dv / 2) * dt
145 self.v += dv
147 if abs (self.x - self.y) < self.xtol and abs (self.v) < self.vtol:
148 self.y = self.x
149 self.v = 0