Posted By

ozkriff on 08/04/08


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

theonlyalterego
hemanthhm


=(


 / Published in: Python
 

  1. import direct.directbase.DirectStart
  2. from pandac.PandaModules import loadPrcFileData
  3. from pandac.PandaModules import *
  4. from pandac.PandaModules import CollisionTraverser,CollisionNode
  5. from pandac.PandaModules import CollisionHandlerQueue,CollisionRay
  6. from pandac.PandaModules import Filename,GeomNode
  7. from pandac.PandaModules import PandaNode,NodePath,Camera,TextNode
  8. from pandac.PandaModules import Vec3,Vec4,BitMask32
  9. from direct.gui.OnscreenText import OnscreenText
  10. from direct.actor.Actor import Actor
  11. from direct.task.Task import Task
  12. from direct.showbase.DirectObject import DirectObject
  13. from pandac.PandaModules import GeoMipTerrain
  14. import random, sys, os, math
  15.  
  16.  
  17. loadPrcFileData("", "interpolate-frames 1")
  18.  
  19.  
  20. # Figure out what directory this program is in.
  21. MYDIR=os.path.abspath(sys.path[0])
  22. MYDIR=Filename.fromOsSpecific(MYDIR).getFullpath()
  23.  
  24. # Function to put instructions on the screen.
  25. def addInstructions(pos, msg):
  26. return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
  27. pos=(-1.3, pos), align=TextNode.ALeft, scale = .05,mayChange=True)
  28.  
  29. # Function to put title on the screen.
  30. def addTitle(text):
  31. return OnscreenText(text=text, style=1, fg=(1,1,1,1),
  32. pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)
  33.  
  34. class World(DirectObject):
  35.  
  36. def __init__(self):
  37.  
  38. #-3,5,0 =)
  39.  
  40. self.terrain = GeoMipTerrain("terrain")
  41. self.terrain.setHeightfield(Filename("models/heightmap2.bmp"))
  42. self.terrain.getRoot().setSz(15)
  43. self.terrain.getRoot().reparentTo(render)
  44. self.terrain.generate()
  45. self.terrain.getRoot().setPos(0,0,10)
  46.  
  47. base.win.setClearColor(Vec4(0,0,0,1))
  48.  
  49. self.picker = CollisionTraverser()
  50. self.queue = CollisionHandlerQueue()
  51.  
  52. # Post the instructions
  53. self.title = addTitle("Panda3D Tutorial: Roaming Ralph (Walking on Uneven Terrain)")
  54. self.inst1 = addInstructions(0.95, "[ESC]: Quit")
  55. self.inst2 = addInstructions(0.90, "[Left Arrow]: Rotate Ralph Left")
  56. self.inst3 = addInstructions(0.85, "[Right Arrow]: Rotate Ralph Right")
  57. self.inst4 = addInstructions(0.80, "[Up Arrow]: Run Ralph Forward")
  58. self.inst5 = addInstructions(0.75, "[Down Arrow]: Home")
  59. self.inst10 = addInstructions(0.60, "!!!")
  60. self.inst11 = addInstructions(0.50, "...")
  61. self.inst30 = addInstructions(0.10, " ")
  62.  
  63. self.loadPicker()
  64.  
  65. # Create the main character, Ralph
  66.  
  67. ralphStartPos = (3,5)
  68.  
  69. self.ralph = Actor("models/ralph",
  70. {"run":"models/ralph-run",
  71. "walk":"models/ralph-walk"})
  72. self.ralph.reparentTo(render)
  73. self.ralph.setScale(.5)
  74.  
  75.  
  76. self.ralph.setTag('pickable', 'R')
  77.  
  78. # Create a floater object. We use the "floater" as a temporary
  79. # variable in a variety of calculations.
  80. self.floater = NodePath(PandaNode("floater"))
  81. self.floater.reparentTo(render)
  82.  
  83. # Accept the control keys for movement and rotation
  84. self.accept("escape", sys.exit)
  85. self.accept("shift-mouse1", self.collisionCheck,[1])
  86. self.accept("shift-mouse3", self.collisionCheck,[2])
  87. self.accept("shift-mouse2", self.collisionCheck,[3])
  88.  
  89. taskMgr.add(self.move,"moveTask")
  90. taskMgr.add(self.ZCord,"moveTask")
  91.  
  92. # Set up the camera
  93.  
  94. #~ base.disableMouse()
  95. base.camera.setPos(0,0,20)
  96.  
  97. # Game state variables
  98. self.prevtime = 0
  99. self.isMoving = False
  100.  
  101.  
  102. #load s light=========================
  103.  
  104. plight = PointLight('plight')
  105. plight.setColor(VBase4(1, 1.3, 1.2, 1))
  106. plnp = render.attachNewNode(plight)
  107. plnp.setPos(0, 0, 30)
  108. render.setLight(plnp)
  109.  
  110. #~ =============================
  111.  
  112.  
  113.  
  114. #Records the state of the arrow keys
  115. def setKey(self, key, value):
  116. self.keyMap[key] = value
  117.  
  118. #teleport ralph
  119. def teleport(self, pos):
  120. self.ralph.setPos(pos)
  121.  
  122. #the same
  123. def move2(self, pos):
  124. self.ralph.setPos(pos)
  125.  
  126. def loadPicker(self):
  127. #attach a CollisionRay node to the camera
  128. self.pickerNode = CollisionNode('mouseRay')
  129. self.pickerNP = camera.attachNewNode(self.pickerNode)
  130. self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
  131. self.pickerRay = CollisionRay()
  132. self.pickerNode.addSolid(self.pickerRay)
  133.  
  134. self.pickerNP.show()
  135.  
  136. #add collision node (fromObject) to the traverser
  137. self.picker.addCollider(self.pickerNP, self.queue)
  138. self.picker.showCollisions(render)
  139.  
  140. if base.mouseWatcherNode.hasMouse():
  141. #get the mouse position
  142. mpos = base.mouseWatcherNode.getMouse()
  143. #aim the collision ray
  144. self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
  145.  
  146.  
  147.  
  148. self.inst30.destroy()
  149. self.inst30 = addInstructions(-0.50, str(mpos.getX())+" "+str(mpos.getY()))
  150. #fire the ray and record, in the queue, any collisions
  151. self.picker.traverse(render)
  152.  
  153. if self.queue.getNumEntries() > 0:
  154. if self.queue.getNumEntries() == 1:
  155. self.mytext = "A collision with: _ " \
  156. + str(self.queue.getEntry(0).getIntoNodePath()) +" at point of: _ " \
  157. + str(self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath()))
  158. else:
  159. #organize the collisions from first hit to second...
  160. self.queue.sortEntries()
  161. #output the name of the node paths the ray collided with
  162. self.mytext = "A collision occured first with " \
  163. + str(self.queue.getEntry(0).getIntoNodePath()) \
  164. + " and then with " + str(self.queue.getEntry(1).getIntoNodePath())
  165.  
  166.  
  167. self.onCollisionText.setText(self.mytext)
  168.  
  169.  
  170. def collisionCheck(self,but):
  171. if base.mouseWatcherNode.hasMouse():
  172. #get the mouse position
  173. mpos = base.mouseWatcherNode.getMouse()
  174. #aim the collision ray
  175. self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
  176. self.inst30.destroy()
  177. self.inst30 = addInstructions(-0.50, str(mpos.getX())+" "+str(mpos.getY()))
  178. #fire the ray and record, in the queue, any collisions
  179. self.picker.traverse(render)
  180.  
  181. if self.queue.getNumEntries() > 0:
  182. if but ==1:
  183. #self.teleport(self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath()))
  184. self.moveTo = self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath())
  185. self.ralph.loop("run")
  186.  
  187. if but ==2:
  188. self.move2(self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath()))
  189.  
  190.  
  191. def move(self, task):
  192.  
  193. #time saving
  194. elapsed = task.time - self.prevtime
  195.  
  196. # save ralph's initial position so that we can restore it,
  197. # in case he falls off the map or runs into something.
  198.  
  199. startpos = self.ralph.getPos()
  200.  
  201. #mose moving =) thanks InternZero
  202. if hasattr(self, "moveTo"):
  203. self.ralph.lookAt(self.moveTo)
  204. self.ralph.setH(self.ralph, 180)
  205. backward = self.ralph.getNetTransform().getMat().getRow3(1)
  206. #~ backward.setZ(0)
  207. backward.normalize()
  208. self.ralph.setPos(self.ralph.getPos() - backward*(elapsed*5))
  209. #stop when reach the destination point
  210. if self.ralph.getPos(render).almostEqual(self.moveTo, 0.05):
  211. #~ print "Comparison:", self.ralph.getPos(render), self.moveTo
  212. self.ralph.setPos(self.moveTo)
  213. self.isMoving = False
  214. self.ralph.pose("walk",5)
  215. backward = self.ralph.getNetTransform().getMat().getRow3(1)
  216. #~ backward.setZ(0)
  217. backward.normalize()
  218. del self.moveTo
  219.  
  220.  
  221. self.inst10.destroy()
  222. self.inst10 = addInstructions(0.55, str(self.ralph.getPos)+"(ralph position):")
  223.  
  224. self.inst11.destroy()
  225. self.inst11 = addInstructions(0.50, str(self.ralph.getPos()))
  226.  
  227. # Store the task time and continue.
  228. self.prevtime = task.time
  229. return Task.cont
  230.  
  231. def ZCord(self, task):
  232. # Adjust ralph's Z coordinate.
  233. z=self.terrain.getElevation(self.ralph.getX(),self.ralph.getY())*self.terrain.getRoot().getSz()
  234. self.ralph.setPos(self.ralph.getX(),self.ralph.getY(),z)
  235.  
  236.  
  237.  
  238. #suka rabotay!!!
  239. w = World()
  240. run()

Report this snippet  

You need to login to post a comment.