python - Pygame 3D Cube translate,move -
i have make follow 3d cube , need make translate function move cube example key move axis x, cube y move in axis y,and right in axis z, search find translate function it's 2d here mind stack , confuse.
now want follow please, how can anjust translate function code, or have idea how move,transate cube vertices3 of code below?
please help
import sys, math, pygame class point3d: def __init__(self, x = 0, y = 0, z = 0): self.x, self.y, self.z = float(x), float(y), float(z) def rotatex(self, angle): """ rotates point around x axis given angle in degrees. """ rad = angle * math.pi / 180 cosa = math.cos(rad) sina = math.sin(rad) y = self.y * cosa - self.z * sina z = self.y * sina + self.z * cosa return point3d(self.x, y, z) def rotatey(self, angle): """ rotates point around y axis given angle in degrees. """ rad = angle * math.pi / 180 cosa = math.cos(rad) sina = math.sin(rad) z = self.z * cosa - self.x * sina x = self.z * sina + self.x * cosa return point3d(x, self.y, z) def rotatez(self, angle): """ rotates point around z axis given angle in degrees. """ rad = angle * math.pi / 180 cosa = math.cos(rad) sina = math.sin(rad) x = self.x * cosa - self.y * sina y = self.x * sina + self.y * cosa return point3d(x, y, self.z) def project(self, win_width, win_height, fov, viewer_distance): """ transforms 3d point 2d using perspective projection. """ factor = fov / (viewer_distance + self.z) x = self.x * factor + win_width / 2 y = -self.y * factor + win_height / 2 return point3d(x, y, 1) class simulation: def __init__(self, win_width = 640, win_height = 480): pygame.init() self.screen = pygame.display.set_mode((win_width, win_height)) pygame.display.set_caption("3d wireframe cube simulation (http://codentronix.com)") self.clock = pygame.time.clock() self.vertices = [ point3d(-1,-1,-1), point3d(-1,1,-1), point3d(1,1,-1), point3d(1,-1,-1), point3d(-1,1,1), point3d(1,1,1), point3d(1,-1,1), point3d(-1,-1,1) ] self.vertices2 = [ point3d(-1,-1,-1), point3d(-1,0,-1), point3d(0,0,-1), point3d(0,-1,-1), point3d(-1,0,0), point3d(0,0,0), point3d(0,-1,0), point3d(-1,-1,0) ] self.vertices3 = [ point3d(0,-1,-1), point3d(0,0,-1), point3d(1,0,-1), point3d(1,-1,-1), point3d(0,0,0), point3d(1,0,0), point3d(1,-1,0), point3d(0,-1,0) ] # define vertices compose each of 6 faces. these numbers # indices vertices list defined above. self.faces = [(0,1,2,3),(0,1,4,7),(4,5,6,7),(7,6,3,0),(5,6,3,2)] self.faces2 = [(0,1,2,3),(0,1,4,7),(4,5,6,7),(7,6,3,0),(5,6,3,2)] self.anglex, self.angley, self.anglez = 0, 0, 0 def run(self): """ main loop """ while 1: event in pygame.event.get(): if event.type == pygame.quit: sys.exit() self.clock.tick(50) self.screen.fill((0,0,0)) # hold transformed vertices. t = [] t1 = [] v in self.vertices: # rotate point around x axis, around y axis, , around z axis. r = v.rotatex(self.anglex).rotatey(self.angley).rotatez(self.anglez) # transform point 3d 2d p = r.project(self.screen.get_width(), self.screen.get_height(), 256, 4) # put point in list of transformed vertices t.append(p) x, y = int(p.x), int(p.y) self.screen.fill((255,0,0),(x,y,2,2)) v1 in self.vertices2: # rotate point around x axis, around y axis, , around z axis. r1 = v1.rotatex(self.anglex).rotatey(self.angley).rotatez(self.anglez) # transform point 3d 2d p1 = r1.project(self.screen.get_width(), self.screen.get_height(), 256, 4) # put point in list of transformed vertices t1.append(p1) x, y = int(p1.x), int(p1.y) self.screen.fill((255,0,0),(x,y,3,3)) f in self.faces: pygame.draw.line(self.screen, (255,255,255), (t[f[0]].x, t[f[0]].y), (t[f[1]].x, t[f[1]].y)) pygame.draw.line(self.screen, (255,255,255), (t[f[1]].x, t[f[1]].y), (t[f[2]].x, t[f[2]].y)) pygame.draw.line(self.screen, (255,255,255), (t[f[2]].x, t[f[2]].y), (t[f[3]].x, t[f[3]].y)) pygame.draw.line(self.screen, (255,255,255), (t[f[3]].x, t[f[3]].y), (t[f[0]].x, t[f[0]].y)) f1 in self.faces2: pygame.draw.line(self.screen, (255,255,255), (t1[f1[0]].x, t1[f1[0]].y), (t1[f1[1]].x, t1[f1[1]].y)) pygame.draw.line(self.screen, (255,255,255), (t1[f1[1]].x, t1[f1[1]].y), (t1[f1[2]].x, t1[f1[2]].y)) pygame.draw.line(self.screen, (255,255,255), (t1[f1[2]].x, t1[f1[2]].y), (t1[f1[3]].x, t1[f1[3]].y)) pygame.draw.line(self.screen, (255,255,255), (t1[f1[3]].x, t1[f1[3]].y), (t1[f1[0]].x, t1[f1[0]].y)) self.anglex += 1 self.angley += 1 self.anglez += 1 pygame.display.flip() if __name__ == "__main__": simulation().run()
i find answer question, , more, follow can move,rotate , scale cube can find code
save page .py in same folder, run displaywireframe3 , that´s .if want know control keys check code in displaywireframe3 guys.
Comments
Post a Comment