-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysicsobject.py
More file actions
165 lines (140 loc) · 4.71 KB
/
physicsobject.py
File metadata and controls
165 lines (140 loc) · 4.71 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import division
import math
MAX_VELOCITY = 25
SLOW_RATE = 0.1
def polar_to_rect( mag, arg ):
return mag*math.cos(arg), mag*math.sin(arg)
def rect_to_polar( x, y ):
mag = math.sqrt(x*x + y*y)
arg = math.atan2( y, x )
if x < 0:
arg = -arg
arg = arg % (2*math.pi)
return mag, arg
def sign( x ):
if x > 0:
return 1
else:
return -1
def do_collision( va, vb ):
if vb == 0:
return -(va/2)
elif va == 0:
return (vb/2)
dv = (abs(va) + abs(vb)) / 2
if sign(va) == sign(vb):
return sign(va)*dv
else:
return -sign(va)*dv
class PhysicsObject(object):
def __init__( self, (x,y), (vmag,varg), (sizex,sizey) ):
self.x, self.y = x,y
self.vmag, self.varg = vmag,varg
self.sizex, self.sizey = sizex, sizey
self.top = self.y - self.sizey/2
self.bottom = self.y + self.sizey/2
self.left = self.x - self.sizex/2
self.right = self.x + self.sizex/2
self.next_top = self.top
self.next_bottom = self.bottom
self.next_left = self.left
self.next_right = self.right
def __str__( self ):
return "x: {0}, y: {1}; vmag: {2}, varg: {3}".format( self.x, self.y, self.vmag, self.varg)
def start_update( self, a, darg ):
self.next_varg = (self.varg + darg) % (2*math.pi)
if a == 0:
a = -SLOW_RATE
self.next_vmag = self.vmag + a
if self.next_vmag > MAX_VELOCITY:
self.next_vmag = MAX_VELOCITY
if self.next_vmag < 0:
self.next_vmag = 0
self.next_x, self.next_y = polar_to_rect( self.next_vmag, self.next_varg )
self.next_x, self.next_y = self.next_x + self.x, self.next_y + self.y
self.next_top = self.next_y - self.sizey/2
self.next_bottom = self.next_y + self.sizey/2
self.next_left = self.next_x - self.sizex/2
self.next_right = self.next_x + self.sizex/2
def update_bounce( self, other ):
selfdx, selfdy = polar_to_rect( self.vmag, self.varg )
otherdx, otherdy = polar_to_rect( other.vmag, other.varg )
intersection = self.intersect( other )
if intersection == None:
return
self.next_x = self.x
self.next_y = self.y
if intersection == "top":
#self.next_y = other.y + 16
selfdy = do_collision( selfdy, otherdy )
elif intersection == "bottom":
#self.next_y = other.y - 16
selfdy = do_collision( selfdy, otherdy )
elif intersection == "left":
#self.next_x = other.x - 16
selfdx = do_collision( selfdx, otherdx )
elif intersection == "right":
#self.next_x = other.x + 16
selfdx = do_collision( selfdx, otherdx )
self.next_vmag, self.next_varg = rect_to_polar( selfdx, selfdy )
dx, dy = polar_to_rect( self.next_vmag, self.next_varg )
self.next_x, self.next_y = self.next_x + dx, self.next_y + dy
def execute_update( self ):
self.x = self.next_x
self.y = self.next_y
self.vmag = self.next_vmag
self.varg = self.next_varg
self.top = self.next_top
self.bottom = self.next_bottom
self.left = self.next_left
self.right = self.next_right
def isAbove( self, other, updated=False ):
if updated == True:
return self.next_bottom < other.next_top
return self.bottom < other.top
def isBelow( self, other, updated=False ):
if updated == True:
return self.next_top > other.next_bottom
return self.top > other.bottom
def isLeft( self, other, updated=False ):
if updated == True:
return self.next_right < other.next_left
return self.right < other.left
def isRight( self, other, updated=False ):
if updated == True:
return self.next_left > other.next_right
return self.left > other.right
def intersect( self, other ):
#print self.isAbove( other, True ), self.isBelow( other, True ), self.isLeft( other, True ), self.isRight( other, True )
#print self.next_right, other.next_right
#print not self.isAbove( other, True ) and not self.isBelow( other, True ) and not self.isLeft( other, True ) and not self.isRight( other, True )
if not self.isAbove( other, True ) and not self.isBelow( other, True ) and not self.isLeft( other, True ) and not self.isRight( other, True ):
#print "Collision"
if self.isAbove( other ):
#print "top"
return "top"
elif self.isBelow( other ):
#print "bottom"
return "bottom"
elif self.isLeft( other ):
#print "left"
return "left"
elif self.isRight( other ):
#print "right"
return "right"
else:
return None
if __name__ == "__main__":
a = PhysicsObject( (0,0), (0,0), (0,0) )
print a
a.start_update( 0, 0 )
a.execute_update()
print a
a.start_update( 1, 0 )
a.execute_update()
print a
a.start_update( 0, math.pi )
a.execute_update()
print a
a.start_update( 1, math.pi )
a.execute_update()