def keyEvent(self, key): if key == pygame.K_a: # print("a is pressed") temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateYMatrix(-pi / 16))[:-1] if key == pygame.K_d: # print("d is pressed") temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateYMatrix(pi / 16))[:-1] if key == pygame.K_w: # print("w is pressed") temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateXMatrix(pi / 16))[:-1] if key == pygame.K_s: # print("s is pressed") temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateXMatrix(-pi / 16))[:-1] if key == pygame.K_q: # print("q is pressed") temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateZMatrix(pi / 16))[:-1] if key == pygame.K_e: # print("e is pressed") temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateZMatrix(-pi / 16))[:-1] return
def rotateAll(self, axis, theta): """ Rotates all wireframe objects in simulation. """ global correction_matrix # Create rotation matrix, and update global correction matrix. if axis == 'X': rotateMatrix = wf.rotateXMatrix(theta) correction_matrix = np.dot(wf.rotateXMatrix(-theta), correction_matrix) elif axis == 'Y': rotateMatrix = wf.rotateYMatrix(theta) correction_matrix = np.dot(wf.rotateYMatrix(-theta), correction_matrix) elif axis == 'Z': rotateMatrix = wf.rotateZMatrix(theta) correction_matrix = np.dot(wf.rotateZMatrix(-theta), correction_matrix) for wireframe in [self.hat.base, self.hat.bill, self.hat.lightSensors]: center = hat.base.findCenter() wireframe.nodes = wireframe.nodes - center wireframe.transform(rotateMatrix) wireframe.nodes = wireframe.nodes + center
def keyEvent(self, key): homogeneous_light_vector = np.array([*self.light_vector, 0]) #Your code here if key == pygame.K_w: self.light_vector = np.matmul(homogeneous_light_vector, wf.rotateXMatrix(np.radians(5)))[:3] if key == pygame.K_s: self.light_vector = np.matmul(homogeneous_light_vector, wf.rotateXMatrix(-np.radians(5)))[:3] if key == pygame.K_a: self.light_vector = np.matmul(homogeneous_light_vector, wf.rotateYMatrix(-np.radians(5)))[:3] if key == pygame.K_d: self.light_vector = np.matmul(homogeneous_light_vector, wf.rotateYMatrix(np.radians(5)))[:3] if key == pygame.K_q: self.light_vector = np.matmul(homogeneous_light_vector, wf.rotateZMatrix(np.radians(5)))[:3] if key == pygame.K_e: self.light_vector = np.matmul(homogeneous_light_vector, wf.rotateZMatrix(-np.radians(5)))[:3] return
def keyEvent(self, key): #Your code here if key == pygame.K_a: print("a is pressed") self.light_vector = np.matmul(wf.rotateYMatrix( math.pi / 20), (np.append(self.light_vector, [1], axis=0))) self.light_vector = self.light_vector[0:3] self.light_vector = self.light_vector / np.sqrt( np.sum(self.light_vector**2)) if key == pygame.K_d: print("d is pressed") #self.light_vector = wf.rotateYMatrix(-math.pi/20) * self.light_vector self.light_vector = np.matmul(wf.rotateYMatrix( -math.pi / 20), (np.append(self.light_vector, [1], axis=0))) self.light_vector = self.light_vector[0:3] self.light_vector = self.light_vector / np.sqrt( np.sum(self.light_vector**2)) if key == pygame.K_w: print("w is pressed") #self.light_vector = wf.rotateXMatrix(math.pi/20) * self.light_vector self.light_vector = np.matmul(wf.rotateXMatrix( -math.pi / 20), (np.append(self.light_vector, [1], axis=0))) self.light_vector = self.light_vector[0:3] self.light_vector = self.light_vector / np.sqrt( np.sum(self.light_vector**2)) if key == pygame.K_s: print("s is pressed") #self.light_vector = wf.rotateXMatrix(-math.pi/20) * self.light_vector self.light_vector = np.matmul(wf.rotateXMatrix( math.pi / 20), (np.append(self.light_vector, [1], axis=0))) self.light_vector = self.light_vector[0:3] self.light_vector = self.light_vector / np.sqrt( np.sum(self.light_vector**2)) if key == pygame.K_q: print("q is pressed") #self.light_vector = wf.rotateZMatrix(math.pi/20) * self.light_vector self.light_vector = np.matmul(wf.rotateZMatrix( -math.pi / 20), (np.append(self.light_vector, [1], axis=0))) self.light_vector = self.light_vector[0:3] self.light_vector = self.light_vector / np.sqrt( np.sum(self.light_vector**2)) if key == pygame.K_e: print("e is pressed") #self.light_vector = wf.rotateZMatrix(-math.pi/20) * self.light_vector self.light_vector = np.matmul(wf.rotateZMatrix( math.pi / 20), (np.append(self.light_vector, [1], axis=0))) self.light_vector = self.light_vector[0:3] self.light_vector = self.light_vector / np.sqrt( np.sum(self.light_vector**2)) return
def make_wf_hat(hat, pv, nodes_per_hemi = 100): """ Makes wireframe hat consisting of base, bill and light sensors. """ global correction_matrix scale = 50 # Make untransformed base, bill and lightSensor wireframes. base = wf.Wireframe() bill = wf.Wireframe() lightSensors = wf.Wireframe() hat_center = hat.position bill_center = 1 * hat_center ls_center = 1 * hat_center ls_center[1] += scale bill_center[0] += hat.billOffset * scale * np.cos(hat.theta) bill_center[2] += hat.billOffset * scale * np.sin(hat.theta) bill_radius = hat.billDiam / 2 * scale base_radius = hat.diam / 2 * scale lightSensors.make_circle(ls_center, base_radius, len(hat.lightSensors)) lightSensors.clearEdges() bill.make_dense_circle(bill_center, bill_radius, nodes_per_hemi) bill.edges[:] = [x for x in bill.edges if (np.linalg.norm(bill.nodes[x[0]][0:3] - hat_center) > base_radius and np.linalg.norm(bill.nodes[x[1]][0:3] - hat_center) > base_radius)] base.make_sphere(hat_center, base_radius, nodes_per_hemi, 1 * np.pi, 20, 11) # Transform wireframes to viewing angle. center = np.hstack((hat_center, 1)) base.nodes = base.nodes - center bill.nodes = bill.nodes - center lightSensors.nodes = lightSensors.nodes - center rotateXMatrix = wf.rotateXMatrix(pv.viewing_angle) rotateYMatrix = wf.rotateYMatrix(pv.viewing_angle) rotateMatrix = np.dot(rotateYMatrix, rotateXMatrix) rotateMatrix = 1 * rotateXMatrix # Update correction matrix. correction_matrix = np.dot(np.linalg.inv(rotateMatrix), correction_matrix) bill.nodes = np.dot(bill.nodes, rotateMatrix) base.nodes = np.dot(base.nodes, rotateMatrix) lightSensors.nodes = np.dot(lightSensors.nodes, rotateMatrix) bill.nodes = bill.nodes + center base.nodes = base.nodes + center lightSensors.nodes = lightSensors.nodes + center return wf.WireframeHat(base, bill, lightSensors)
def keyEvent(self, key): global Xdisp #Your code here if key == pygame.K_w: temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateXMatrix(math.pi/16))[:-1] if key == pygame.K_s: temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateXMatrix(-math.pi/16))[:-1] if key == pygame.K_a: temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateYMatrix(-math.pi/16))[:-1] if key == pygame.K_d: temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateYMatrix(math.pi/16))[:-1] if key == pygame.K_q: temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateZMatrix(math.pi/16))[:-1] if key == pygame.K_e: temp = self.light_vector temp = np.insert(temp, 3, 1) self.light_vector = np.dot(temp, wf.rotateZMatrix(-math.pi/16))[:-1] if key == pygame.K_p: Xdisp += .01 print(Xdisp) if Xdisp > .7: Xdisp -= 1 if key == pygame.K_l: Xdisp -= .01 print(Xdisp) if Xdisp < 0: Xdisp += 1 return
def rotate(self, axis, amount): (x, y, z) = self.findCentre() translation_matrix1 = wf.translationMatrix(-x, -y, -z) translation_matrix2 = wf.translationMatrix(x, y, z) if axis == 'x': rotation_matrix = wf.rotateXMatrix(amount) elif axis == 'y': rotation_matrix = wf.rotateYMatrix(amount) elif axis == 'z': rotation_matrix = wf.rotateZMatrix(amount) rotation_matrix = np.dot(np.dot(translation_matrix1, rotation_matrix), translation_matrix2) self.transform(rotation_matrix)
def testRotate(): """ Example of how to rotate a wireframe. Creates a cuboid and rotates about its centre by pi/2 radians. """ cuboid = shape.Cuboid((100,100,10), (20,30,40)) cuboid.outputNodes() # Find rotation matrix (x,y,z) = cuboid.findCentre() translation_matrix = wf.translationMatrix(-x, -y, -z) rotation_matrix = np.dot(translation_matrix, wf.rotateXMatrix(math.pi/2)) rotation_matrix = np.dot(rotation_matrix, -translation_matrix) print "\n> Rotate cuboid around its centre and the x-axis" cuboid.transform(rotation_matrix) cuboid.outputNodes()
def testRotate(): """ Example of how to rotate a wireframe. Creates a cuboid and rotates about its centre by pi/2 radians. """ cuboid = shape.Cuboid((100, 100, 10), (20, 30, 40)) cuboid.outputNodes() # Find rotation matrix (x, y, z) = cuboid.findCentre() translation_matrix = wf.translationMatrix(-x, -y, -z) rotation_matrix = np.dot(translation_matrix, wf.rotateXMatrix(math.pi / 2)) rotation_matrix = np.dot(rotation_matrix, -translation_matrix) print "\n> Rotate cuboid around its centre and the x-axis" cuboid.transform(rotation_matrix) cuboid.outputNodes()
def rotate(self, axis, amount): """Rotate the "camera" by rotating all other wireframes""" (x, y, z) = self.findCenter() translation_matrix1 = wf.translationMatrix(-x, -y, -z) translation_matrix2 = wf.translationMatrix(x, y, z) axis = axis.lower() if axis == 'x': rotation_matrix = wf.rotateXMatrix(amount) elif axis == 'y': rotation_matrix = wf.rotateYMatrix(amount) elif axis == 'z': rotation_matrix = wf.rotateZMatrix(amount) else: raise Exception("Invalid rotation axis. Valid axes are x, y, and z.") rotation_matrix = np.dot(np.dot(translation_matrix1, rotation_matrix), translation_matrix2) self.transform(rotation_matrix)
pygame.K_LEFT: (lambda x: x.transform(wf.translationMatrix(dx=-MOVEMENT_AMOUNT))), pygame.K_RIGHT: (lambda x: x.transform(wf.translationMatrix(dx= MOVEMENT_AMOUNT))), pygame.K_UP: (lambda x: x.transform(wf.translationMatrix(dy=-MOVEMENT_AMOUNT))), pygame.K_DOWN: (lambda x: x.transform(wf.translationMatrix(dy= MOVEMENT_AMOUNT))), pygame.K_EQUALS: (lambda x: x.scale(1.25)), pygame.K_MINUS: (lambda x: x.scale(0.8)), pygame.K_q: (lambda x: x.rotate('x', ROTATION_AMOUNT)), pygame.K_w: (lambda x: x.rotate('x',-ROTATION_AMOUNT)), pygame.K_a: (lambda x: x.rotate('y', ROTATION_AMOUNT)), pygame.K_s: (lambda x: x.rotate('y',-ROTATION_AMOUNT)), pygame.K_z: (lambda x: x.rotate('z', ROTATION_AMOUNT)), pygame.K_x: (lambda x: x.rotate('z',-ROTATION_AMOUNT)) } light_movement = { pygame.K_q: (lambda x: x.transform(wf.rotateXMatrix(-ROTATION_AMOUNT))), pygame.K_w: (lambda x: x.transform(wf.rotateXMatrix( ROTATION_AMOUNT))), pygame.K_a: (lambda x: x.transform(wf.rotateYMatrix(-ROTATION_AMOUNT))), pygame.K_s: (lambda x: x.transform(wf.rotateYMatrix( ROTATION_AMOUNT))), pygame.K_z: (lambda x: x.transform(wf.rotateZMatrix(-ROTATION_AMOUNT))), pygame.K_x: (lambda x: x.transform(wf.rotateZMatrix( ROTATION_AMOUNT))) } class WireframeViewer(wf.WireframeGroup): """ A group of wireframes which can be displayed on a Pygame screen """ def __init__(self, width, height, name="Wireframe Viewer"): self.width = width self.height = height self.screen = pygame.display.set_mode((width, height))
pygame.K_UP: (lambda x: x.transform(wf.translationMatrix(dy=-MOVEMENT_AMOUNT))), pygame.K_DOWN: (lambda x: x.transform(wf.translationMatrix(dy=MOVEMENT_AMOUNT))), pygame.K_EQUALS: (lambda x: x.scale(1.25)), pygame.K_MINUS: (lambda x: x.scale(0.8)), pygame.K_q: (lambda x: x.rotate('x', ROTATION_AMOUNT)), pygame.K_w: (lambda x: x.rotate('x', -ROTATION_AMOUNT)), pygame.K_a: (lambda x: x.rotate('y', ROTATION_AMOUNT)), pygame.K_s: (lambda x: x.rotate('y', -ROTATION_AMOUNT)), pygame.K_z: (lambda x: x.rotate('z', ROTATION_AMOUNT)), pygame.K_x: (lambda x: x.rotate('z', -ROTATION_AMOUNT)) } light_movement = { pygame.K_q: (lambda x: x.transform(wf.rotateXMatrix(-ROTATION_AMOUNT))), pygame.K_w: (lambda x: x.transform(wf.rotateXMatrix(ROTATION_AMOUNT))), pygame.K_a: (lambda x: x.transform(wf.rotateYMatrix(-ROTATION_AMOUNT))), pygame.K_s: (lambda x: x.transform(wf.rotateYMatrix(ROTATION_AMOUNT))), pygame.K_z: (lambda x: x.transform(wf.rotateZMatrix(-ROTATION_AMOUNT))), pygame.K_x: (lambda x: x.transform(wf.rotateZMatrix(ROTATION_AMOUNT))) } class WireframeViewer(wf.WireframeGroup): """ A group of wireframes which can be displayed on a Pygame screen """ def __init__(self, width, height, name="Wireframe Viewer"): self.width = width self.height = height self.screen = pygame.display.set_mode((width, height))