Browse Source

tests: add collision tests

Closes #583
hecris 6 years ago
parent
commit
2d9079713b

+ 28 - 0
tests/collide/collisions.py

@@ -0,0 +1,28 @@
+from panda3d.core import CollisionNode, NodePath
+from panda3d.core import CollisionTraverser, CollisionHandlerQueue
+from panda3d.core import CollisionSphere, CollisionBox, CollisionPolygon
+from panda3d.core import Point3, Vec3
+
+
+def make_collision(solid_from, solid_into):
+    node_from = CollisionNode("from")
+    node_from.add_solid(solid_from)
+    node_into = CollisionNode("into")
+    node_into.add_solid(solid_into)
+
+    root = NodePath("root")
+    trav = CollisionTraverser()
+    queue = CollisionHandlerQueue()
+
+    np_from = root.attach_new_node(node_from)
+    np_into = root.attach_new_node(node_into)
+
+    trav.add_collider(np_from, queue)
+    trav.traverse(root)
+
+    entry = None
+    for e in queue.get_entries():
+        if e.get_into() == solid_into:
+            entry = e
+
+    return (entry, np_from, np_into)

+ 19 - 0
tests/collide/test_into_box.py

@@ -0,0 +1,19 @@
+from collisions import *
+
+
+def test_sphere_into_box():
+    sphere = CollisionSphere(0, 0, 4, 3)
+    box = CollisionBox((0, 0, 0), 2, 3, 4)
+    entry = make_collision(sphere, box)[0]
+    assert entry is not None
+    assert entry.get_from() == sphere
+    assert entry.get_into() == box
+
+    # Colliding just on the edge
+    entry, np_from, np_into = make_collision(CollisionSphere(0, 0, 10, 6), box)
+    assert entry.get_surface_point(np_from) == Point3(0, 0, 4)
+    assert entry.get_surface_normal(np_into) == Vec3(0, 0, 1)  # Testing surface normal
+
+    # No collision
+    entry = make_collision(CollisionSphere(100, 100, 100, 100), box)[0]
+    assert entry is None

+ 39 - 0
tests/collide/test_into_poly.py

@@ -0,0 +1,39 @@
+from collisions import *
+
+
+def test_box_into_poly():
+    box = CollisionBox((0, 0, 0), 2, 3, 4)
+    poly = CollisionPolygon(Point3(0, 0, 0), Point3(0, 0, 1), Point3(0, 1, 1), Point3(0, 1, 0))
+
+    entry = make_collision(box, poly)[0]
+    assert entry is not None
+    assert entry.get_from() == box
+    assert entry.get_into() == poly
+
+    # Colliding just on the edge
+    entry, np_from, np_into = make_collision(CollisionBox((0, 3, 0), 1, 2, 1), poly)
+    assert entry.get_surface_point(np_from) == Point3(0, 3, 0)
+    assert entry.get_surface_normal(np_into) == Vec3(-1, 0, 0)  # Testing surface normal
+
+    # No collision
+    entry = make_collision(CollisionBox((10, 10, 10), 8, 9, 10), poly)[0]
+    assert entry is None
+
+
+def test_sphere_into_poly():
+    sphere = CollisionSphere(0, 0, 0, 1)
+    poly = CollisionPolygon(Point3(0, 0, 0), Point3(0, 0, 1), Point3(0, 1, 1), Point3(0, 1, 0))
+
+    entry = make_collision(sphere, poly)[0]
+    assert entry is not None
+    assert entry.get_from() == sphere
+    assert entry.get_into() == poly
+
+    # Colliding just on the edge
+    entry, np_from, np_into = make_collision(CollisionSphere(0, 0, 3, 2), poly)
+    assert entry.get_surface_point(np_from) == Point3(0, 0, 3)
+    assert entry.get_surface_normal(np_into) == Vec3(-1, 0, 0)  # Testing surface normal
+
+    # No collision
+    entry = make_collision(CollisionSphere(100, 100, 100, 100), poly)[0]
+    assert entry is None

+ 39 - 0
tests/collide/test_into_sphere.py

@@ -0,0 +1,39 @@
+from collisions import *
+
+
+def test_sphere_into_sphere():
+    sphere1 = CollisionSphere(0, 0, 3, 3)
+    sphere2 = CollisionSphere(0, 0, 0, 3)
+
+    entry = make_collision(sphere1, sphere2)[0]
+    assert entry is not None
+    assert entry.get_from() == sphere1
+    assert entry.get_into() == sphere2
+
+    # Colliding just on the edge
+    entry, np_from, np_into = make_collision(CollisionSphere(0, 0, 10, 7), sphere2)
+    assert entry.get_surface_point(np_from) == Point3(0, 0, 3)
+    assert entry.get_surface_normal(np_into) == Vec3(0, 0, 1)  # Testing surface normal
+
+    # No collision
+    entry = make_collision(CollisionSphere(0, 0, 10, 6), sphere2)[0]
+    assert entry is None
+
+
+def test_box_into_sphere():
+    box = CollisionBox((0, 0, 0), 2, 3, 4)
+    sphere = CollisionSphere(0, 0, 0, 3)
+
+    entry = make_collision(box, sphere)[0]
+    assert entry is not None
+    assert entry.get_from() == box
+    assert entry.get_into() == sphere
+
+    # Colliding just on the edge
+    entry, np_from, np_into = make_collision(CollisionBox((0, 0, 10), 6, 6, 7), sphere)
+    assert entry.get_surface_point(np_from) == Point3(0, 0, 3)
+    assert entry.get_surface_normal(np_into) == Vec3(0, 0, 1)  # Testing surface normal
+
+    # No collision
+    entry = make_collision(CollisionBox((0, 0, 10), 6, 6, 6), sphere)[0]
+    assert entry is None