-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathball.py
110 lines (86 loc) · 3.04 KB
/
ball.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
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
from pyglet.gl import *
from OpenGL.GLUT import *
from object_3d import *
import colors
class Ball(Object3D):
radius = 30
slices = 30
stacks = 20
def __init__(self, color, initxp, inityp, initzp, initxv, inityv, initzv):
Object3D.__init__(self)
self.color = color
self.xp = initxp
self.yp = inityp
self.zp = initzp
self.xv = initxv
self.yv = inityv
self.zv = initzv
def update(self, delta):
self.xp += self.xv #* delta * 50
self.yp += self.yv #* delta * 50
self.zp += self.zv #* delta * 50
# glMatrixMode(GL_MODELVIEW)
# glPushMatrix()
# glLoadMatrixf(self.OM)
# glRotatef(delta * 10, 0, 1, 0) # Rotate about y-axis
# glGetFloatv(GL_MODELVIEW_MATRIX, self.OM)
# glPopMatrix()
pass
def draw(self):
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glMultMatrixf(self.OM)
glColor4fv(self.color)
glMaterialfv(GL_FRONT, GL_SPECULAR, colors.WHITE)
glMateriali(GL_FRONT, GL_SHININESS, 60)
glTranslatef(self.xp, self.yp, self.zp)
glutSolidSphere(self.radius, self.slices, self.stacks)
glPopMatrix()
def reflect(self):
self.xv *= -1
self.yv *= -1
self.zv *= -1
self.xp += 1*self.xv
self.yp += 1*self.yv
self.zp += 1*self.zv
def xneg(self):
return self.xp - self.radius
def xpos(self):
return self.xp + self.radius
def yneg(self):
return self.yp - self.radius
def ypos(self):
return self.yp + self.radius
def zneg(self):
return self.zp - self.radius
def zpos(self):
return self.zp + self.radius
class CollidableBall(CollidableObject):
slices = 25
stacks = 25
def __init__(self, color, radius=50, mass=10, start_p=Point3(), start_v=Vector3()):
super(CollidableBall, self).__init__(mass=mass, position=start_p, velocity=start_v)
self.color = color
self.radius = radius
def update(self, delta):
# self.translate_v(self.velocity * delta) # update with delta
self.translate_v(self.velocity) # update without delta
super(CollidableBall, self).update(delta)
def draw(self):
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glMultMatrixf(self.OM)
glColor4fv(self.color)
glMaterialfv(GL_FRONT, GL_SPECULAR, colors.WHITE)
glMateriali(GL_FRONT, GL_SHININESS, 60)
glutSolidSphere(self.radius, self.slices, self.stacks)
# self.__draw_axes__(self.radius * 2)
# self.draw_velocity(self.radius * 2)
glPopMatrix()
def is_colliding(self, ball):
# assert isinstance(ball.position, CollidableObject)
dist_sqr = distance_squared(self.position(), ball.position())
if (self.radius + ball.radius) ** 2 >= dist_sqr:
if dot(self.velocity, ball.velocity) > 0:
return True
return False