test_bullet_bam.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import pytest
  2. # Skip these tests if we can't import bullet.
  3. bullet = pytest.importorskip("panda3d.bullet")
  4. from panda3d import core
  5. def reconstruct(object):
  6. # Create a temporary buffer, which we first write the object into, and
  7. # subsequently read it from again.
  8. buffer = core.DatagramBuffer()
  9. writer = core.BamWriter(buffer)
  10. writer.init()
  11. writer.write_object(object)
  12. reader = core.BamReader(buffer)
  13. reader.init()
  14. object = reader.read_object()
  15. reader.resolve()
  16. return object
  17. def test_box_shape():
  18. shape = bullet.BulletBoxShape((1, 2, 3))
  19. shape.margin = 0.5
  20. shape2 = reconstruct(shape)
  21. assert type(shape) is type(shape2)
  22. assert shape.margin == shape2.margin
  23. assert shape.name == shape2.name
  24. assert shape.half_extents_without_margin == shape2.half_extents_without_margin
  25. assert shape.half_extents_with_margin == shape2.half_extents_with_margin
  26. def test_capsule_shape():
  27. shape = bullet.BulletCapsuleShape(1.4, 3.5, bullet.Y_up)
  28. shape.margin = 0.5
  29. shape2 = reconstruct(shape)
  30. assert type(shape) is type(shape2)
  31. assert shape.margin == shape2.margin
  32. assert shape.name == shape2.name
  33. assert shape.radius == shape2.radius
  34. assert shape.height == shape2.height
  35. def test_cone_shape():
  36. shape = bullet.BulletConeShape(1.4, 3.5, bullet.Y_up)
  37. shape.margin = 0.5
  38. shape2 = reconstruct(shape)
  39. assert type(shape) is type(shape2)
  40. assert shape.margin == shape2.margin
  41. assert shape.name == shape2.name
  42. assert shape.radius == shape2.radius
  43. assert shape.height == shape2.height
  44. def test_cylinder_shape():
  45. shape = bullet.BulletCylinderShape(1.4, 3.5, bullet.Y_up)
  46. shape.margin = 0.5
  47. shape2 = reconstruct(shape)
  48. assert type(shape) is type(shape2)
  49. assert shape.margin == shape2.margin
  50. assert shape.name == shape2.name
  51. assert shape.radius == shape2.radius
  52. assert shape.half_extents_without_margin == shape2.half_extents_without_margin
  53. assert shape.half_extents_with_margin == shape2.half_extents_with_margin
  54. def test_minkowski_sum_shape():
  55. box = bullet.BulletBoxShape((1, 2, 3))
  56. sphere = bullet.BulletSphereShape(2.0)
  57. shape = bullet.BulletMinkowskiSumShape(box, sphere)
  58. shape.transform_a = core.TransformState.make_pos((8, 7, 3))
  59. shape.transform_b = core.TransformState.make_hpr((45, -10, 110))
  60. shape.margin = 0.5
  61. shape2 = reconstruct(shape)
  62. assert type(shape) is type(shape2)
  63. assert shape.margin == shape2.margin
  64. assert shape.name == shape2.name
  65. assert shape.transform_a.mat.compare_to(shape2.transform_a.mat, 0.001) == 0
  66. assert shape.transform_b.mat.compare_to(shape2.transform_b.mat, 0.001) == 0
  67. assert type(shape.shape_a) == type(shape2.shape_a)
  68. assert type(shape.shape_b) == type(shape2.shape_b)
  69. def test_multi_sphere_shape():
  70. shape = bullet.BulletMultiSphereShape([(1, 2, 3), (4, 5, 6)], [1.5, 2.5])
  71. shape.margin = 0.5
  72. shape2 = reconstruct(shape)
  73. assert type(shape) is type(shape2)
  74. assert shape.margin == shape2.margin
  75. assert shape.name == shape2.name
  76. assert shape.sphere_count == shape2.sphere_count
  77. assert tuple(shape.sphere_pos) == tuple(shape2.sphere_pos)
  78. assert tuple(shape.sphere_radius) == tuple(shape2.sphere_radius)
  79. def test_plane_shape():
  80. shape = bullet.BulletPlaneShape((1.2, 0.2, 0.5), 2.5)
  81. shape.margin = 0.5
  82. shape2 = reconstruct(shape)
  83. assert type(shape) is type(shape2)
  84. assert shape.margin == shape2.margin
  85. assert shape.name == shape2.name
  86. assert shape.plane_normal.almost_equal(shape2.plane_normal, 0.1)
  87. assert shape.plane_constant == shape2.plane_constant
  88. def test_sphere_shape():
  89. shape = bullet.BulletSphereShape(1.4)
  90. shape.margin = 0.5
  91. shape2 = reconstruct(shape)
  92. assert type(shape) is type(shape2)
  93. assert shape.margin == shape2.margin
  94. assert shape.name == shape2.name
  95. assert shape.radius == shape2.radius
  96. def test_convex_shape():
  97. shape = bullet.BulletConvexHullShape()
  98. shape.add_array([
  99. (-1.0, -1.0, -1.0),
  100. (1.0, -1.0, -1.0),
  101. (-1.0, 1.0, -1.0),
  102. (-1.0, -1.0, 1.0),
  103. (1.0, 1.0, -1.0),
  104. (1.0, -1.0, 1.0),
  105. (-1.0, 1.0, 1.0),
  106. (1.0, 1.0, 1.0),
  107. ])
  108. shape.margin = 0.5
  109. shape2 = reconstruct(shape)
  110. assert type(shape) is type(shape2)
  111. assert shape.margin == shape2.margin
  112. def test_ghost():
  113. node = bullet.BulletGhostNode("some ghost node")
  114. node2 = reconstruct(node)
  115. assert type(node) is type(node2)
  116. assert node.name == node2.name