MCPcopy Create free account
hub / github.com/ActiveState/code / Ball

Class Ball

recipes/Python/502252_Physics/recipe-502252.py:86–113  ·  view source on GitHub ↗

Ball(x, y, radius) -> Ball

Source from the content-addressed store, hash-verified

84################################################################################
85
86class Ball:
87
88 'Ball(x, y, radius) -> Ball'
89
90 def __init__(self, x, y, radius):
91 'Initialize the Ball object.'
92 self.pos = Vector(x, y)
93 self.vel = Vector(0, 0)
94 self.err = Vector(0, 0)
95 self.rad = radius
96
97 def crash(self, ball):
98 'Try to crash two balls together.'
99 p = ball.pos - self.pos
100 a = abs(p)
101 if a <= self.rad + ball.rad:
102 v = self.vel - ball.vel
103 s = _sub(_ang(p), _ang(v))
104 if s < _PI_D_2:
105 e = p * (_math.cos(s) * abs(v) / a)
106 ball.err += e
107 self.err -= e
108
109 def move(self, frames_per_second):
110 'Update the ball\'s position.&#x27;
111 self.vel += self.err
112 self.err = Vector(0, 0)
113 self.pos += self.vel / frames_per_second
114
115################################################################################
116

Callers 1

initialiseFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected