Guest User

panda3d -- 3d maze

a guest
May 15th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.51 KB | None | 0 0
  1. #
  2. #   @author: Nino Cocchiarella
  3. #
  4.  
  5. import random, sys, math
  6. from visual import *
  7. from direct.directbase import DirectStart
  8. from direct.showbase.DirectObject import DirectObject
  9. from direct.gui.DirectGui import *
  10. from direct.interval.IntervalGlobal import *
  11. from panda3d.core import lookAt
  12. from panda3d.core import GeomVertexFormat, GeomVertexData
  13. from panda3d.core import Geom, GeomTriangles, GeomVertexWriter
  14. from panda3d.core import Texture, GeomNode
  15. from panda3d.core import PerspectiveLens
  16. from panda3d.core import CardMaker
  17. from panda3d.core import Light, Spotlight
  18. from panda3d.core import TextNode
  19. from panda3d.core import Vec3, Vec4, Point3
  20. import sys, os
  21.  
  22. icount = 0
  23.  
  24.  
  25. WALLDEPTH = 1
  26. DZ = WALLDEPTH / 2.
  27.  
  28. base.disableMouse()
  29. base.camera.setPos(0,0,0)
  30. base.camera.lookAt(0,0,1)
  31.  
  32. sys.setrecursionlimit(10000)
  33.  
  34. WIDTH = 5
  35. HEIGHT = 5
  36. DEPTH = 5
  37. BOXSIZE = 100
  38. STEPSIZE = 1
  39.  
  40. UP          =   vector(0,STEPSIZE,0)
  41. DOWN        =   vector(0,-STEPSIZE,0)
  42. RIGHT       =   vector(STEPSIZE,0,0)
  43. LEFT        =   vector(-STEPSIZE,0,0)
  44. FORWARD     =   vector(0,BOXSIZE,0)
  45. BACKWARDS   =   vector(0,-BOXSIZE,0)
  46.  
  47. # you cant normalize in-place so this is a helper function
  48. def myNormalize(myVec):
  49.     myVec.normalize()
  50.     return myVec
  51.  
  52. # helper function to make a square given the Lower-Left-Hand and Upper-Right-Hand corners
  53. def makeBox(x1, y1, z1, x2, y2, z2):
  54.     format=GeomVertexFormat.getV3n3cpt2()
  55.     vdata=GeomVertexData('square', format, Geom.UHDynamic)
  56.  
  57.     vertex=GeomVertexWriter(vdata, 'vertex')
  58.     normal=GeomVertexWriter(vdata, 'normal')
  59.     color=GeomVertexWriter(vdata, 'color')
  60.     texcoord=GeomVertexWriter(vdata, 'texcoord')
  61.    
  62.     # make sure we draw the sqaure in the right plane
  63.     if x1!=x2:
  64.         vertex.addData3f(x1, y1, z1)
  65.         vertex.addData3f(x2, y1, z1)
  66.         vertex.addData3f(x2, y2, z2)
  67.         vertex.addData3f(x1, y2, z2)
  68.  
  69.         normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
  70.         normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y1-1, 2*z1-1)))
  71.         normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
  72.         normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y2-1, 2*z2-1)))
  73.        
  74.     else:
  75.         vertex.addData3f(x1, y1, z1)
  76.         vertex.addData3f(x2, y2, z1)
  77.         vertex.addData3f(x2, y2, z2)
  78.         vertex.addData3f(x1, y1, z2)
  79.  
  80.         normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
  81.         normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z1-1)))
  82.         normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
  83.         normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z2-1)))
  84.  
  85.     #adding different colors to the vertex for visibility
  86.     color.addData4f(1.0, 0.0, 0.0, .3)
  87.     #color.addData4f(0.0, 1.0, 0.0, .7)
  88.     #color.addData4f(0.0, 0.0, 1.0, .7)
  89.     #color.addData4f(1.0, 0.0, 1.0, .7)
  90.  
  91.     texcoord.addData2f(0.0, 0.4)
  92.     #texcoord.addData2f(0.0, 0.0)
  93.     #texcoord.addData2f(1.0, 0.0)
  94.     #texcoord.addData2f(1.0, 1.0)
  95.  
  96.     # quads arent directly supported by the Geom interface
  97.     # you might be interested in the CardMaker class if you are
  98.     # interested in rectangle though
  99.     tri1=GeomTriangles(Geom.UHDynamic)
  100.     tri2=GeomTriangles(Geom.UHDynamic)
  101.  
  102.     tri1.addVertex(0)
  103.     tri1.addVertex(1)
  104.     tri1.addVertex(3)
  105.  
  106.     tri2.addConsecutiveVertices(1, 3)
  107.  
  108.     tri1.closePrimitive()
  109.     tri2.closePrimitive()
  110.  
  111.  
  112.     square=Geom(vdata)
  113.     square.addPrimitive(tri1)
  114.     square.addPrimitive(tri2)
  115.    
  116.     return square
  117.  
  118.  
  119. class Cuboid(DirectObject):
  120.  
  121.     # takes the x, y, z coordinates of two OPPOSITE corners, where x, y, and z are all UNIQUE
  122.     def __init__(self, x1, y1, z1, x2, y2, z2):
  123.  
  124.         xx = x1, x2
  125.         yy = y1, y2
  126.         zz = z1, z2
  127.  
  128.         #print x1, y1, z1, x2, y2, z2
  129.         global icount
  130.         icount += 1
  131.  
  132.         #if (x1 == x2 or y1 == y2 or z1 == z2):
  133.         #   print x1, y1, z2, x2, y2, z2
  134.         #   raise Exception('Invalid box coordinates')
  135.  
  136.         square0=makeBox(min(xx), min(yy), min(zz), max(xx), min(yy), max(zz)) # bottom
  137.         square1=makeBox(min(xx), max(yy), min(zz), max(xx), max(yy), max(zz)) # top
  138.         square2=makeBox(min(xx), max(yy), max(zz), max(xx), min(yy), max(zz)) # back
  139.         square3=makeBox(min(xx), max(yy), min(zz), max(xx), min(yy), min(zz)) # front
  140.         square4=makeBox(min(xx), min(yy), min(zz), min(xx), max(yy), max(zz)) # left
  141.         square5=makeBox(max(xx), min(yy), min(zz), max(xx), max(yy), max(zz)) # right
  142.         snode=GeomNode('box')
  143.         snode.addGeom(square0)
  144.         snode.addGeom(square1)
  145.         snode.addGeom(square2)
  146.         snode.addGeom(square3)
  147.         snode.addGeom(square4)
  148.         snode.addGeom(square5)
  149.  
  150.         box=render.attachNewNode(snode)
  151.         box.setTwoSided(True)
  152.        
  153.         self.LightsOn=False
  154.         self.LightsOn1=False
  155.  
  156.         self.slnp  = render.attachNewNode(slight)
  157.         self.slnp1 = render.attachNewNode(slight)
  158.  
  159. class Cell:
  160.     def __init__(self, x, y, z, N, E, S, W, U, D):
  161.         if N:
  162.             rn = Cuboid(x-DZ, y-DZ, z+BOXSIZE-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
  163.         if E:
  164.             re = Cuboid(x+BOXSIZE-DZ, y-DZ, z-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
  165.         if S:
  166.             rs = Cuboid(x-DZ, y-DZ, z-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+DZ)
  167.         if W:
  168.             rw = Cuboid(x-DZ, y-DZ, z-DZ, x+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
  169.         if D:
  170.             ru = Cuboid(x-DZ, y-DZ, z-DZ, x+BOXSIZE+DZ, y+DZ, z+BOXSIZE+DZ)
  171.         if U:
  172.             ru = Cuboid(x-DZ, y+BOXSIZE-DZ, z-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
  173.  
  174. class Maze:
  175.  
  176.     def __init__(self):
  177.         self.maze = [[[[1,1,1,1,1,1] for x in range(WIDTH)] for y in range(HEIGHT)] for z in range(DEPTH)]
  178.         self.walls = []
  179.         self.cellsVisited = []
  180.         self.generate()
  181.         self.render()
  182.  
  183.     def generate(self):
  184.        
  185.         # pick a random cell to start
  186.         rx, ry, rz =    random.randint(0, WIDTH-1), \
  187.                         random.randint(0, HEIGHT-1), \
  188.                         random.randint(0, DEPTH-1)
  189.  
  190.         self.start = (rx, ry, rz)
  191.  
  192.         def findPath(cx,cy,cz):
  193.             neighbor = self.getNeighbor(cx,cy,cz)
  194.             self.cellsVisited.append((cx,cy,cz))
  195.  
  196.             while neighbor in self.cellsVisited or neighbor == None:
  197.                 for n in self.cellsVisited:
  198.                     neighbor = self.getNeighbor(n[0], n[1], n[2])
  199.                     if neighbor != None:
  200.                         break
  201.                 if neighbor == None:
  202.                     return
  203.  
  204.             if neighbor == None:
  205.                 return
  206.  
  207.             # first, figure out which neighbor was returned
  208.             d = self.getNeighborDirection((cx,cy,cz), neighbor)
  209.             nx,ny,nz = neighbor
  210.  
  211.             # knock down wall between current cell and current neighbor
  212.             if d == UP:
  213.                 self.maze[cz][cy][cx][0] = 0
  214.                 self.maze[nz][ny][nx][2] = 0
  215.             elif d == RIGHT:
  216.                 self.maze[cz][cy][cx][1] = 0
  217.                 self.maze[nz][ny][nx][3] = 0
  218.             elif d == DOWN:
  219.                 self.maze[cz][cy][cx][2] = 0
  220.                 self.maze[nz][ny][nx][0] = 0
  221.             elif d == LEFT:
  222.                 self.maze[cz][cy][cx][3] = 0
  223.                 self.maze[nz][ny][nx][1] = 0
  224.             elif d == FORWARD:
  225.                 self.maze[cz][cy][cx][4] = 0
  226.                 self.maze[nz][ny][nx][5] = 0
  227.             elif d == BACKWARDS:
  228.                 self.maze[cz][cy][cx][5] = 0
  229.                 self.maze[nz][ny][nx][4] = 0
  230.  
  231.             findPath(nx,ny,nz)
  232.  
  233.         scene.center = rx,ry,rz
  234.         findPath(rx,ry,rz)
  235.  
  236.  
  237.     def getNeighborDirection(self, xyz, nxyz):
  238.         x,y,z = xyz
  239.         nx,ny,nz = nxyz
  240.         if ny >  y and x == nx and z == nz:
  241.             return UP
  242.         if nx > x and y == ny and nz == nz:
  243.             return RIGHT
  244.         if ny < y and x == nx and nz == nz:
  245.             return DOWN
  246.         if nx < x and y == ny and z == nz:
  247.             return LEFT
  248.         if z > nz and y == ny and z == nz:
  249.             return FORWARD
  250.         if z < nz and y == ny and x == nx:
  251.             return BACKWARDS
  252.  
  253.     def getNeighbor(self,x,y,z):
  254.         # generate a list of tuples for all neighbors
  255.         # eliminate cells that are not on grid
  256.         # eliminate visited cells
  257.         n = []
  258.        
  259.         # UP
  260.         if y < (HEIGHT - 1) and not (x, y+1, z) in self.cellsVisited:
  261.             n.append((x, y+1, z))
  262.         # RIGHT
  263.         if x < (WIDTH - 1) and not (x+1, y, z) in self.cellsVisited:
  264.             n.append((x+1, y, z))
  265.         # DOWN
  266.         if y >= (HEIGHT - 1) and not (x, y-1, z) in self.cellsVisited:
  267.             n.append((x, y-1, z))
  268.         # LEFT
  269.         if x >= 1 and not (x-1, y, z) in self.cellsVisited:
  270.             n.append((x-1, y, z))
  271.         # FORWARD
  272.         if z < (DEPTH - 1) and not (x, y, z+1) in self.cellsVisited:
  273.             n.append((x, y, z+1))
  274.         # BACKWARDS
  275.         if z >= 1 and not (x, y, z-1) in self.cellsVisited:
  276.             n.append((x, y, z-1))
  277.  
  278.         # then randomize order of neighbors
  279.         random.shuffle(n)
  280.  
  281.         # get first cell from neighbors
  282.         for nn in n:
  283.             if not (nn[0], nn[1], nn[2]) in self.cellsVisited:
  284.                 return nn
  285.  
  286.         return None
  287.  
  288.     def render(self):
  289.         dx = dy = dz = 0
  290.         for z in self.maze:
  291.             dy = 0
  292.             for y in z:
  293.                 dx = 0
  294.                 for x in y:
  295.                     cell = self.maze[dz][dy][dx]
  296.                     cx,cy,cz = dx*BOXSIZE, dy*BOXSIZE, dz*BOXSIZE
  297.                    
  298.                     # paint planes N,E,S,W,T,B
  299.                     N = E = S = W = F = B = False
  300.  
  301.                     if cell[4]:
  302.                         N = True
  303.                     if cell[1]:
  304.                          E = True
  305.                     if cell[5]:
  306.                          S = True
  307.                     if cell[3]:
  308.                          W = True
  309.                     if cell[0]:
  310.                          F = True
  311.                     if cell[2]:
  312.                          B = True
  313.  
  314.                     cell = Cell(cx, cy, cz, N, E, S, W, F, B)
  315.  
  316.                     dx += 1
  317.                 dy += 1
  318.             dz += 1
  319.  
  320.     def rotateY(self, dir):
  321.         lastpos.x += 5
  322.         rotation = cross((lastpos-camera),norm(scene.forward))
  323.         if dir > 0:
  324.             rf = 1
  325.         else:
  326.             rf = -1
  327.         scene.forward = scene.forward.rotate(angle=rf*-rotation.y/100, axis=(0,1,0))
  328.         scene.center = camera+R*norm(scene.forward)
  329.  
  330.     def rotateX(self, dir):
  331.         lastpos.y += 5
  332.         rotation = cross((lastpos-camera),norm(scene.forward))
  333.         if dir > 0:
  334.             rf = 1
  335.         else:
  336.             rf = -1
  337.         scene.forward = scene.forward.rotate(angle=rf*-rotation.x/100, axis=(1,0,0))
  338.         scene.center = camera+R*norm(scene.forward)
  339.  
  340.     def move(self, (dx, dy, dz)):
  341.         scene.center = scene.center.x + dx, scene.center.y + dy, scene.center.z + dz
  342.         print scene.center
  343.        
  344.  
  345. movingUp = False
  346. movingRight = False
  347. movingDown = False
  348. movingLeft = False
  349. movingForward = False
  350. movingBackwards = False
  351.  
  352. slight = Spotlight('slight')
  353. slight.setColor(Vec4(1, 1, 1, 1))
  354. lens = PerspectiveLens()
  355. slight.setLens(lens)
  356.  
  357. board = Maze()
  358. run()
Advertisement
Add Comment
Please, Sign In to add comment