def draw_cone(self, matrix, radius, height, color=coloring.RED): tip = Vector(0.0, height, 0.0) wtip = matrix.transform_point(tip) circle_points = [ matrix.transform_point(point * radius) for point in CIRCLE_POINTS ] self.draw_polygon(circle_points, color) self.draw_tip_lines(wtip, circle_points, color)
def __init__(self, parent): super(OutputLog, self).__init__() header = FONT2.render("message log:", True, (0, 0, 0)) self.image = pygame.Surface((header.get_width(), header.get_height())) self.image.fill((255, 255, 200)) self.image.blit(header, (0, 0)) self.parent = parent self.pos = Vector(0, 50)
def world_to_screen(self, position): vp_matrix = self.get_view_projection() clip_position = vp_matrix.project_point(position) height = 600 width = height * self.aspect return Vector((clip_position.x + 1.0) * width / 2.0, (clip_position.y + 1.0) * height / 2.0, 1.0 - clip_position.z)
def SetupVec(self, dim1, dim2, fixedDim, dim1Val, dim2Val, fixedDimVal): v = Vector() v[dim1] = dim1Val v[dim2] = dim2Val v[fixedDim] = fixedDimVal if self.angles: xform = matrix3x4_t() AngleMatrix(self.angles, xform) vNew = Vector() VectorRotate(v, xform, vNew) v = vNew if self.origin: v += self.origin return v
def __init__(self, listener): super(VisualSprite, self).__init__() VisualSprite.SPRITES.append(self) self.pos = Vector(0, 0) self._state = DrawState.NORMAL self.peer = VisualNode(self) self.peer.bind(*listener) self.build()
def Render(self): '''Renders the sprite (by passing in the position override parameter to :func:`SSSprite.Render`).''' x, y, z = gluProject(*self.pos.FastTo3(), view=numpy.array([-1, -1, 2, 2])) #The weird viewport above should (theoretically) give us an identity viewport. ## print 'Render at', x, y, z super(WSSprite, self).Render(Vector(x, y, z)) self.RenderChildren()
def on_mouse_press(self, x, y, button, modifiers): if button == pyglet.window.mouse.LEFT: if self.current_plane_id: print('chase plane', self.current_plane_id) param = {'speed': 2.0, 'target_id': self.current_plane_id} missile = self.entity_manager.create_entity(Missile, param) position = self.app.camera.top_down_screen_to_world( Vector(x, y, 0.0)) missile.position = position
def on_key_press(self, symbol, modifiers): if symbol == key.F5: print('reload') retroreload.retroreload(sys.modules[__name__]) elif symbol == key.SPACE: print('chase plane', self.current_plane_id) if self.current_plane_id: param = {'speed': 2.0, 'target_id': self.current_plane_id} missile = self.entity_manager.create_entity(Missile, param) missile.position = Vector(0.0, 0.0, -5.0)
def draw_sphere(self, position, radius, color=coloring.RED): longitude_segments = 8 longitude_step = math.pi / longitude_segments sub_circle_points = [position + Vector(0.0, radius, 0.0) ] * CIRCLE_SEGMENTS for i in range(1, longitude_segments): phi = i * longitude_step sub_radius = radius * math.sin(phi) sub_height = radius * math.cos(phi) offset = position + Vector(0.0, sub_height, 0.0) next_sub_circle_points = [ offset + sub_radius * point for point in CIRCLE_POINTS ] self.draw_polygon(next_sub_circle_points, color) self.draw_pair_lines(sub_circle_points, next_sub_circle_points, color) sub_circle_points = next_sub_circle_points next_sub_circle_points = [position + Vector(0.0, -radius, 0.0) ] * CIRCLE_SEGMENTS self.draw_pair_lines(sub_circle_points, next_sub_circle_points, color)
def top_down_screen_to_world(self, position): screen_x = position.x screen_y = self.height - position.y clip_x = (screen_x - self.width / 2.0) * 2.0 / self.width clip_y = (screen_y - self.height / 2.0) * 2.0 / self.height pmatrix = self.projection_ortho world_origin_x = (clip_x + pmatrix.m30) / pmatrix.m00 world_origin_y = (clip_y + pmatrix.m31) / pmatrix.m11 world_x = self.view_eye.x - world_origin_x world_z = self.view_eye.z - world_origin_y return Vector(world_x, 0.0, world_z)
def on_key_press(self, symbol, modifiers): camera = self.camera if symbol == key.O: # ortho camera.set_mode(camera.MODE_ORTHO) elif symbol == key.P: # perspective camera.set_mode(camera.MODE_PERSPECTIVE) elif symbol == key.F9: camera.set_look_at(Vector(-30.0, 0.0, 0.0), Vector(0.0, 0.0, 0.0), Vector(0.0, 1.0, 0.0)) elif symbol == key.F10: camera.set_look_at(Vector(0.0, 50.0, 0.0), Vector(0.0, 0.0, 0.0), Vector(0.0, 0.0, 1.0)) elif symbol == key.F11: camera.set_look_at(Vector(0.0, 0.0, -30.0), Vector(0.0, 0.0, 0.0), Vector(0.0, 1.0, 0.0))
def draw_cylinder(self, position0, position1, radius, color=coloring.RED): normal = (position1 - position0).normalized() rotation = Quaternion.from_from_to_rotation(Vector(0.0, 1.0, 0.0), normal) transform0 = Transform(position0, rotation, Vector(radius, radius, radius)) matrix0 = transform0.to_matrix() circle_points0 = [ matrix0.transform_point(point) for point in CIRCLE_POINTS ] transform1 = Transform(position1, rotation, Vector(radius, radius, radius)) matrix1 = transform1.to_matrix() circle_points1 = [ matrix1.transform_point(point) for point in CIRCLE_POINTS ] self.draw_polygon(circle_points0, color) self.draw_polygon(circle_points1, color) self.draw_pair_lines(circle_points0, circle_points1, color) self.draw_point(position0, color) self.draw_point(position1, color)
def Render(self): '''Renders the slider.''' glPushAttrib(GL_ENABLE_BIT) glDisable(GL_DEPTH_TEST) glDisable(GL_TEXTURE_2D) if self.bcol is not None: glColor4d(*self.bcol.FastTo4()) glRectdv((-1, -1), (1, 1)) if self.showval and self.value != self._oldvalue: self.text = str(self.value) self.Update() self.RenderText() hcol = self.hcol if hcol is None: hcol = Vector(0.5, 0.5, 0.5, 0.5) glColor4d(*hcol.FastTo4()) pos = self.ratio * 2 - 1 if self.orient == ORIENT.HORIZONTAL: glRectdv((pos - self.hwidth, -1), (pos + self.hwidth, 1)) else: glRectdv((-1, pos - self.hwidth), (1, pos + self.hwidth)) glPopAttrib()
def __init__(self, msg, src, dest): self.text = packetlib.message.MessageType.LOOKUP[msg.type][0] if msg.is_response: self.text += "r" self.msg = msg self.src = src self.dest = dest self.active = True self.pos = Vector(src.pos) self.pos.x += 40; self.pos.y += 20 self.build() super(VisualNode.Dot, self).__init__()
def PushState(self): '''Initialize the state. Depending on whether this is a top-level container, this may initialize the matrices (without affecting the viewport), or it may just set a viewport as with the usual :func:`Widget.PushState`.''' if self.xcell is None or self.ycell is None: #Initialize this as if we are a master layout (we probably are) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() self.grid.Compute(Vector(*(glGetIntegerv(GL_VIEWPORT)[2:]))) else: self.grid.Compute(self.size) super(Container, self).PushState() #Just do what every other widget does
class Actor(object): def __init__(self, world, actor_id, param): self.world = world self.actor_id = actor_id self._position = Vector() if 'position' in param: self._position = param['position'] def update(self, delta_time): pass def on_destroy(self): pass def get_position(self): return self._position.copy() def set_position(self, position): self._position = position.copy()
def init(self, app): self.app = app self.world = gel.World(self) self.non_overlap_manager = NonOverlapManager(self.world) self.player = self.world.actor_manager.create_actor(Character) self.enemy = self.world.actor_manager.create_actor( Enemy, {'position': Vector(0.0, 0.0, 5.0)}) self.block = self.world.actor_manager.create_actor( Block, {'position': Vector(5.0, 0.0, 0.0)}) self.block2 = self.world.actor_manager.create_actor( Block, {'position': Vector(4.0, 0.0, 4.0)}) camera = self.app.camera camera.set_mode(camera.MODE_ORTHO) camera.set_look_at(Vector(0.0, 50.0, 0.0), Vector(0.0, 0.0, 0.0), Vector(0.0, 0.0, 1.0))
def __init__(self, material, color, mins, maxs, origin=None, angles=None, texturex=128.0, texturey=128.0): super(FXCube, self).__init__('FXCube') self.material = material self.color = color self.texturex = texturex self.texturey = texturey # Rotate if needed self.origin = origin self.angles = angles # Create all sides vLightDir = Vector(-1, -2, -3) VectorNormalize(vLightDir) self.CreateBoxSide(1, 2, 0, mins[1], mins[2], maxs[1], maxs[2], mins[0], False, vLightDir.x * 0.5 + 0.5) self.CreateBoxSide(1, 2, 0, mins[1], mins[2], maxs[1], maxs[2], maxs[0], True, -vLightDir.x * 0.5 + 0.5) self.CreateBoxSide(0, 2, 1, mins[0], mins[2], maxs[0], maxs[2], mins[1], True, vLightDir.y * 0.5 + 0.5) self.CreateBoxSide(0, 2, 1, mins[0], mins[2], maxs[0], maxs[2], maxs[1], False, -vLightDir.y * 0.5 + 0.5) self.CreateBoxSide(0, 1, 2, mins[0], mins[1], maxs[0], maxs[1], mins[2], False, vLightDir.z * 0.5 + 0.5) self.CreateBoxSide(0, 1, 2, mins[0], mins[1], maxs[0], maxs[1], maxs[2], True, -vLightDir.z * 0.5 + 0.5)
def __init__(self): self.width = 512 self.height = 512 self.aspect = 1.0 self.view = Matrix() self.perspective_fov = math.radians(60.0) self.ortho_extent = 10.0 self.view_eye = Vector(0.0, 4.0, -10.0) self.view_at = Vector(0.0, 0.0, 0.0) self.view_up = Vector(0.0, 1.0, 0.0) self.set_look_at(self.view_eye, self.view_at, self.view_up) self.set_perspective(self.perspective_fov) self.set_ortho(self.ortho_extent) self.mode = self.MODE_PERSPECTIVE
def update(self, delta_time): game = self.world.game draw = toy.draw.LocalDraw(game.app.batch, Transform(self._position)) draw.draw_sphere(Vector(), self._radius, color=toy.coloring.BLACK)
def update(self, dt): keys = self.app.keys camera = self.camera going_up = 0.0 going_horz = 0.0 going_vert = 0.0 if keys[key.E]: going_up += 1.0 if keys[key.Q]: going_up -= 1.0 if keys[key.W]: going_vert += 1.0 if keys[key.S]: going_vert -= 1.0 if keys[key.A]: going_horz -= 1.0 if keys[key.D]: going_horz += 1.0 move_delta = self.move_speed * dt rotate_delta = self.rotate_speed * dt view_eye, view_at, view_up = camera.get_look_at() view_direction = (view_at - view_eye).normalized() drag_as_pan = False if view_up == Vector(0.0, 0.0, 1.0): drag_as_pan = True pan_dx, pan_dy = self.pan_dx_dy drag_dx, drag_dy = self.drag_dx_dy if drag_as_pan: pan_dx += drag_dx pan_dy += drag_dy drag_dx = 0.0 drag_dy = 0.0 self.pan_dx_dy = (0.0, 0.0) self.drag_dx_dy = (0.0, 0.0) if camera.mode == camera.MODE_PERSPECTIVE: pan_k = view_eye.y * math.tan( camera.perspective_fov / 2.0) * 2.0 / camera.height move_delta = 1.0 elif camera.mode == camera.MODE_ORTHO: pan_k = camera.ortho_extent * 2 / camera.height if pan_dx != 0.0 or pan_dy != 0.0: going_up -= pan_dy * pan_k going_horz -= pan_dx * pan_k if camera.mode == camera.MODE_PERSPECTIVE: view_eye += view_up * going_up * move_delta view_eye += view_direction * going_vert * move_delta right = view_direction.cross(view_up) view_eye += right * going_horz * move_delta elif camera.mode == camera.MODE_ORTHO: view_eye += view_up * (going_up + going_vert) * 1.0 right = view_direction.cross(view_up) view_eye += right * going_horz * 1.0 if drag_dx != 0.0 or drag_dy != 0.0: old_view_direction = view_direction.copy() q_yawing = Quaternion.from_angle_axis(-drag_dx * rotate_delta, view_up) view_direction = q_yawing.to_matrix().transform_vector( old_view_direction) right = view_direction.cross(view_up) q_pitching = Quaternion.from_angle_axis(drag_dy * rotate_delta, right) view_direction = q_pitching.to_matrix().transform_vector( view_direction) camera.set_look_at(view_eye, view_eye + view_direction, view_up)
def matrix_position(matrix): return Vector(matrix.d, matrix.h, matrix.l)
def draw_grid(self, step, n, color=coloring.RED): self.draw_point(Vector(0.0, 0.0, 0.0), color) border = n * step for i in range(1, n + 1): seg = i * step v0 = Vector(-seg, 0.0, -border) v1 = Vector(-seg, 0.0, border) self.draw_line(v0, v1, color) v0 = Vector(seg, 0.0, -border) v1 = Vector(seg, 0.0, border) self.draw_line(v0, v1, color) v0 = Vector(-border, 0.0, -seg) v1 = Vector(border, 0.0, -seg) self.draw_line(v0, v1, color) v0 = Vector(-border, 0.0, seg) v1 = Vector(border, 0.0, seg) self.draw_line(v0, v1, color) v0 = Vector(-border, 0.0, 0.0) v1 = Vector(border, 0.0, 0.0) red = Vector(1.0, 0.0, 0.0) self.draw_line(v0, v1, red) self.draw_point(Vector(border, 0.0, 0.0), red) v0 = Vector(0.0, 0.0, -border) v1 = Vector(0.0, 0.0, border) blue = Vector(0.0, 0.0, 1.0) self.draw_line(v0, v1, blue) self.draw_point(Vector(0.0, 0.0, border), blue)
for remote in node.peers: sock = remote.peer_sock if sock.remote == peer: wrapped = functools.partial(VisualNode.Dot.from_bytes, sprite.peer) sock.hooks["send"] = wrapped RuntimePatchHack.peer_association = staticmethod(peer_association_callback) ring = pygame.sprite.Group([ VisualSprite(("localhost", 10000 + i)) for i in xrange(10) ]) fix_ring(window, ring) mode = Mode.SELECT mouse_pos = Vector(0, 0) while not quit: window.fill() ring.draw(window.screen) window.blit(FONT.render(repr(mode), True, (0, 0, 0)), (0, 0)) for evt in pygame.event.get(): if evt.type == pygame.QUIT: quit = True elif evt.type == pygame.KEYUP: if evt.key == pygame.K_q: quit = True # Adds a new node to the ring. elif evt.key == pygame.K_a:
""" Draw. """ import math from vmath import Vector, Matrix, Quaternion, Transform from toy import coloring CIRCLE_SEGMENTS = 8 CIRCLE_STEP = math.pi * 2.0 / CIRCLE_SEGMENTS CIRCLE_POINTS = [ Vector(math.cos(i * CIRCLE_STEP), 0.0, math.sin(i * CIRCLE_STEP)) for i in range(CIRCLE_SEGMENTS) ] CUBE_VERTICES = [ Vector(-0.5, -0.5, -0.5), Vector(-0.5, -0.5, 0.5), Vector(0.5, -0.5, 0.5), Vector(0.5, -0.5, -0.5), Vector(-0.5, 0.5, -0.5), Vector(-0.5, 0.5, 0.5), Vector(0.5, 0.5, 0.5), Vector(0.5, 0.5, -0.5) ] def matrix_position(matrix): return Vector(matrix.d, matrix.h, matrix.l)
def move(self, x, y): self.pos = Vector(x, y)
def size(self): '''A 2D :class:`vmath.Vector` containing the cell sizes.''' return Vector(self.xcell.size, self.ycell.size)
def pos(self): '''A 2D :class:`vmath.Vector` containing the cell positions.''' return Vector(self.xcell.offset, self.ycell.offset)
def __init__(self, material, color, scale, origin, normal, yaw=0.0, alpha=1.0, flags=0): super(FXQuad, self).__init__('FXQuad') self.material = material self.color = self.VectorToColor(color) self.scale = scale self.origin = origin self.normal = normal self.yaw = yaw self.alpha = alpha self.flags = flags mesh = MeshBuilder(self.material, MATERIAL_QUADS) pos = Vector() vRight = Vector() vUp = Vector() if self.flags & self.FXQUAD_COLOR_FADE: self.color = Color(self.color.r() * alpha, self.color.g() * alpha, self.color.b() * alpha, int(alpha * 255)) else: self.color = Color(self.color.r(), self.color.g(), self.color.b(), int(alpha * 255)) VectorVectors(self.normal, vRight, vUp) rRight = (vRight * cos(radians(self.yaw))) - (vUp * sin(radians(self.yaw))) rUp = (vRight * cos(radians(self.yaw + 90.0))) - ( vUp * sin(radians(self.yaw + 90.0))) vRight = rRight * (scale * 0.5) vUp = rUp * (scale * 0.5) pos = self.origin + vRight - vUp vertex = MeshVertex() vertex.position = pos vertex.normal = self.normal vertex.color = self.color vertex.stage = 0 vertex.s = 1.0 vertex.t = 1.0 mesh.AddVertex(vertex) pos = self.origin - vRight - vUp vertex = MeshVertex() vertex.position = pos vertex.normal = self.normal vertex.color = self.color vertex.stage = 0 vertex.s = 0.0 vertex.t = 1.0 mesh.AddVertex(vertex) pos = self.origin - vRight + vUp vertex = MeshVertex() vertex.position = pos vertex.normal = self.normal vertex.color = self.color vertex.stage = 0 vertex.s = 0.0 vertex.t = 0.0 mesh.AddVertex(vertex) pos = self.origin + vRight + vUp vertex = MeshVertex() vertex.position = pos vertex.normal = self.normal vertex.color = self.color vertex.stage = 0 vertex.s = 1.0 vertex.t = 0.0 mesh.AddVertex(vertex) self.AddMeshBuilder(mesh)
def CreateBoxSide(self, dim1, dim2, fixedDim, minX, minY, maxX, maxY, fixedDimVal, bFlip, shade): v = Vector() color = Vector() VectorScale(self.color, shade, color) mesh = MeshBuilder(self.material, MATERIAL_TRIANGLE_STRIP) vertex1 = MeshVertex() vertex1.position = self.SetupVec(dim1, dim2, fixedDim, minX, maxY, fixedDimVal) vertex1.color = self.VectorToColor(color) vertex2 = MeshVertex() vertex2.position = self.SetupVec(dim1, dim2, fixedDim, maxX if bFlip else minX, maxY if bFlip else minY, fixedDimVal) vertex2.color = self.VectorToColor(color) vertex3 = MeshVertex() vertex3.position = self.SetupVec(dim1, dim2, fixedDim, minX if bFlip else maxX, minY if bFlip else maxY, fixedDimVal) vertex3.color = self.VectorToColor(color) vertex4 = MeshVertex() vertex4.position = self.SetupVec(dim1, dim2, fixedDim, maxX, minY, fixedDimVal) vertex4.color = self.VectorToColor(color) vertex1.s = 0.0 vertex1.t = 0.0 if not bFlip: vertex2.s = 0.0 vertex2.t = (vertex2.position - vertex1.position).Length() / self.texturey vertex3.s = (vertex3.position - vertex2.position).Length() / self.texturex vertex3.t = 0.0 vertex4.s = (vertex4.position - vertex1.position).Length() / self.texturex vertex4.t = (vertex4.position - vertex3.position).Length() / self.texturey else: vertex2.s = (vertex2.position - vertex1.position).Length() / self.texturex vertex2.t = 0.0 vertex3.s = 0.0 vertex3.t = (vertex3.position - vertex1.position).Length() / self.texturey vertex4.s = (vertex4.position - vertex1.position).Length() / self.texturex vertex4.t = (vertex4.position - vertex2.position).Length() / self.texturey mesh.AddVertex(vertex1) mesh.AddVertex(vertex2) mesh.AddVertex(vertex3) mesh.AddVertex(vertex4) self.AddMeshBuilder(mesh)