test_bullet_heightfield.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import pytest
  2. # Skip these tests if we can't import bullet.
  3. bullet = pytest.importorskip("panda3d.bullet")
  4. from panda3d.bullet import BulletWorld, BulletRigidBodyNode, ZUp
  5. from panda3d.bullet import BulletHeightfieldShape, BulletSphereShape
  6. from panda3d.core import NodePath, PNMImage
  7. def make_node(name, BulletShape, *args):
  8. # Returns a BulletRigidBodyNode for the given shape
  9. shape = BulletShape(*args)
  10. node = BulletRigidBodyNode(name)
  11. node.add_shape(shape)
  12. return node
  13. def test_sphere_into_heightfield():
  14. root = NodePath("root")
  15. world = BulletWorld()
  16. # Create PNMImage to construct Heightfield with
  17. img = PNMImage(10, 10, 1)
  18. img.fill_val(255)
  19. # Make our nodes
  20. heightfield = make_node("Heightfield", BulletHeightfieldShape, img, 1, ZUp)
  21. sphere = make_node("Sphere", BulletSphereShape, 1)
  22. # Attach to world
  23. np1 = root.attach_new_node(sphere)
  24. np1.set_pos(0, 0, 1)
  25. world.attach(sphere)
  26. np2 = root.attach_new_node(heightfield)
  27. np2.set_pos(0, 0, 0)
  28. world.attach(heightfield)
  29. assert world.get_num_rigid_bodies() == 2
  30. test = world.contact_test_pair(sphere, heightfield)
  31. assert test.get_num_contacts() > 0
  32. assert test.get_contact(0).get_node0() == sphere
  33. assert test.get_contact(0).get_node1() == heightfield
  34. # Increment sphere's Z coordinate, no longer colliding
  35. np1.set_pos(0, 0, 2)
  36. test = world.contact_test_pair(sphere, heightfield)
  37. assert test.get_num_contacts() == 0