step5_complete_solar_system.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  2. # Author: Shao Zhang and Phil Saltzman
  3. # Last Updated: 2015-03-13
  4. #
  5. # This tutorial is intended as a initial panda scripting lesson going over
  6. # display initialization, loading models, placing objects, and the scene graph.
  7. #
  8. # Step 5: Here we put the finishing touches on our solar system model by
  9. # making the planets move. The actual code for doing the movement is covered
  10. # in the next tutorial, but watching it move really shows what inheritance on
  11. # the scene graph is all about.
  12. from direct.showbase.ShowBase import ShowBase
  13. base = ShowBase()
  14. from direct.gui.DirectGui import *
  15. from panda3d.core import TextNode
  16. import sys
  17. class World(object):
  18. def __init__(self):
  19. # This is the initialization we had before
  20. self.title = OnscreenText( # Create the title
  21. text="Panda3D: Tutorial 1 - Solar System",
  22. parent=base.a2dBottomRight, align=TextNode.A_right,
  23. style=1, fg=(1, 1, 1, 1), pos=(-0.1, 0.1), scale=.07)
  24. base.setBackgroundColor(0, 0, 0) # Set the background to black
  25. base.disableMouse() # disable mouse control of the camera
  26. camera.setPos(0, 0, 45) # Set the camera position (X, Y, Z)
  27. camera.setHpr(0, -90, 0) # Set the camera orientation
  28. #(heading, pitch, roll) in degrees
  29. # Here again is where we put our global variables. Added this time are
  30. # variables to control the relative speeds of spinning and orbits in the
  31. # simulation
  32. # Number of seconds a full rotation of Earth around the sun should take
  33. self.yearscale = 60
  34. # Number of seconds a day rotation of Earth should take.
  35. # It is scaled from its correct value for easier visability
  36. self.dayscale = self.yearscale / 365.0 * 5
  37. self.orbitscale = 10 # Orbit scale
  38. self.sizescale = 0.6 # Planet size scale
  39. self.loadPlanets() # Load and position the models
  40. # Finally, we call the rotatePlanets function which puts the planets,
  41. # sun, and moon into motion.
  42. self.rotatePlanets()
  43. def loadPlanets(self):
  44. # This is the same function that we completed in the previous step
  45. # It is unchanged in this version
  46. # Create the dummy nodes
  47. self.orbit_root_mercury = render.attachNewNode('orbit_root_mercury')
  48. self.orbit_root_venus = render.attachNewNode('orbit_root_venus')
  49. self.orbit_root_mars = render.attachNewNode('orbit_root_mars')
  50. self.orbit_root_earth = render.attachNewNode('orbit_root_earth')
  51. # The moon orbits Earth, not the sun
  52. self.orbit_root_moon = (
  53. self.orbit_root_earth.attachNewNode('orbit_root_moon'))
  54. ###############################################################
  55. # Load the sky
  56. self.sky = loader.loadModel("models/solar_sky_sphere")
  57. self.sky_tex = loader.loadTexture("models/stars_1k_tex.jpg")
  58. self.sky.setTexture(self.sky_tex, 1)
  59. self.sky.reparentTo(render)
  60. self.sky.setScale(40)
  61. # Load the Sun
  62. self.sun = loader.loadModel("models/planet_sphere")
  63. self.sun_tex = loader.loadTexture("models/sun_1k_tex.jpg")
  64. self.sun.setTexture(self.sun_tex, 1)
  65. self.sun.reparentTo(render)
  66. self.sun.setScale(2 * self.sizescale)
  67. # Load mercury
  68. self.mercury = loader.loadModel("models/planet_sphere")
  69. self.mercury_tex = loader.loadTexture("models/mercury_1k_tex.jpg")
  70. self.mercury.setTexture(self.mercury_tex, 1)
  71. self.mercury.reparentTo(self.orbit_root_mercury)
  72. self.mercury.setPos(0.38 * self.orbitscale, 0, 0)
  73. self.mercury.setScale(0.385 * self.sizescale)
  74. # Load Venus
  75. self.venus = loader.loadModel("models/planet_sphere")
  76. self.venus_tex = loader.loadTexture("models/venus_1k_tex.jpg")
  77. self.venus.setTexture(self.venus_tex, 1)
  78. self.venus.reparentTo(self.orbit_root_venus)
  79. self.venus.setPos(0.72 * self.orbitscale, 0, 0)
  80. self.venus.setScale(0.923 * self.sizescale)
  81. # Load Mars
  82. self.mars = loader.loadModel("models/planet_sphere")
  83. self.mars_tex = loader.loadTexture("models/mars_1k_tex.jpg")
  84. self.mars.setTexture(self.mars_tex, 1)
  85. self.mars.reparentTo(self.orbit_root_mars)
  86. self.mars.setPos(1.52 * self.orbitscale, 0, 0)
  87. self.mars.setScale(0.515 * self.sizescale)
  88. # Load Earth
  89. self.earth = loader.loadModel("models/planet_sphere")
  90. self.earth_tex = loader.loadTexture("models/earth_1k_tex.jpg")
  91. self.earth.setTexture(self.earth_tex, 1)
  92. self.earth.reparentTo(self.orbit_root_earth)
  93. self.earth.setScale(self.sizescale)
  94. self.earth.setPos(self.orbitscale, 0, 0)
  95. # Offest the moon dummy node so that it is positioned properly
  96. self.orbit_root_moon.setPos(self.orbitscale, 0, 0)
  97. # Load the moon
  98. self.moon = loader.loadModel("models/planet_sphere")
  99. self.moon_tex = loader.loadTexture("models/moon_1k_tex.jpg")
  100. self.moon.setTexture(self.moon_tex, 1)
  101. self.moon.reparentTo(self.orbit_root_moon)
  102. self.moon.setScale(0.1 * self.sizescale)
  103. self.moon.setPos(0.1 * self.orbitscale, 0, 0)
  104. # end loadPlanets()
  105. def rotatePlanets(self):
  106. # rotatePlanets creates intervals to actually use the hierarchy we created
  107. # to turn the sun, planets, and moon to give a rough representation of the
  108. # solar system. The next lesson will go into more depth on intervals.
  109. self.day_period_sun = self.sun.hprInterval(20, (360, 0, 0))
  110. self.orbit_period_mercury = self.orbit_root_mercury.hprInterval(
  111. (0.241 * self.yearscale), (360, 0, 0))
  112. self.day_period_mercury = self.mercury.hprInterval(
  113. (59 * self.dayscale), (360, 0, 0))
  114. self.orbit_period_venus = self.orbit_root_venus.hprInterval(
  115. (0.615 * self.yearscale), (360, 0, 0))
  116. self.day_period_venus = self.venus.hprInterval(
  117. (243 * self.dayscale), (360, 0, 0))
  118. self.orbit_period_earth = self.orbit_root_earth.hprInterval(
  119. self.yearscale, (360, 0, 0))
  120. self.day_period_earth = self.earth.hprInterval(
  121. self.dayscale, (360, 0, 0))
  122. self.orbit_period_moon = self.orbit_root_moon.hprInterval(
  123. (.0749 * self.yearscale), (360, 0, 0))
  124. self.day_period_moon = self.moon.hprInterval(
  125. (.0749 * self.yearscale), (360, 0, 0))
  126. self.orbit_period_mars = self.orbit_root_mars.hprInterval(
  127. (1.881 * self.yearscale), (360, 0, 0))
  128. self.day_period_mars = self.mars.hprInterval(
  129. (1.03 * self.dayscale), (360, 0, 0))
  130. self.day_period_sun.loop()
  131. self.orbit_period_mercury.loop()
  132. self.day_period_mercury.loop()
  133. self.orbit_period_venus.loop()
  134. self.day_period_venus.loop()
  135. self.orbit_period_earth.loop()
  136. self.day_period_earth.loop()
  137. self.orbit_period_moon.loop()
  138. self.day_period_moon.loop()
  139. self.orbit_period_mars.loop()
  140. self.day_period_mars.loop()
  141. # end RotatePlanets()
  142. # end class world
  143. w = World()
  144. base.run()