Advertisement
H3LLB0Y

Z-axis codes!

Apr 30th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. from direct.showbase.ShowBase import ShowBase
  2. from pandac.PandaModules import *
  3. from math import sin, cos, pi
  4.  
  5. def move_forwards(rotation, multi):
  6.     # Calculate Change
  7.     x_movement=-sin(rotation*(pi/180))*multi
  8.     z_movement=cos(rotation*(pi/180))*multi
  9.    
  10.     return Vec3(x_movement,0,z_movement)
  11.  
  12. # World Class
  13. class World():
  14.     def __init__(self,showbase,num_sides,radius):      
  15.         # generate n gon for platform
  16.         n_gon=self.create_world(num_sides,radius)
  17.         self.world=render.attachNewNode(n_gon)
  18.        
  19.         # this part sets up the texturing
  20.         self.proj = render.attachNewNode(LensNode('proj'))
  21.         self.lens = OrthographicLens()
  22.         self.lens.setFilmSize(250) # not sure exactly what this was :S
  23.         self.proj.node().setLens(self.lens)
  24.         self.proj.reparentTo(render)
  25.         # sets projection to be straight down from above the circle
  26.         self.proj.setPos(0,1,0)
  27.         self.proj.lookAt(0,0,0)
  28.  
  29.         # your texture
  30.         self.tex = loader.loadTexture('ground.png')
  31.         self.ts = TextureStage('ts')
  32.         self.world.clearTexture()
  33.         # apply it!
  34.         self.world.projectTexture(self.ts, self.tex, self.proj)
  35.    
  36.     def create_world(self,n,radius):
  37.         points={}
  38.        
  39.         # rotation between each warlock for even spacing
  40.         rotation=360.0/n
  41.        
  42.         for i in range(n):
  43.             # adjust rotation for this point
  44.             rot=rotation*(i+1)
  45.            
  46.             # set its position radius units from center
  47.             points[i]=move_forwards(rot,radius)
  48.            
  49.         nnode=GeomNode('n_gon')
  50.         for i in range(n):
  51.             triangle=self.create_triangle(points[i],points[(i+1)%n])
  52.             nnode.addGeom(triangle)
  53.            
  54.         return nnode
  55.        
  56.     def create_triangle(self,point1,point2):
  57.         format=GeomVertexFormat.getV3n3cpt2()
  58.         vdata=GeomVertexData('triangle', format, Geom.UHStatic)
  59.  
  60.         vertex=GeomVertexWriter(vdata, 'vertex')
  61.         normal=GeomVertexWriter(vdata, 'normal')
  62.         color=GeomVertexWriter(vdata, 'color')
  63.         texcoord=GeomVertexWriter(vdata, 'texcoord')
  64.        
  65.         # first point
  66.         vertex.addData3f(point1.getX(), 0, point1.getZ())
  67.         normal.addData3f(0, -1, 0)
  68.         color.addData4f(1, 1, 1, 1)
  69.         texcoord.addData2f(1, 0)
  70.  
  71.         # second point
  72.         vertex.addData3f(point2.getX(), 0, point2.getZ())
  73.         normal.addData3f(0, -1, 0)
  74.         color.addData4f(1, 1, 1, 1)
  75.         texcoord.addData2f(1, 1)
  76.  
  77.         # center point (0,0,0)
  78.         vertex.addData3f(0, 0, 0)
  79.         normal.addData3f(0, 0, 1)
  80.         color.addData4f(1, 1, 1, 1)
  81.         texcoord.addData2f(0, 1)
  82.        
  83.         tri1=GeomTriangles(Geom.UHStatic)
  84.  
  85.         tri1.addVertex(0)
  86.         tri1.addVertex(1)
  87.         tri1.addVertex(2)
  88.        
  89.         tri1.closePrimitive()
  90.        
  91.         triangle=Geom(vdata)
  92.         triangle.addPrimitive(tri1)
  93.        
  94.         return triangle
  95.  
  96. class Main(ShowBase):
  97.     def __init__(self):
  98.         ShowBase.__init__(self)
  99.         self.world=World(self,160,25)
  100.  
  101. game = Main()
  102. game.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement