conftest.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import pytest
  2. @pytest.fixture
  3. def world():
  4. bullet = pytest.importorskip("panda3d.bullet")
  5. world = bullet.BulletWorld()
  6. return world
  7. def simulate_until(world, cond, dt=1.0/60, limit=600):
  8. for x in range(limit):
  9. world.do_physics(dt)
  10. if cond():
  11. return True
  12. return False
  13. @pytest.fixture
  14. def scene(world):
  15. """
  16. This test scene contains a "ramp" which slopes down at a 45-degree angle at
  17. locations where X<0, and is flat at X>0. A bowling ball is placed above the
  18. ramp at X=-5, Z=7. A stack of 2 very light boxes is placed at X=5, Z=0.
  19. The world is given gravity.
  20. When running the Bullet simulation, the ball should fall, travel down the
  21. ramp, and hit the lower box with enough force to displace it and cause the
  22. upper box to topple to the ground.
  23. """
  24. core = pytest.importorskip("panda3d.core")
  25. bullet = pytest.importorskip("panda3d.bullet")
  26. bodies = []
  27. ramp = bullet.BulletRigidBodyNode('ramp')
  28. ramp.add_shape(bullet.BulletPlaneShape(core.Vec4(0, 0, 1, 0)))
  29. ramp.add_shape(bullet.BulletPlaneShape(core.Vec4(1, 0, 1, 0).normalized()))
  30. bodies.append(ramp)
  31. ball = bullet.BulletRigidBodyNode('ball')
  32. ball.add_shape(bullet.BulletSphereShape(1))
  33. ball.set_mass(100)
  34. ball.set_transform(core.TransformState.make_pos(core.Point3(-5, 0, 7)))
  35. bodies.append(ball)
  36. lower_box = bullet.BulletRigidBodyNode('lower_box')
  37. lower_box.add_shape(bullet.BulletBoxShape(core.Vec3(2.5)))
  38. lower_box.set_mass(1)
  39. lower_box.set_transform(core.TransformState.make_pos(core.Point3(5, 0, 2.5)))
  40. bodies.append(lower_box)
  41. upper_box = bullet.BulletRigidBodyNode('upper_box')
  42. upper_box.add_shape(bullet.BulletBoxShape(core.Vec3(2.5)))
  43. upper_box.set_mass(1)
  44. upper_box.set_transform(core.TransformState.make_pos(core.Point3(5, 0, 7.5)))
  45. bodies.append(upper_box)
  46. world.set_gravity(core.Vec3(0, 0, -9.8))
  47. scene = core.NodePath('scene')
  48. for body in bodies:
  49. scene.attach_new_node(body)
  50. world.attach(body)
  51. yield scene
  52. scene.remove_node()
  53. for body in bodies:
  54. world.remove(body)