Guest User

Untitled

a guest
Oct 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. from direct.directbase import DirectStart
  2. from pandac.PandaModules import *
  3. from random import randint, random
  4.  
  5. # Setup our physics world
  6. world = OdeWorld()
  7. world.setGravity(0, 0, -9.81)
  8.  
  9. # The surface table is needed for autoCollide
  10. world.initSurfaceTable(1)
  11. world.setSurfaceEntry(0, 0, 150, 0.0, 9.1, 0.9, 0.00001, 0.0, 0.002)
  12.  
  13. # Create a space and add a contactgroup to it to add the contact joints
  14. space = OdeSimpleSpace()
  15. space.setAutoCollideWorld(world)
  16. contactgroup = OdeJointGroup()
  17. space.setAutoCollideJointGroup(contactgroup)
  18.  
  19. # Load the box
  20. box = loader.loadModel("box")
  21. # Make sure its center is at 0, 0, 0 like OdeBoxGeom
  22. box.setPos(-.5, -.5, -.5)
  23. box.flattenLight() # Apply transform
  24. box.setTextureOff()
  25.  
  26. # Add a random amount of boxes
  27. boxes = []
  28. for i in range(randint(15, 30)):
  29. # Setup the geometry
  30. boxNP = box.copyTo(render)
  31. boxNP.setPos(randint(-10, 10), randint(-10, 10), 10 + random())
  32. boxNP.setColor(random(), random(), random(), 1)
  33. boxNP.setHpr(randint(-45, 45), randint(-45, 45), randint(-45, 45))
  34. # Create the body and set the mass
  35. boxBody = OdeBody(world)
  36. M = OdeMass()
  37. M.setBox(50, 1, 1, 1)
  38. boxBody.setMass(M)
  39. boxBody.setPosition(boxNP.getPos(render))
  40. boxBody.setQuaternion(boxNP.getQuat(render))
  41. # Create a BoxGeom
  42. boxGeom = OdeBoxGeom(space, 1, 1, 1)
  43. boxGeom.setBody(boxBody)
  44. boxes.append((boxNP, boxBody))
  45.  
  46. # Add a plane to collide with
  47. ground = loader.loadModel("map/polinomialcarnage.egg")
  48. ground.setPos(0, 0, 0);
  49. ground.reparentTo(render)
  50.  
  51. groundGeomData = OdeTriMeshData(ground, True)
  52. groundGeom = OdeTriMeshGeom(space, groundGeomData)
  53.  
  54. # The task for our simulation
  55. def simulationTask(task):
  56. space.autoCollide() # Setup the contact joints
  57.  
  58. # Step the simulation and set the new positions
  59. world.quickStep(globalClock.getDt())
  60. for np, body in boxes:
  61. np.setPosQuat(render, body.getPosition(), Quat(body.getQuaternion()))
  62. contactgroup.empty() # Clear the contact joints
  63. return task.cont
  64.  
  65. # Wait a split second, then start the simulation
  66. taskMgr.doMethodLater(3, simulationTask, "Physics Simulation")
  67.  
  68. run()
Add Comment
Please, Sign In to add comment