test_into_box.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. # No collision
  14. entry = make_collision(CollisionSphere(100, 100, 100, 100), box)[0]
  15. assert entry is None
  16. def test_plane_into_box():
  17. # CollisionPlane is not a 'from' object
  18. plane = CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, 0)))
  19. box = CollisionBox((0, 0, 0), 2, 3, 4)
  20. entry = make_collision(plane, box)[0]
  21. assert entry is None
  22. def test_ray_into_box():
  23. ray = CollisionRay(1, 1, 1, 0, 1, 0)
  24. box = CollisionBox((0, 0, 0), 3, 3, 5)
  25. entry = make_collision(ray, box)[0]
  26. assert entry is not None
  27. assert entry.get_from() == ray
  28. assert entry.get_into() == box
  29. # Colliding just on the edge
  30. entry, np_from, np_into = make_collision(CollisionRay(3, 3, 0, 1, -1, 0), box)
  31. assert entry.get_surface_point(np_from) == Point3(3, 3, 0)
  32. # No collision
  33. entry = make_collision(CollisionRay(0, 0, 100, 1, 0, 0), box)[0]
  34. assert entry is None
  35. def test_parabola_into_box():
  36. # Set up Parabola and Box
  37. parabola = CollisionParabola()
  38. parabola.set_t1(0)
  39. parabola.set_t2(2)
  40. box = CollisionBox((0,0,0), 3, 3, 3)
  41. # Parabola is inside the Box and not colliding
  42. parabola.set_parabola(
  43. LParabola((-1, 0, -1), (1, 0, 1), (1, 1, 1)))
  44. entry = make_collision(parabola, box)[0]
  45. assert entry is None
  46. # Parabola is inside the Box and colliding on its projectile
  47. parabola.set_parabola(
  48. LParabola((0, 0, 1), (0, 0, 1), (1, 1, 1)))
  49. # Parabola collides with Box when t == 1 at point (1, 1, 3)
  50. assert parabola.get_parabola().calc_point(1) == (1, 1, 3)
  51. entry, np_from, into = make_collision(parabola, box)
  52. assert entry.get_surface_point(np_from) == (1, 1, 3)
  53. assert entry.get_from() == parabola
  54. assert entry.get_into() == box
  55. # Parabola is inside the Box and colliding on one of the endpoints
  56. parabola.set_parabola(
  57. LParabola((0, 0, 0), (0, 0, 1), (-3, 0, -3)))
  58. entry, np_from, np_into = make_collision(parabola, box)
  59. assert entry.get_surface_point(np_from) == (-3, 0, -3)
  60. # Parabola is outside the Box and not colliding
  61. parabola.set_parabola(
  62. LParabola((0, 0, 0), (0, 0, 1), (-5, 0, 0)))
  63. entry = make_collision(parabola, box)[0]
  64. assert entry is None
  65. # Parabola is outside the Box and colliding on its projecticle
  66. parabola.set_parabola(
  67. LParabola((-2, -2, -2), (1, 1, 1), (4, 4, 4)))
  68. # Parabola collides with Box when t == 1 at point (3, 3, 3)
  69. assert parabola.get_parabola().calc_point(1) == (3, 3, 3)
  70. entry, np_from, into = make_collision(parabola, box)
  71. assert entry.get_surface_point(np_from) == (3, 3, 3)
  72. # Parabola is outside the Box and colliding on the first endpoint
  73. parabola.set_parabola(
  74. LParabola((1, 1, 1), (1, 1, 1), (3, 3, 3)))
  75. entry, np_from, np_into = make_collision(parabola, box)
  76. assert entry.get_surface_point(np_from) == (3, 3, 3)
  77. # Parabola is outside the Box and colliding on the second endpoint
  78. parabola.set_parabola(
  79. LParabola((1, 0, 1), (-1, 0, -1), (-5, -3, -5)))
  80. assert parabola.get_parabola().calc_point(2) == (-3, -3, -3)
  81. entry, np_from, np_into = make_collision(parabola, box)
  82. assert entry.get_surface_point(np_from) == (-3, -3, -3)
  83. # Parabola intersects the Box at two points,
  84. # t == 0 and t == 2 the earliest one should be chosen.
  85. parabola.set_parabola(
  86. LParabola((-1, -1, -1), (-1, -1, -1), (3, 3, 3)))
  87. entry, np_from, np_into = make_collision(parabola, box)
  88. assert entry.get_surface_point(np_from) == parabola.get_parabola().calc_point(0)
  89. # First point no longer intersecting
  90. parabola.set_t1(1)
  91. entry, np_from, np_into = make_collision(parabola, box)
  92. assert parabola.get_parabola().calc_point(2) == (-3, -3, -3)
  93. assert entry.get_surface_point(np_from) == parabola.get_parabola().calc_point(2)
  94. assert entry.get_surface_normal(np_from) is not None