def check_collision(self): # make the snake move in the next cube self.snake.move() # have to make the bbox bigger by one unit so that the program can # use the intersect function ext_bbox = copy.deepcopy(self.bbox) ext_bbox.v_list[0] = self.bbox.v_list[0] - vec.V4(1, 1, 1, 1) ext_bbox.v_list[1] = self.bbox.v_list[1] + vec.V4(1, 1, 1, 1) # if is outside of the bbox if not self.intersect(self.snake.head_pos, ext_bbox): return self.collision_outbbox # if the snake got food if self.intersect(self.snake.head_pos, self.food): return self.collision_food # if the snake crosses itself for p in self.snake.p_list[:-1]: if self.intersect(self.snake.head_pos, p): return self.collision_snake # else no collisions return self.collision_none
def __init__(self): # position of the head self.head_pos = vec.V4(0, 0, 0, 0) # direction self.head_dir = "UP" #initial size init_size = 4 # polygon list of the snake self.p_list = [] for i in range(init_size): next_cube_center = self.head_pos - vec.V4( (init_size - 1) - i, 0, 0, 0) next_cube = self.create_cube(next_cube_center) self.p_list.append(next_cube) # generate the directions vectors and forbbidden directions possible_dirs = [ "UP", "DOWN", "LEFT", "RIGHT", "FW", "RW", "IN", "OUT" ] self.dir_v = {} self.opposite_dir = {} for i, direction in enumerate(possible_dirs): # generate direction vectors v = [0, 0, 0, 0] v[int(i / 2)] = 1 if i % 2 == 0 else -1 self.dir_v[direction] = vec.V4(v) # generate forbidden directions (ex. UP -> cant go DOWN) self.opposite_dir[direction] = possible_dirs[i + (1 if i % 2 == 0 else -1)]
def interpret_bytes(self, bf): # read the color self.color = bf.read_string() # read vertex list v_list = [] v_len = bf.read("I") for i in range(v_len): v4 = vec.V4(0, 0, 0, 0) v4.interpret_bytes(bf) v_list.append(v4) self.v_list = v_list # read edge list earrlen = bf.read("I") # read in the linear array containg the values earr = [] for i in range(earrlen): e = bf.read("I") earr.append(e) # the array is composed of a series of couples (0 1)(2 3) # which indicates which vertex are connected half_earrlen = int(earrlen / 2) # initialize the edge list with 0s self.e_list = [[0,0] for i in range(half_earrlen)] # assign those values to the initialized edge list for i in range(half_earrlen): self.e_list[i][0] = earr[i*2] self.e_list[i][1] = earr[i*2 + 1] # read face list farrlen = bf.read("I") # read the linear array containgin the vertex indexes that compose a # face farr = [] for i in range(farrlen): f = bf.read("I") farr.append(f) # the array is composed of a series of couples (0 1 2 3)(5 6 7 8) # which indicates which vertex form a face half_farrlen = int(farrlen / 4) # initialize the list self.f_list = [[0, 0, 0, 0] for i in range(half_farrlen)] # assign the values read to the face list for i in range(half_farrlen): self.f_list[i][0] = farr[i*4] self.f_list[i][1] = farr[i*4 + 1] self.f_list[i][2] = farr[i*4 + 2] self.f_list[i][3] = farr[i*4 + 3]
def rot_v(v, alpha, beta, gamma, delta, rho, epsilon): # XY Plane rot matrix mxy = [ [ math.cos(alpha), math.sin(alpha), 0, 0], [ -math.sin(alpha), math.cos(alpha), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ] mxy = mat.SquareMatrix(mxy) # YZ Plane rot matrix myz = [ [ 1, 0, 0, 0], [ 0, math.cos(beta), math.sin(beta), 0], [ 0, -math.sin(beta), math.cos(beta), 0], [ 0, 0, 0, 1] ] myz = mat.SquareMatrix(myz) # ZX Plane rot matrix mzx = [ [ math.cos(gamma), 0, -math.sin(gamma), 0], [ 0, 1, 0, 0], [ math.sin(gamma), 0, math.cos(gamma), 0], [ 0, 0, 0, 1] ] mzx = mat.SquareMatrix(mzx) # XW Plane rot matrix mxw = [ [ math.cos(delta), 0, 0, math.sin(delta)], [ 0, 1, 0, 0], [ 0, 0, 1, 0], [ -math.sin(delta), 0, 0, math.cos(delta)] ] mxw = mat.SquareMatrix(mxw) # YW Plane rot matrix myw = [ [ 1, 0, 0, 0], [ 0, math.cos(rho), 0, -math.sin(rho)], [ 0, 0, 1, 0], [ 0, math.sin(rho), 0, math.cos(rho)] ] myw = mat.SquareMatrix(myw) # ZW Plane rot matrix mzw = [ [ 1, 0, 0, 0], [ 0, 1, 0, 0], [ 0, 0, math.cos(epsilon), -math.sin(epsilon)], [ 0, 0, math.sin(epsilon), math.cos(epsilon)] ] mzw = mat.SquareMatrix(mzw) # multiply them mres = mxy * myz * mzx * mxw * myw * mzw mv = mat.Matrix(v.coords) # creates a 1 column matrix r = mres * mv return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def create_cube4d( point, size, color): v = vec.V4(1, 1, 1, 1) * size/2.0 higher = point + v lower = point - v c = cube4d(lower,higher) c.color = color return c
def rot_beta(v, beta): myz = [ [ 1, 0, 0, 0], [ 0, math.cos(beta), math.sin(beta), 0], [ 0, -math.sin(beta), math.cos(beta), 0], [ 0, 0, 0, 1] ] myz = mat.SquareMatrix(myz) r = myz * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_epsilon(v, epsilon): mzw = [ [ 1, 0, 0, 0], [ 0, 1, 0, 0], [ 0, 0, math.cos(epsilon), -math.sin(epsilon)], [ 0, 0, math.sin(epsilon), math.cos(epsilon)] ] mzw = mat.SquareMatrix(mzw) r = mzw * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_rho(v, rho): myw = [ [ 1, 0, 0, 0], [ 0, math.cos(rho), 0, -math.sin(rho)], [ 0, 0, 1, 0], [ 0, math.sin(rho), 0, math.cos(rho)] ] myw = mat.SquareMatrix(myw) r = myw * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_delta(v, delta): mxw = [ [ math.cos(delta), 0, 0, math.sin(delta)], [ 0, 1, 0, 0], [ 0, 0, 1, 0], [ -math.sin(delta), 0, 0, math.cos(delta)] ] mxw = mat.SquareMatrix(mxw) r = mxw * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_gamma(v, gamma): mzx = [ [ math.cos(gamma), 0, -math.sin(gamma), 0], [ 0, 1, 0, 0], [ math.sin(gamma), 0, math.cos(gamma), 0], [ 0, 0, 0, 1] ] mzx = mat.SquareMatrix(mzx) r = mzx * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_alpha(v, alpha): mxy = [ [ math.cos(alpha), math.sin(alpha), 0, 0], [ -math.sin(alpha), math.cos(alpha), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ] mxy = mat.SquareMatrix(mxy) r = mxy * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def __init__(self): self.From = vec.V4(10, 0, 0, 0) self.To = vec.V4(0,0,0,0) self.Up = vec.V4(0,1,0,0) self.Over = vec.V4(0,0,1,0) self.t_matrix = self.calc_tmatrix() # prjection screen cube center self.Bx = 0 self.By = 0 self.Bz = 0 # projection screen cube dimensions self.Lx = 1.5 self.Ly = 1.5 self.Lz = 1.5 # 4d view angle (set at 60 deg by default) self.view_angle = math.pi/3
def prj(self, point): '''project the a 4d point into a 3d cube screen''' point = point - self.From if point == vec.V4(0, 0, 0, 0): raise CamExcept("Cam4: prj point null vec") T = 1/(math.tan(self.view_angle)/2) S = T / (point.dot(self.t_matrix[3])) x = self.Bx + (self.Lx * S * point.dot(self.t_matrix[0])) y = self.By + (self.Ly * S * point.dot(self.t_matrix[1])) z = self.Bz + (self.Lz * S * point.dot(self.t_matrix[2])) return vec.V3(x,y,z)
def __init__(self): # 3d cam self.cam3 = prj.Cam3() self.cam3.change_position(vec.V3(2.5, 2.5, 2.5)) # 4d cam self.cam4 = prj.Cam4() self.cam4.change_position(vec.V4(15, 0.1, 0.1, 0.1)) # construct the index to axis mapping for the 3d-rotations self.idx_to_axis = {} for i in range(3): axis = [0 for k in range(3)] axis[i] = 1 self.idx_to_axis[i] = vec.V3(axis)
def interpret_bytes(self, bf): # read the color self.color = bf.read_string() # read vertex list v_list = [] # read len v_len = bf.read("I") for i in range(v_len): v4 = vec.V4(0, 0, 0, 0) v4.interpret_bytes(bf) v_list.append(v4) self.v_list = v_list # read edge list earrlen = bf.read("I") half_earrlen = int(earrlen / 2) self.e_list = [[0, 0] for i in range(half_earrlen)] earr = [] for i in range(earrlen): e = bf.read("I") earr.append(e) for i in range(half_earrlen): self.e_list[i][0] = earr[i * 2] self.e_list[i][1] = earr[i * 2 + 1] # read face list farrlen = bf.read("I") half_farrlen = int(farrlen / 4) self.f_list = [[0, 0, 0, 0] for i in range(half_farrlen)] farr = [] for i in range(farrlen): f = bf.read("I") farr.append(f) for i in range(half_farrlen): self.f_list[i][0] = farr[i * 4] self.f_list[i][1] = farr[i * 4 + 1] self.f_list[i][2] = farr[i * 4 + 2] self.f_list[i][3] = farr[i * 4 + 3]
def __init__(self): # creates a snake, which is positioned in the center and with 4 # segments in the -x direction self.snake = snake.Snake() # create the bounding box, the variable is used to test border # collisions self.bbox_size = 3 self.bbox = poly.create_cube4d(vec.V4(0, 0, 0, 0), self.bbox_size * 2, "black") # add a food hypercube self.food = self.initialize_food() # game state self.state = self.gamestate_run # score (sum of the cubes) self.score = 0 # the list of polygons to be drawn self.p_list = [] self.generate_plist()
x = self.Bx + (self.Lx * S * point.dot(self.t_matrix[0])) y = self.By + (self.Ly * S * point.dot(self.t_matrix[1])) z = self.Bz + (self.Lz * S * point.dot(self.t_matrix[2])) return vec.V3(x,y,z) def change_target(self, t): self.To = t self.t_matrix = self.calc_tmatrix() def change_position(self, p): self.From = p self.t_matrix = self.calc_tmatrix() if __name__ == "__main__": cam = Cam4() p = vec.V4(1, 1, 1, 1) p_proj = cam.prj(p) print(p_proj) cam = Cam3() p_proj = cam.prj(p_proj) print(p_proj)
def generate_rand_point(): coords = [] for i in range(4): coords.append( random.randrange(-self.bbox_size + 1, self.bbox_size - 1)) return vec.V4(coords)
def cube4d(v0,v1): polygon = Polygon() polygon.v_list.append(v0) #v0 polygon.v_list.append(v1) #v1 polygon.v_list.append(vec.V4(v1[0],v0[1],v0[2],v0[3])) #v2 polygon.v_list.append(vec.V4(v1[0],v1[1],v0[2],v0[3])) #v3 polygon.v_list.append(vec.V4(v0[0],v1[1],v0[2],v0[3])) #v4 polygon.v_list.append(vec.V4(v0[0],v0[1],v1[2],v0[3])) #v5 polygon.v_list.append(vec.V4(v1[0],v0[1],v1[2],v0[3])) #v6 polygon.v_list.append(vec.V4(v1[0],v1[1],v1[2],v0[3])) #v7 polygon.v_list.append(vec.V4(v0[0],v1[1],v1[2],v0[3])) #v8 polygon.v_list.append(vec.V4(v0[0],v0[1],v0[2],v1[3])) #v9 polygon.v_list.append(vec.V4(v1[0],v0[1],v0[2],v1[3])) #v10 polygon.v_list.append(vec.V4(v1[0],v1[1],v0[2],v1[3])) #v11 polygon.v_list.append(vec.V4(v0[0],v1[1],v0[2],v1[3])) #v12 polygon.v_list.append(vec.V4(v0[0],v0[1],v1[2],v1[3])) #v13 polygon.v_list.append(vec.V4(v1[0],v0[1],v1[2],v1[3])) #v14 polygon.v_list.append(vec.V4(v0[0],v1[1],v1[2],v1[3])) #v15 polygon.e_list.append((0,2)) polygon.e_list.append((0,9)) polygon.e_list.append((2,3)) polygon.e_list.append((2,10)) polygon.e_list.append((3,4)) polygon.e_list.append((3,11)) polygon.e_list.append((4,0)) polygon.e_list.append((4,12)) polygon.e_list.append((0,5)) polygon.e_list.append((2,6)) polygon.e_list.append((4,8)) polygon.e_list.append((3,7)) polygon.e_list.append((5,6)) polygon.e_list.append((5,13)) polygon.e_list.append((6,7)) polygon.e_list.append((6,14)) polygon.e_list.append((7,8)) polygon.e_list.append((7,1)) polygon.e_list.append((8,5)) polygon.e_list.append((8,15)) polygon.e_list.append((9,10)) polygon.e_list.append((10,11)) polygon.e_list.append((11,12)) polygon.e_list.append((12,9)) polygon.e_list.append((10,14)) polygon.e_list.append((11,1)) polygon.e_list.append((12,15)) polygon.e_list.append((9,13)) polygon.e_list.append((13,14)) polygon.e_list.append((14,1)) polygon.e_list.append((1,15)) polygon.e_list.append((15,13)) polygon.f_list.append((1,14,6,7)) polygon.f_list.append((6,2,3,7)) polygon.f_list.append((2,10,11,3)) polygon.f_list.append((10,14,1,11)) polygon.f_list.append((7,6,5,8)) polygon.f_list.append((3,2,0,4)) polygon.f_list.append((11,10,9,12)) polygon.f_list.append((1,14,13,15)) polygon.f_list.append((14,6,5,13)) polygon.f_list.append((6,2,0,5)) polygon.f_list.append((2,10,9,0)) polygon.f_list.append((10,14,13,9)) polygon.f_list.append((1,15,8,7)) polygon.f_list.append((8,4,3,7)) polygon.f_list.append((4,12,11,3)) polygon.f_list.append((12,15,1,11)) polygon.f_list.append((1,11,3,7)) polygon.f_list.append((14,6,2,10)) polygon.f_list.append((15,8,4,12)) polygon.f_list.append((13,5,0,9)) polygon.f_list.append((15,13,5,8)) polygon.f_list.append((5,8,4,0)) polygon.f_list.append((0,4,12,9)) polygon.f_list.append((13,9,12,15)) return polygon
polygon.f_list.append((13,5,0,9)) polygon.f_list.append((15,13,5,8)) polygon.f_list.append((5,8,4,0)) polygon.f_list.append((0,4,12,9)) polygon.f_list.append((13,9,12,15)) return polygon #============================================================================== # Generate an hypercube given a center and a size #============================================================================== def create_cube4d( point, size, color): v = vec.V4(1, 1, 1, 1) * size/2.0 higher = point + v lower = point - v c = cube4d(lower,higher) c.color = color return c if __name__ == "__main__": c4 = cube4d(vec.V4(-1, -1, -1, -1), vec.V4(1, 1, 1, 1)) c4.write_file("./test_poly.poly") c4.read_file4("./test_poly.poly")