test_into_box.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from collisions import *
  2. def test_sphere_into_box():
  3. sphere = CollisionSphere(0, 0, 4, 3)
  4. box = CollisionBox((0, 0, 0), 2, 3, 4)
  5. entry = make_collision(sphere, box)[0]
  6. assert entry is not None
  7. assert entry.get_from() == sphere
  8. assert entry.get_into() == box
  9. # Colliding just on the edge
  10. entry, np_from, np_into = make_collision(CollisionSphere(0, 0, 10, 6), box)
  11. assert entry.get_surface_point(np_from) == Point3(0, 0, 4)
  12. assert entry.get_surface_normal(np_into) == Vec3(0, 0, 1) # Testing surface normal
  13. # Fully inside
  14. entry, np_from, np_into = make_collision(CollisionSphere(0, 0, 0, 1), box)
  15. assert entry is not None
  16. # Inside but touching one of the sides
  17. entry, np_from, np_into = make_collision(CollisionSphere(1.5, 0, 0, 1), box)
  18. assert entry is not None
  19. # Exactly inside one of the sides
  20. entry, np_from, np_into = make_collision(CollisionSphere(2, 0, 0, 1), box)
  21. assert entry is not None
  22. # Touching one of the sides from the outside
  23. entry, np_from, np_into = make_collision(CollisionSphere(2.5, 0, 0, 1), box)
  24. assert entry is not None
  25. # Outside on one of the axes
  26. entry, np_from, np_into = make_collision(CollisionSphere(3.5, 0, 0, 1), box)
  27. assert entry is None
  28. # No collision
  29. entry = make_collision(CollisionSphere(100, 100, 100, 100), box)[0]
  30. assert entry is None
  31. def test_plane_into_box():
  32. # CollisionPlane is not a 'from' object
  33. plane = CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, 0)))
  34. box = CollisionBox((0, 0, 0), 2, 3, 4)
  35. entry = make_collision(plane, box)[0]
  36. assert entry is None
  37. def test_ray_into_box():
  38. ray = CollisionRay(1, 1, 1, 0, 1, 0)
  39. box = CollisionBox((0, 0, 0), 3, 3, 5)
  40. entry = make_collision(ray, box)[0]
  41. assert entry is not None
  42. assert entry.get_from() == ray
  43. assert entry.get_into() == box
  44. # Colliding just on the edge
  45. entry, np_from, np_into = make_collision(CollisionRay(3, 3, 0, 1, -1, 0), box)
  46. assert entry.get_surface_point(np_from) == Point3(3, 3, 0)
  47. # No collision
  48. entry = make_collision(CollisionRay(0, 0, 100, 1, 0, 0), box)[0]
  49. assert entry is None
  50. def test_parabola_into_box():
  51. # Set up Parabola and Box
  52. parabola = CollisionParabola()
  53. parabola.set_t1(0)
  54. parabola.set_t2(2)
  55. box = CollisionBox((0,0,0), 3, 3, 3)
  56. # Parabola is inside the Box and not colliding
  57. parabola.set_parabola(
  58. LParabola((-1, 0, -1), (1, 0, 1), (1, 1, 1)))
  59. entry = make_collision(parabola, box)[0]
  60. assert entry is None
  61. # Parabola is inside the Box and colliding on its projectile
  62. parabola.set_parabola(
  63. LParabola((0, 0, 1), (0, 0, 1), (1, 1, 1)))
  64. # Parabola collides with Box when t == 1 at point (1, 1, 3)
  65. assert parabola.get_parabola().calc_point(1) == (1, 1, 3)
  66. entry, np_from, into = make_collision(parabola, box)
  67. assert entry.get_surface_point(np_from) == (1, 1, 3)
  68. assert entry.get_from() == parabola
  69. assert entry.get_into() == box
  70. # Parabola is inside the Box and colliding on one of the endpoints
  71. parabola.set_parabola(
  72. LParabola((0, 0, 0), (0, 0, 1), (-3, 0, -3)))
  73. entry, np_from, np_into = make_collision(parabola, box)
  74. assert entry.get_surface_point(np_from) == (-3, 0, -3)
  75. # Parabola is outside the Box and not colliding
  76. parabola.set_parabola(
  77. LParabola((0, 0, 0), (0, 0, 1), (-5, 0, 0)))
  78. entry = make_collision(parabola, box)[0]
  79. assert entry is None
  80. # Parabola is outside the Box and colliding on its projecticle
  81. parabola.set_parabola(
  82. LParabola((-2, -2, -2), (1, 1, 1), (4, 4, 4)))
  83. # Parabola collides with Box when t == 1 at point (3, 3, 3)
  84. assert parabola.get_parabola().calc_point(1) == (3, 3, 3)
  85. entry, np_from, into = make_collision(parabola, box)
  86. assert entry.get_surface_point(np_from) == (3, 3, 3)
  87. # Parabola is outside the Box and colliding on the first endpoint
  88. parabola.set_parabola(
  89. LParabola((1, 1, 1), (1, 1, 1), (3, 3, 3)))
  90. entry, np_from, np_into = make_collision(parabola, box)
  91. assert entry.get_surface_point(np_from) == (3, 3, 3)
  92. # Parabola is outside the Box and colliding on the second endpoint
  93. parabola.set_parabola(
  94. LParabola((1, 0, 1), (-1, 0, -1), (-5, -3, -5)))
  95. assert parabola.get_parabola().calc_point(2) == (-3, -3, -3)
  96. entry, np_from, np_into = make_collision(parabola, box)
  97. assert entry.get_surface_point(np_from) == (-3, -3, -3)
  98. # Parabola intersects the Box at two points,
  99. # t == 0 and t == 2 the earliest one should be chosen.
  100. parabola.set_parabola(
  101. LParabola((-1, -1, -1), (-1, -1, -1), (3, 3, 3)))
  102. entry, np_from, np_into = make_collision(parabola, box)
  103. assert entry.get_surface_point(np_from) == parabola.get_parabola().calc_point(0)
  104. # First point no longer intersecting
  105. parabola.set_t1(1)
  106. entry, np_from, np_into = make_collision(parabola, box)
  107. assert parabola.get_parabola().calc_point(2) == (-3, -3, -3)
  108. assert entry.get_surface_point(np_from) == parabola.get_parabola().calc_point(2)
  109. assert entry.get_surface_normal(np_from) is not None