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): 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 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 setup_viewer(viewer): if scene == "shark": viewer.addEffect(fx.DrawSpeedTween(1, 45, 1, 20)) viewer.addWireframe('spin', obj.loadOBJ("shark.obj")) viewer.wireframes['spin'].transform(wf.scaleMatrix(150)) elif scene == "text": viewer.addEffect(fx.DrawSpeedTween(1, 150, 1, 50)) viewer.addWireframe('spin', obj.loadOBJ("text_test.obj")) viewer.wireframes['spin'].transform(wf.scaleMatrix(3)) viewer.wireframes['spin'].transform(wf.rotateZMatrix(-np.pi)) viewer.wireframes['spin'].transform(wf.rotateYMatrix(-np.pi)) elif scene == "watchdogs": viewer.addEffect(fx.DrawSpeedTween(1, 70, 1, 10)) viewer.addWireframe('spin', obj.loadOBJ("watchdogs.obj")) viewer.wireframes['spin'].transform(wf.scaleMatrix(3)) viewer.wireframes['spin'].transform(wf.rotateZMatrix(-np.pi)) viewer.wireframes['spin'].transform(wf.rotateYMatrix(-np.pi)) elif scene == "milkey": viewer.addEffect(fx.MIDIModulator("milkey.uss")) viewer.addWireframe('head', shape.Spheroid((0,)*3, (150,)*3, resolution=5)) viewer.addWireframe('left_ear', shape.Spheroid((0,)*3, (75,)*3, resolution=3)) viewer.addWireframe('right_ear', shape.Spheroid((0,)*3, (75,)*3, resolution=3)) elif scene == "cube": viewer.addEffect(fx.DrawSpeedTween(1, 250, 2, 50)) viewer.addWireframe('spin', shape.Cuboid((0,)*3, (150,)*3)) elif scene == "sphere": viewer.addEffect(fx.DrawSpeedTween(0.25, 130, 0.25, 20)) viewer.addWireframe('spin', shape.Spheroid((0,)*3, (150,)*3)) if scene != "milkey": viewer.centerWireframe("spin") else: viewer.centerWireframe("head") viewer.centerWireframe("left_ear") viewer.wireframes["left_ear"].transform(wf.translationMatrix(150, -150, 0)) viewer.centerWireframe("right_ear") viewer.wireframes["right_ear"].transform(wf.translationMatrix(-150, -150, 0)) viewer.key_to_function = {} viewer.object_update = object_update
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 rotate_bill(self, angle): """ Rotates bill of hat in simulation. """ global correction_matrix # Rotate bill in relative coordinates. center = self.hat.base.findCenter() hat.bill.nodes = self.hat.bill.nodes - center rotateY = wf.rotateYMatrix(angle) # Rotate relative to correction matrix. self.hat.bill.nodes = np.dot(self.hat.bill.nodes, correction_matrix) self.hat.bill.nodes = np.dot(self.hat.bill.nodes, rotateY) self.hat.bill.nodes = np.dot(self.hat.bill.nodes, np.linalg.inv(correction_matrix)) self.hat.bill.nodes = self.hat.bill.nodes + center
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_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.display.set_caption(name)
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.display.set_caption(name)