def FindFacingAxis(self): best_axis = None best_z = 0.0 for i in range(2): for j in range(3): axis = Vector() c = 1.0 if i == 0 else -1.0 axis.x = c if j == 0 else 0.0 axis.y = c if j == 1 else 0.0 axis.z = c if j == 2 else 0.0 transformed_axis = self.orientation * axis if transformed_axis.z > best_z: best_z = transformed_axis.z best_axis = axis return best_axis
def __init__(self, position): self.position = position self.animation_transform = None self.face_list = [] if position.x == -1.0: self.face_list.append( LatchCubieFace('blue', Vector(-1, 0, 0), 'ccw' if position.y == 0.0 else None)) if position.x == 1.0: self.face_list.append( LatchCubieFace('green', Vector(1, 0, 0), 'cw' if position.z == 0.0 else None)) if position.y == -1.0: self.face_list.append( LatchCubieFace('yellow', Vector(0, -1, 0), 'ccw' if position.x == 0.0 else None)) if position.y == 1.0: self.face_list.append( LatchCubieFace('white', Vector(0, 1, 0), 'cw' if position.z == 0.0 else None)) if position.z == -1.0: self.face_list.append( LatchCubieFace('red', Vector(0, 0, -1), 'ccw' if position.x == 0.0 else None)) if position.z == 1.0: self.face_list.append( LatchCubieFace('orange', Vector(0, 0, 1), 'cw' if position.y == 0.0 else None))
def RenderCubieQuads(self, cubie): center = cubie.position quad_vertices = [ Vector(-0.45, -0.45, 0.0), Vector(0.45, -0.45, 0.0), Vector(0.45, 0.45, 0.0), Vector(-0.45, 0.45, 0.0) ] for face in cubie.face_list: normal = face.normal if cubie.animation_transform: normal = cubie.animation_transform * normal normal = self.orientation * normal if normal.z < 0: continue self.RenderColor(face.color) frame = LinearTransform() frame.MakeFrame(face.normal) for vertex in quad_vertices: vertex = frame * vertex + center + face.normal * 0.5 if cubie.animation_transform: vertex = cubie.animation_transform * vertex vertex = self.orientation * vertex GL.glVertex3f(vertex.x, vertex.y, vertex.z)
def RotateFace(self, axis, direction): if direction == 'ccw': angle = math.pi / 2.0 elif direction == 'cw': angle = -math.pi / 2.0 else: return False # This matrix's inverse is its transpose, so it # should work on positions and normals. transform = LinearTransform() transform.MakeRotation(axis, angle) # This is what makes the Latch Cube so hard!!! cubie_list = self.CollectCubiesForAxis(axis) # TODO: Uncomment this code when ready. #for cubie in cubie_list: # face = cubie.FindFaceWithNormal( axis ) # if face.constraint_direction and face.constraint_direction != direction: # return False normal_list = [] normal_list.append(Vector(-1, 0, 0)) normal_list.append(Vector(1, 0, 0)) normal_list.append(Vector(0, -1, 0)) normal_list.append(Vector(0, 1, 0)) normal_list.append(Vector(0, 0, -1)) normal_list.append(Vector(0, 0, 1)) # Compute the face transitions. for cubie in cubie_list: position = transform * cubie.position target_cubie = self.CubieAtPosition(position) for normal in normal_list: face = cubie.FindFaceWithNormal(normal) if face: target_normal = transform * normal target_normal.RoundToNearestIntegerComponents() target_face = target_cubie.FindFaceWithNormal( target_normal) target_face.new_face = face.Clone() target_face.new_face.normal = target_normal # Apply the face transitions. for cubie in cubie_list: for i in range(len(cubie.face_list)): face = cubie.face_list[i] if face.new_face: cubie.face_list[i] = face.new_face return True
def mouseMoveEvent(self, event): if self.dragging: pos = event.pos() dragVector = Vector() dragVector.x = float(pos.x() - self.dragPos.x()) dragVector.y = float(pos.y() - self.dragPos.y()) self.dragPos = pos scale = 0.01 xAngle = scale * dragVector.y yAngle = scale * dragVector.x xAxisRotation = LinearTransform() yAxisRotation = LinearTransform() xAxisRotation.MakeRotation(Vector(1.0, 0.0, 0.0), xAngle) yAxisRotation.MakeRotation(Vector(0.0, 1.0, 0.0), yAngle) self.orientation = xAxisRotation * self.orientation self.orientation = yAxisRotation * self.orientation self.orientation.Orthonormalize( ) # Do this just to deal with accumulated round-off error. self.update()
from VectorMath import Vector va = Vector((3.0, 4.0, 0.0)) vb = Vector((9.0, 2.1, 5.2)) print va.components, " + ", vb.components vc = va + vb print vc.components print va.components, " - ", vb.components vc = va - vb print vc.components print va.components, " dot ", vb.components vc = va.dot(vb) print vc print va.components, " x ", vb.components vc = va.cross(vb) print vc.components print va.components, " ang ", vb.components vc = va.angle(vb) print vc print va.components, " length " vc = va.length() print vc print vb.components, " length " vc = vb.length() print vc print va.components, " norm " vc = va.norm() print vc.components print vb.components, " norm " vc = vb.norm()
def __init__(self): self.cubie_matrix = [[[ LatchCubie(Vector(x - 1, y - 1, z - 1)) for z in range(3) ] for y in range(3)] for x in range(3)]