Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # @author: Nino Cocchiarella
- #
- import random, sys, math
- from visual import *
- from direct.directbase import DirectStart
- from direct.showbase.DirectObject import DirectObject
- from direct.gui.DirectGui import *
- from direct.interval.IntervalGlobal import *
- from panda3d.core import lookAt
- from panda3d.core import GeomVertexFormat, GeomVertexData
- from panda3d.core import Geom, GeomTriangles, GeomVertexWriter
- from panda3d.core import Texture, GeomNode
- from panda3d.core import PerspectiveLens
- from panda3d.core import CardMaker
- from panda3d.core import Light, Spotlight
- from panda3d.core import TextNode
- from panda3d.core import Vec3, Vec4, Point3
- import sys, os
- icount = 0
- WALLDEPTH = 1
- DZ = WALLDEPTH / 2.
- base.disableMouse()
- base.camera.setPos(0,0,0)
- base.camera.lookAt(0,0,1)
- sys.setrecursionlimit(10000)
- WIDTH = 5
- HEIGHT = 5
- DEPTH = 5
- BOXSIZE = 100
- STEPSIZE = 1
- UP = vector(0,STEPSIZE,0)
- DOWN = vector(0,-STEPSIZE,0)
- RIGHT = vector(STEPSIZE,0,0)
- LEFT = vector(-STEPSIZE,0,0)
- FORWARD = vector(0,BOXSIZE,0)
- BACKWARDS = vector(0,-BOXSIZE,0)
- # you cant normalize in-place so this is a helper function
- def myNormalize(myVec):
- myVec.normalize()
- return myVec
- # helper function to make a square given the Lower-Left-Hand and Upper-Right-Hand corners
- def makeBox(x1, y1, z1, x2, y2, z2):
- format=GeomVertexFormat.getV3n3cpt2()
- vdata=GeomVertexData('square', format, Geom.UHDynamic)
- vertex=GeomVertexWriter(vdata, 'vertex')
- normal=GeomVertexWriter(vdata, 'normal')
- color=GeomVertexWriter(vdata, 'color')
- texcoord=GeomVertexWriter(vdata, 'texcoord')
- # make sure we draw the sqaure in the right plane
- if x1!=x2:
- vertex.addData3f(x1, y1, z1)
- vertex.addData3f(x2, y1, z1)
- vertex.addData3f(x2, y2, z2)
- vertex.addData3f(x1, y2, z2)
- normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
- normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y1-1, 2*z1-1)))
- normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
- normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y2-1, 2*z2-1)))
- else:
- vertex.addData3f(x1, y1, z1)
- vertex.addData3f(x2, y2, z1)
- vertex.addData3f(x2, y2, z2)
- vertex.addData3f(x1, y1, z2)
- normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
- normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z1-1)))
- normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
- normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z2-1)))
- #adding different colors to the vertex for visibility
- color.addData4f(1.0, 0.0, 0.0, .3)
- #color.addData4f(0.0, 1.0, 0.0, .7)
- #color.addData4f(0.0, 0.0, 1.0, .7)
- #color.addData4f(1.0, 0.0, 1.0, .7)
- texcoord.addData2f(0.0, 0.4)
- #texcoord.addData2f(0.0, 0.0)
- #texcoord.addData2f(1.0, 0.0)
- #texcoord.addData2f(1.0, 1.0)
- # quads arent directly supported by the Geom interface
- # you might be interested in the CardMaker class if you are
- # interested in rectangle though
- tri1=GeomTriangles(Geom.UHDynamic)
- tri2=GeomTriangles(Geom.UHDynamic)
- tri1.addVertex(0)
- tri1.addVertex(1)
- tri1.addVertex(3)
- tri2.addConsecutiveVertices(1, 3)
- tri1.closePrimitive()
- tri2.closePrimitive()
- square=Geom(vdata)
- square.addPrimitive(tri1)
- square.addPrimitive(tri2)
- return square
- class Cuboid(DirectObject):
- # takes the x, y, z coordinates of two OPPOSITE corners, where x, y, and z are all UNIQUE
- def __init__(self, x1, y1, z1, x2, y2, z2):
- xx = x1, x2
- yy = y1, y2
- zz = z1, z2
- #print x1, y1, z1, x2, y2, z2
- global icount
- icount += 1
- #if (x1 == x2 or y1 == y2 or z1 == z2):
- # print x1, y1, z2, x2, y2, z2
- # raise Exception('Invalid box coordinates')
- square0=makeBox(min(xx), min(yy), min(zz), max(xx), min(yy), max(zz)) # bottom
- square1=makeBox(min(xx), max(yy), min(zz), max(xx), max(yy), max(zz)) # top
- square2=makeBox(min(xx), max(yy), max(zz), max(xx), min(yy), max(zz)) # back
- square3=makeBox(min(xx), max(yy), min(zz), max(xx), min(yy), min(zz)) # front
- square4=makeBox(min(xx), min(yy), min(zz), min(xx), max(yy), max(zz)) # left
- square5=makeBox(max(xx), min(yy), min(zz), max(xx), max(yy), max(zz)) # right
- snode=GeomNode('box')
- snode.addGeom(square0)
- snode.addGeom(square1)
- snode.addGeom(square2)
- snode.addGeom(square3)
- snode.addGeom(square4)
- snode.addGeom(square5)
- box=render.attachNewNode(snode)
- box.setTwoSided(True)
- self.LightsOn=False
- self.LightsOn1=False
- self.slnp = render.attachNewNode(slight)
- self.slnp1 = render.attachNewNode(slight)
- class Cell:
- def __init__(self, x, y, z, N, E, S, W, U, D):
- if N:
- rn = Cuboid(x-DZ, y-DZ, z+BOXSIZE-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
- if E:
- re = Cuboid(x+BOXSIZE-DZ, y-DZ, z-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
- if S:
- rs = Cuboid(x-DZ, y-DZ, z-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+DZ)
- if W:
- rw = Cuboid(x-DZ, y-DZ, z-DZ, x+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
- if D:
- ru = Cuboid(x-DZ, y-DZ, z-DZ, x+BOXSIZE+DZ, y+DZ, z+BOXSIZE+DZ)
- if U:
- ru = Cuboid(x-DZ, y+BOXSIZE-DZ, z-DZ, x+BOXSIZE+DZ, y+BOXSIZE+DZ, z+BOXSIZE+DZ)
- class Maze:
- def __init__(self):
- self.maze = [[[[1,1,1,1,1,1] for x in range(WIDTH)] for y in range(HEIGHT)] for z in range(DEPTH)]
- self.walls = []
- self.cellsVisited = []
- self.generate()
- self.render()
- def generate(self):
- # pick a random cell to start
- rx, ry, rz = random.randint(0, WIDTH-1), \
- random.randint(0, HEIGHT-1), \
- random.randint(0, DEPTH-1)
- self.start = (rx, ry, rz)
- def findPath(cx,cy,cz):
- neighbor = self.getNeighbor(cx,cy,cz)
- self.cellsVisited.append((cx,cy,cz))
- while neighbor in self.cellsVisited or neighbor == None:
- for n in self.cellsVisited:
- neighbor = self.getNeighbor(n[0], n[1], n[2])
- if neighbor != None:
- break
- if neighbor == None:
- return
- if neighbor == None:
- return
- # first, figure out which neighbor was returned
- d = self.getNeighborDirection((cx,cy,cz), neighbor)
- nx,ny,nz = neighbor
- # knock down wall between current cell and current neighbor
- if d == UP:
- self.maze[cz][cy][cx][0] = 0
- self.maze[nz][ny][nx][2] = 0
- elif d == RIGHT:
- self.maze[cz][cy][cx][1] = 0
- self.maze[nz][ny][nx][3] = 0
- elif d == DOWN:
- self.maze[cz][cy][cx][2] = 0
- self.maze[nz][ny][nx][0] = 0
- elif d == LEFT:
- self.maze[cz][cy][cx][3] = 0
- self.maze[nz][ny][nx][1] = 0
- elif d == FORWARD:
- self.maze[cz][cy][cx][4] = 0
- self.maze[nz][ny][nx][5] = 0
- elif d == BACKWARDS:
- self.maze[cz][cy][cx][5] = 0
- self.maze[nz][ny][nx][4] = 0
- findPath(nx,ny,nz)
- scene.center = rx,ry,rz
- findPath(rx,ry,rz)
- def getNeighborDirection(self, xyz, nxyz):
- x,y,z = xyz
- nx,ny,nz = nxyz
- if ny > y and x == nx and z == nz:
- return UP
- if nx > x and y == ny and nz == nz:
- return RIGHT
- if ny < y and x == nx and nz == nz:
- return DOWN
- if nx < x and y == ny and z == nz:
- return LEFT
- if z > nz and y == ny and z == nz:
- return FORWARD
- if z < nz and y == ny and x == nx:
- return BACKWARDS
- def getNeighbor(self,x,y,z):
- # generate a list of tuples for all neighbors
- # eliminate cells that are not on grid
- # eliminate visited cells
- n = []
- # UP
- if y < (HEIGHT - 1) and not (x, y+1, z) in self.cellsVisited:
- n.append((x, y+1, z))
- # RIGHT
- if x < (WIDTH - 1) and not (x+1, y, z) in self.cellsVisited:
- n.append((x+1, y, z))
- # DOWN
- if y >= (HEIGHT - 1) and not (x, y-1, z) in self.cellsVisited:
- n.append((x, y-1, z))
- # LEFT
- if x >= 1 and not (x-1, y, z) in self.cellsVisited:
- n.append((x-1, y, z))
- # FORWARD
- if z < (DEPTH - 1) and not (x, y, z+1) in self.cellsVisited:
- n.append((x, y, z+1))
- # BACKWARDS
- if z >= 1 and not (x, y, z-1) in self.cellsVisited:
- n.append((x, y, z-1))
- # then randomize order of neighbors
- random.shuffle(n)
- # get first cell from neighbors
- for nn in n:
- if not (nn[0], nn[1], nn[2]) in self.cellsVisited:
- return nn
- return None
- def render(self):
- dx = dy = dz = 0
- for z in self.maze:
- dy = 0
- for y in z:
- dx = 0
- for x in y:
- cell = self.maze[dz][dy][dx]
- cx,cy,cz = dx*BOXSIZE, dy*BOXSIZE, dz*BOXSIZE
- # paint planes N,E,S,W,T,B
- N = E = S = W = F = B = False
- if cell[4]:
- N = True
- if cell[1]:
- E = True
- if cell[5]:
- S = True
- if cell[3]:
- W = True
- if cell[0]:
- F = True
- if cell[2]:
- B = True
- cell = Cell(cx, cy, cz, N, E, S, W, F, B)
- dx += 1
- dy += 1
- dz += 1
- def rotateY(self, dir):
- lastpos.x += 5
- rotation = cross((lastpos-camera),norm(scene.forward))
- if dir > 0:
- rf = 1
- else:
- rf = -1
- scene.forward = scene.forward.rotate(angle=rf*-rotation.y/100, axis=(0,1,0))
- scene.center = camera+R*norm(scene.forward)
- def rotateX(self, dir):
- lastpos.y += 5
- rotation = cross((lastpos-camera),norm(scene.forward))
- if dir > 0:
- rf = 1
- else:
- rf = -1
- scene.forward = scene.forward.rotate(angle=rf*-rotation.x/100, axis=(1,0,0))
- scene.center = camera+R*norm(scene.forward)
- def move(self, (dx, dy, dz)):
- scene.center = scene.center.x + dx, scene.center.y + dy, scene.center.z + dz
- print scene.center
- movingUp = False
- movingRight = False
- movingDown = False
- movingLeft = False
- movingForward = False
- movingBackwards = False
- slight = Spotlight('slight')
- slight.setColor(Vec4(1, 1, 1, 1))
- lens = PerspectiveLens()
- slight.setLens(lens)
- board = Maze()
- run()
Advertisement
Add Comment
Please, Sign In to add comment