def test_project(self): box = BoundingBox() box.vertices = [vec3(2), vec3(-1)] normal = vec3(1) min_box, max_box = CollisionSystem.project(box, normal) self.assertEqual(-1, min_box) self.assertEqual(2, max_box)
def test_rotate_around_x_and_y(self): m = identity() v1 = vec3(90, 90) rotate(m, v1) self.assertEqual(mat4([ [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1] ]), m) v2 = m * vec3(0, 1, 0) self.assertEqual(vec3(0, 0, 1), v2)
def test_rotate_and_translate(self): m = identity() rot = vec3(90) trans = vec3(1, 2, 3) rotate(m, rot) translate(m, trans) self.assertEqual(mat4([ [1.0, 0.0, 0.0, 1.0], [0.0, 0.0, -1.0, 2.0], [0.0, 1.0, 0.0, 3.0], [0.0, 0.0, 0.0, 1.0] ]), m) v = m * vec3(0, 1, 0) self.assertEqual(vec3(1, 2, 4), v)
def test_mul_vec3(self): m = mat4([ [1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1] ]) v = vec3(2, 3, 4) self.assertEqual(vec3(3, 5, 7), m * v) self.assertEqual(mat4([ [1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1] ]), m) self.assertEqual(vec3(2, 3, 4), v)
def test_translate(self): m = identity() v = vec3(1, 2, 3) translate(m, v) self.assertEqual(mat4([ [1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1] ]), m)
def test_not_collides_simple_position(self): box = BoundingBox() box.vertices = self.vertices box.normals = self.normals other = BoundingBox() other.vertices = self.vertices other.normals = self.normals matrix = identity() translate(matrix, vec3(3)) collides = CollisionSystem.collides(box, matrix, other, identity()) self.assertFalse(collides)
def test_cross_product(self): v1 = vec3() v2 = vec3() self.assertEqual(vec3(), cross(v1, v2)) v1 = vec3(1, 0, 0) v2 = vec3(0, 1, 0) self.assertEqual(vec3(0, 0, 1), cross(v1, v2))
def test_add(self): v1 = vec3(1, 2, 3) v2 = vec3(4, 5, 6) self.assertEqual(v1 + v2, vec3(5, 7, 9))
def test_spread(self): v = vec3(1, 2, 3) self.assertEqual([1, 2, 3], [*v])
def test_to_vec2(self): v = vec3(1, 2, 3) self.assertEqual(v.to_vec2({'x': 'y', 'y': 'x'}), vec2(2, 1)) self.assertEqual(v.to_vec2(['z', 'x']), vec2(3, 1)) self.assertEqual(v.to_vec2(('x', 'z')), vec2(1, 3))
def test_collides_example2(self): box = BoundingBox() box.vertices = [vec3(1.0, -1.0, -1.0), vec3(1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, -0.999999), vec3(0.999999, 1.0, 1.000001), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0)] box.normals = [vec3(1.0, -1.0, -2.0), vec3(2.0, -2.0, 1.0), vec3(-2.0, -1.0, 2.0), vec3(-1.0, -2.0, -1.0), vec3(2.0, 1.0, -1.0), vec3(1.0, 2.0, 2.0), vec3(-1.0, 1.0, 1.0), vec3(-2.0, 2.0, -2.0)] box_model_matrix = mat4() box_model_matrix.numbers = [[1.0, 0.0, 0.0, 2.500000000000009], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.5308084989341933e-16], [0.0, 0.0, 0.0, 1.0]] other = BoundingBox() other.vertices = [ vec3(1.0, -1.0, -1.0), vec3(1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, -0.999999), vec3(0.999999, 1.0, 1.000001), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0)] other.normals = [vec3(1.0, -1.0, -2.0), vec3(2.0, -2.0, 1.0), vec3(-2.0, -1.0, 2.0), vec3(-1.0, -2.0, -1.0), vec3(2.0, 1.0, -1.0), vec3(1.0, 2.0, 2.0), vec3(-1.0, 1.0, 1.0), vec3(-2.0, 2.0, -2.0)] other_model_matrix = mat4() other_model_matrix.numbers = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]] collides = CollisionSystem.collides(box, box_model_matrix, other, other_model_matrix) self.assertFalse(collides)
class CollisionTest(unittest.TestCase): def test_project(self): box = BoundingBox() box.vertices = [vec3(2), vec3(-1)] normal = vec3(1) min_box, max_box = CollisionSystem.project(box, normal) self.assertEqual(-1, min_box) self.assertEqual(2, max_box) normals = [vec3(1), vec3(-1), vec3(0, 1), vec3(0, -1), vec3(0, 0, 1), vec3(0, 0, -1)] vertices = [ vec3(1, 1, 1), vec3(-1, 1, 1), vec3(1, -1, 1), vec3(1, 1, -1), vec3(1, -1, -1), vec3(-1, 1, -1), vec3(-1, -1, 1), vec3(-1, -1, -1) ] def test_collides_simple(self): box = BoundingBox() box.vertices = self.vertices box.normals = self.normals other = BoundingBox() other.vertices = self.vertices other.normals = self.normals matrix = identity() collides = CollisionSystem.collides(box, matrix, other, matrix) self.assertTrue(collides) def test_collides_simple_position(self): box = BoundingBox() box.vertices = self.vertices box.normals = self.normals other = BoundingBox() other.vertices = self.vertices other.normals = self.normals matrix = identity() translate(matrix, vec3(1)) collides = CollisionSystem.collides(box, matrix, other, matrix) self.assertTrue(collides) @unittest.skip("Fix this") def test_not_collides_simple_position(self): box = BoundingBox() box.vertices = self.vertices box.normals = self.normals other = BoundingBox() other.vertices = self.vertices other.normals = self.normals matrix = identity() translate(matrix, vec3(3)) collides = CollisionSystem.collides(box, matrix, other, identity()) self.assertFalse(collides) @unittest.skip("Fix this") def test_collides_example(self): vertices = [ vec3(1.0, -1.0, -1.0), vec3(1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, -0.999999), vec3(0.999999, 1.0, 1.000001), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0) ] normals = [ vec3(1.0, -1.0, -2.0), vec3(2.0, -2.0, 1.0), vec3(-2.0, -1.0, 2.0), vec3(-1.0, -2.0, -1.0), vec3(2.0, 1.0, -1.0), vec3(1.0, 2.0, 2.0), vec3(-1.0, 1.0, 1.0), vec3(-2.0, 2.0, -2.0) ] box = BoundingBox() box.vertices = vertices box.normals = normals matrix = identity() translate(matrix, vec3(0, 0, 25)) other = BoundingBox() other.vertices = vertices other.normals = normals other_matrix = identity() translate(other_matrix, vec3(5, 0, 25)) collides = CollisionSystem.collides(box, matrix, other, other_matrix) self.assertFalse(collides) @unittest.skip("Fix this") def test_collides_example2(self): box = BoundingBox() box.vertices = [vec3(1.0, -1.0, -1.0), vec3(1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, -0.999999), vec3(0.999999, 1.0, 1.000001), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0)] box.normals = [vec3(1.0, -1.0, -2.0), vec3(2.0, -2.0, 1.0), vec3(-2.0, -1.0, 2.0), vec3(-1.0, -2.0, -1.0), vec3(2.0, 1.0, -1.0), vec3(1.0, 2.0, 2.0), vec3(-1.0, 1.0, 1.0), vec3(-2.0, 2.0, -2.0)] box_model_matrix = mat4() box_model_matrix.numbers = [[1.0, 0.0, 0.0, 2.500000000000009], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.5308084989341933e-16], [0.0, 0.0, 0.0, 1.0]] other = BoundingBox() other.vertices = [ vec3(1.0, -1.0, -1.0), vec3(1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, -0.999999), vec3(0.999999, 1.0, 1.000001), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0)] other.normals = [vec3(1.0, -1.0, -2.0), vec3(2.0, -2.0, 1.0), vec3(-2.0, -1.0, 2.0), vec3(-1.0, -2.0, -1.0), vec3(2.0, 1.0, -1.0), vec3(1.0, 2.0, 2.0), vec3(-1.0, 1.0, 1.0), vec3(-2.0, 2.0, -2.0)] other_model_matrix = mat4() other_model_matrix.numbers = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]] collides = CollisionSystem.collides(box, box_model_matrix, other, other_model_matrix) self.assertFalse(collides)
def test_collides_example(self): vertices = [ vec3(1.0, -1.0, -1.0), vec3(1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, -0.999999), vec3(0.999999, 1.0, 1.000001), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0) ] normals = [ vec3(1.0, -1.0, -2.0), vec3(2.0, -2.0, 1.0), vec3(-2.0, -1.0, 2.0), vec3(-1.0, -2.0, -1.0), vec3(2.0, 1.0, -1.0), vec3(1.0, 2.0, 2.0), vec3(-1.0, 1.0, 1.0), vec3(-2.0, 2.0, -2.0) ] box = BoundingBox() box.vertices = vertices box.normals = normals matrix = identity() translate(matrix, vec3(0, 0, 25)) other = BoundingBox() other.vertices = vertices other.normals = normals other_matrix = identity() translate(other_matrix, vec3(5, 0, 25)) collides = CollisionSystem.collides(box, matrix, other, other_matrix) self.assertFalse(collides)