-
Notifications
You must be signed in to change notification settings - Fork 1
/
velocity.py
57 lines (44 loc) · 1.36 KB
/
velocity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import division
import math
#0 degrees is in the direction of increasing y
####################
#^ 0 #
#| | #
#| | #
#y 90-----O----270 #
#| | #
#| | #
#| 180 #
#0--------x------> #
####################
class Velocity():
"""A class for holding a two dimentional velocity"""
def __init__(self, x=0, y=0):
self.setXY(x, y)
def setXY(self, x, y):
self.x = x
self.y = y
def setDM(self, deg, mag):
rad = math.radians((deg+90) % 360)#plus 90 because of what panda3d thinks is 0 degrees
self.setXY(mag*math.cos(rad), mag*math.sin(rad))
def getXY(self):
return (self.x, self.y)
def getD(self):
return (math.degrees(math.atan2(self.y,self.x))-90) % 360
def getM(self):
return math.sqrt(self.x*self.x + self.y*self.y)
def getDM(self):
return (self.getD(), self.getM())
def addXY(self, x, y):
self.x += x
self.y += y
def addDM(self, deg, mag):
temp = Velocity()
temp.setDM(deg, mag)
self.addXY(temp.x, temp.y)
def getMInD(self, D):
temp = Velocity()
temp.setDM(D, 1)
x = self.x * temp.x
y = self.y * temp.y
return x + y