def project(self, points, reverse=False): if reverse: if self.H_ is None: self.H_ = mtx.lu([r[:] for r in self.H]) qs = [ mtx.solve(self.H_, vec.sub(p, self.offset) + [1]) for p in points ] return [vec.div(q[:-1], q[-1]) for q in qs] else: qs = [mtx.mul(self.H, p + [1]) for p in points] return [vec.add(self.offset, vec.div(q[:-1], q[-1])) for q in qs]
def arc_to(self, endpoint, center=None, start_slant=None, end_slant=None): """ Draw an arc ending at the specified point, starting tangent to the current position and heading. """ if points_equal(self._position, endpoint): return # Handle unspecified center. # We need to find the center of the arc, so we can find its radius. The # center of this arc is uniquely defined by the intersection of two # lines: # 1. The first line is perpendicular to the pen heading, passing # through the pen position. # 2. The second line is the perpendicular bisector of the pen position # and the target arc end point. v_pen = self._vector() v_perp = vec.perp(self._vector()) v_chord = vec.vfrom(self._position, endpoint) if center is None: midpoint = vec.div(vec.add(self._position, endpoint), 2) v_bisector = vec.perp(v_chord) center = intersect_lines( self._position, vec.add(self._position, v_perp), midpoint, vec.add(midpoint, v_bisector), ) # Determine true start heading. This may not be the same as the # original pen heading in some circumstances. assert not points_equal(center, self._position) v_radius_start = vec.vfrom(center, self._position) v_radius_perp = vec.perp(v_radius_start) if vec.dot(v_radius_perp, v_pen) < 0: v_radius_perp = vec.neg(v_radius_perp) start_heading = math.degrees(vec.heading(v_radius_perp)) self.turn_to(start_heading) # Refresh v_pen and v_perp based on the new start heading. v_pen = self._vector() v_perp = vec.perp(self._vector()) # Calculate the arc angle. # The arc angle is double the angle between the pen vector and the # chord vector. Arcing to the left is a positive angle, and arcing to # the right is a negative angle. arc_angle = 2 * math.degrees(vec.angle(v_pen, v_chord)) radius = vec.mag(v_radius_start) # Check which side of v_pen the goes toward. if vec.dot(v_chord, v_perp) < 0: arc_angle = -arc_angle radius = -radius self._arc( center, radius, endpoint, arc_angle, start_slant, end_slant, )
def draw(s, gs): sx, sy = gs.screenCoords(s.loc) s.sprite.x = sx s.sprite.y = sy s.sprite.rotation = s.facing s.sprite.draw() if s.parent != None: #parentSloc = gs.screenCoords(s.parent.loc) vecToParent = vec.sub(s.parent.loc, s.loc) angle = vec.toAngle(vecToParent) centerPointVec = vec.div(vecToParent, 2) actualVec = vec.add(s.loc, centerPointVec) s.captureSprite.position = gs.screenCoords(actualVec) s.captureSprite.rotation = angle s.captureSprite.draw()
def __init__(self, environment, screen_size): self.environment = environment self.screen_size = screen_size self.pixels_per_unit = 15 # TEMP, hardcoded zoom level. flags = pg.HWSURFACE | pg.DOUBLEBUF if FULLSCREEN: flags |= pg.FULLSCREEN self.screen = pg.display.set_mode(tuple(self.screen_size), flags) self.screen_origin = vec.div(self.screen_size, 2) self.widgets = [] if SHOW_JOYSTICK: for i, inp in enumerate(self.environment.inputs): self.widgets.append(JoystickWidget(self, inp, i)) if SHOW_INFO: self.fps = 0.0 self.widgets.append(InfoWidget(self)) self.widgets.append(HealthWidget(self, self.environment.players)) self.graphics = Graphics(self)
def applyForce(s, force): dv = vec.div(force, s.mass) s.vel = vec.add(s.vel, dv)