def collide(self, obj): dist = sqrt((self.pos[0]-obj.pos[0])**2 + (self.pos[1]-obj.pos[1])**2) d = util.vsub(obj.pos, self.pos) par = util.vproj(self.vel, d) orth = util.vsub(self.vel, par) if util.vdot(self.vel, d) > 0: self.force = util.vadd(self.force, util.vmuls(par, -1.5 * self.mass)) overlap = max(((self.size + obj.size) - dist) / 20.0, .1) self.force = util.vadd(self.force, util.vmuls(d, -overlap / dist * self.mass))
def draw(self): orth = [self.vel[1], -self.vel[0]] orth = util.vmuls(orth, self.size / util.vmag(orth)) return group(colors.red, shapes.regular_polygon(self.pos[0], self.pos[1], 30, self.size), quads(color(1, 0, 0, .5), self.pos[0] + orth[0], self.pos[1] + orth[1], self.pos[0] - orth[0], self.pos[1] - orth[1], color(1, 0, 0, 0), self.pos[0] - 10*self.vel[0] - orth[0], self.pos[1] - 10*self.vel[1] - orth[1], self.pos[0] - 10*self.vel[0] + orth[0], self.pos[1] - 10*self.vel[1] + orth[1] ))