-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.py
45 lines (36 loc) · 1.52 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
from vector import Vector, distance
from draw import draw_circle
GRAVITY_ACC = Vector(0, 0.02)
#Vector(0, 0.002)
class Ball:
def __init__(self, center, radius, velocity):
self.center = center
# rename it to position...
self.radius = radius
self.velocity = velocity
def intersection(self, segment):
closest_point = segment.closest_point(self.center)
if distance(closest_point, self.center) <= self.radius:
return closest_point
else:
return None
def draw(self, screen):
draw_circle(screen, (255, 255, 255), self.center, self.radius)
def collides_segment(self, segment):
return self.intersection(segment) is not None
def at(self, time):
newPosition = self.center + \
GRAVITY_ACC * (time ** 2) + self.velocity * time
newVelocity = self.velocity + GRAVITY_ACC * time
return Ball(newPosition, self.radius, newVelocity)
def apply_colissions_to_segments(self, segments):
for segment in segments:
collision_point = self.intersection(segment)
if collision_point:
self.center -= self.velocity # undo move
if segment.is_extreme(collision_point):
self.velocity = segment.direction.reflect(self.velocity)
else:
orthogonal = (collision_point - self.center).normal()
self.velocity = orthogonal.reflect(self.velocity)
self.velocity = self.velocity * 0.7